[resolved] How does one create copy of tensors in PyTorch?


(Brando Miranda) #1

I was trying to create new copies (so that python was pointing at different objects and not just updating the same copy across multiple namespaces). I tried it by getting the actual numpy data from a tensor and using that to create a brand new tensor but that didn’t work. Why did it not work? How does one do this?

>>> y

 3  3
 3  3
 3  3
[torch.FloatTensor of size 3x2]

>>> yv.data = torch.FloatTensor( y.numpy() )
>>> yv
Variable containing:
 3  3
 3  3
 3  3
[torch.FloatTensor of size 3x2]

>>> y

 3  3
 3  3
 3  3
[torch.FloatTensor of size 3x2]

>>> yv.data.fill_(5)

 5  5
 5  5
 5  5
[torch.FloatTensor of size 3x2]

>>> yv
Variable containing:
 5  5
 5  5
 5  5
[torch.FloatTensor of size 3x2]

>>> y

 5  5
 5  5
 5  5
[torch.FloatTensor of size 3x2]

(Brando Miranda) #2

clone maybe?

need to check it out…


this seems to be Variables but I wanted just Tensor only…


(Brando Miranda) #3

ok that works:

>>> yv.data = y.clone()
>>> y

 5  5
 5  5
 5  5
[torch.FloatTensor of size 3x2]

>>> yv
Variable containing:
 5  5
 5  5
 5  5
[torch.FloatTensor of size 3x2]

>>> yv.data.fill_(10)

 10  10
 10  10
 10  10
[torch.FloatTensor of size 3x2]

>>> y

 5  5
 5  5
 5  5
[torch.FloatTensor of size 3x2]

>>> yv
Variable containing:
 10  10
 10  10
 10  10
[torch.FloatTensor of size 3x2]