[PYTHON] [Pytorch] numpy to tensor

Write how to change from numpy to tensor with pytorch.

numpy to tensor

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

[Pytorch] numpy to tensor
[PyTorch] Sample ② ~ TENSOR ~
[PyTorch] Sample ① ~ NUMPY ~
Basics of PyTorch (1) -How to use Tensor-
Introduction to Lightning pytorch
How to use numpy
How to operate NumPy
Introduction to PyTorch (1) Automatic differentiation
Convert numpy int64 to python int
How to install mkl numpy
[PyTorch] Tutorial (Japanese version) ① ~ Tensor ~
Subscript access to python numpy array
Introduction to Python Numerical Library NumPy
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Details (?)] Introduction to pytorch ~ CNN CIFAR10 ~
I tried to explain Pytorch dataset
How to Data Augmentation with PyTorch
How to call PyTorch in Julia
[Note] [PyTorch] From installation to easy usage
[PyTorch] Introduction to document classification using BERT
[Python] Introduction to CNN with Pytorch MNIST
PyTorch Tensor max, min specifications are inconvenient
[Introduction to Pytorch] I played with sinGAN ♬
I tried to implement CVAE with PyTorch
How to install NumPy on Raspberry Pi