pytorch--tensor.copy_()

tensor.copy_(src)

将src中的元素复制到tensor中并返回这个tensor; 两个tensor应该有相同shape

例子:

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

输出

tensor([[0.1604, 0.0176],
        [0.3737, 0.2009],
        [0.1438, 0.8394]])
        
tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])
[Finished in 1.9s]
发布了33 篇原创文章 · 获赞 1 · 访问量 2613

猜你喜欢

转载自blog.csdn.net/orangerfun/article/details/104011228