As shown below:
# -*- coding: utf-8 -*- # @Time : 2018/1/17 16:37 # @Author : Zhiwei Zhong # @Site : # @File : Numpy_Pytorch.py # @Software: PyCharm import torch import numpy as np np_data = (6).reshape((2, 3)) # numpy to pytorch format torch_data = torch.from_numpy(np_data) print( '\n numpy', np_data, '\n torch', torch_data, ) ''' numpy [[0 1 2] [3 4 5]] torch 0 1 2 3 4 5 [ of size 2x3] ''' # torch to numpy tensor2array = torch_data.numpy() print(tensor2array) """ [[0 1 2] [3 4 5]] """ # Operators # abs, add, similar to numpy data = [[1, 2], [3, 4]] tensor = (data) # Convert to 32-bit floating point, torch accepts all in the form of Tensor, so convert to Tensor before operation print( '\n numpy', (data, data), '\n torch', (tensor, tensor) # () is a dot product ) ''' numpy [[ 7 10] [15 22]] torch 7 10 15 22 [ of size 2x2] '''
The above this talk about the difference between pytorch and Numpy and the method of conversion to each other is all that I have shared with you, I hope to be able to give you a reference, and I hope that you will support me more.