Write how to change from numpy to tensor with pytorch.
Use torch.from_numpy (ndarray). The ndarray contains a numpy matrix.
>>>import numpy
>>>import torch
>>> a = numpy.array([0, 1, 2])
>>> t = torch.from_numpy(a)
>>> t
tensor([ 0, 1, 2])
>>> t[0] = 1
>>> a
array([ 1, 1, 2])
From the above example, you can see that the array can be changed to a tensor. Also, it can be seen that the array of a and the tensor of t share memory.
Recommended Posts