Rate this Page

torch.from_numpy#

torch.from_numpy(ndarray)Tensor#

Creates aTensor from anumpy.ndarray.

The returned tensor andndarray share the same memory. Modifications tothe tensor will be reflected in thendarray and vice versa. The returnedtensor is not resizable.

It currently acceptsndarray with dtypes ofnumpy.float64,numpy.float32,numpy.float16,numpy.complex64,numpy.complex128,numpy.int64,numpy.int32,numpy.int16,numpy.int8,numpy.uint8,andbool.

Warning

Writing to a tensor created from a read-only NumPy array is not supported and will result in undefined behavior.

Example:

>>>a=numpy.array([1,2,3])>>>t=torch.from_numpy(a)>>>ttensor([ 1,  2,  3])>>>t[0]=-1>>>aarray([-1,  2,  3])