1
$\begingroup$
t=tensor([0.1 0.2 0.3 0.4 0.5 0.6])

now i need to modify this existing tensor t as follows:

t=tensor([0.1 0.2 0.7 0.4 0.8 0.6])

I tried as follows:

t=tensor([0.1 0.2 0.3 0.4 0.5 0.6])
a=tensor([0.1 0.2 0.7 0.4 0.8 0.6])
index=range(len(a))
t.index_copy_(0,index,a)

But still it is not updating how can i modify the tensor in pytorch?

$\endgroup$

1 Answer 1

2
$\begingroup$

Try this:

t = tensor([0.1, 0.2, 0.3, 0.4, 0.5, 0.6])
t.index_copy_(0, tensor([2, 4]), tensor([0.7, 0.8]))

Reference:

torch.Tensor.index_copy_

$\endgroup$
3
  • $\begingroup$ Still I'm unable to update it $\endgroup$ May 29, 2021 at 6:03
  • $\begingroup$ Please share your full code in your question, maybe there is something that is done wrong.. $\endgroup$
    – Nikos M.
    May 29, 2021 at 9:25
  • $\begingroup$ datascience.stackexchange.com/questions/95027/… I would like to update tensor which is in dataloader may be because of that i'm unable to update i guess $\endgroup$ May 29, 2021 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.