stay pytorch in , hold numpy.array Data conversion to tensor tensor Common functions of data are torch.from_numpy(array) perhaps torch.Tensor(array), The first function is more commonly used , However, in the pytorch0.4 This function has been abandoned in , The following simple programming experiment shows the difference between the two methods , The experiment was conducted in pytorch0.4 Under the framework



After running the program , The result is




You can see that the array was modified a Element value of , tensor b The element values of the , But tensors c But it doesn't change . Modify tensor c Element value of , array a Sum tensor b The element values of the . This shows that torch.from_numpy(array) It's a shallow copy of an array ,torch.Tensor(array) It's a deep copy of an array