pytorch torch.from_numpy()(numpy配列からテンソルを作成し、配列とテンソルは同じメモリを共有します)



Pytorch Torch From_numpy Create Tensor From Numpy Array



https://pytorch.org/docs/1.1.0/torch.html?highlight=numpy#torch.from_numpy

torch.from_numpy(ndarray) → Tensor Creates a Tensor from a numpy.ndarray. From numpy.ndarray creates a tensor. The returned tensor and ndarray share the same memory. Modifications to the tensor will be reflected in the ndarray and vice versa. The returned tensor is not resizable. The returned tensor and ndarray share the same memory. Modifications to the tensor will be reflected in the ndarray and vice versa. The returned tensor cannot be resized. Example: >>> a = numpy.array([1, 2, 3]) >>> t = torch.from_numpy(a) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([-1, 2, 3])