pytorch--tensor.copy_()

tensor.copy_(src)

Copy the elements src tensor and returns to the tensor; two tensor should have the same shape

example:

x = torch.tensor([[1,2], [3,4], [5,6]])
y = torch.rand((3,2))
print(y)
y.copy_(x)
print(y)

Export

tensor([[0.1604, 0.0176],
        [0.3737, 0.2009],
        [0.1438, 0.8394]])
        
tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])
[Finished in 1.9s]
Published 33 original articles · won praise 1 · views 2613

Guess you like

Origin blog.csdn.net/orangerfun/article/details/104011228