目录

1 tensor->array(tensor.numpy())

2 array->tensor(torch.from_numpy(array))


1 tensor->array(tensor.numpy())

x=torch.ones(3,2)
y=x.numpy()
print(x)
print(y)

底层是一样的数据

x.add_(1)
print(x)
print(y)

但是,如果不用add命令,而是用+,则两者又会不一样

x=x+z
print(x)
print(y)

2 array->tensor(torch.from_numpy(array))

m=np.ones((3,2))
n=torch.from_numpy(m)
print(m)
print(n)