1. numpy & pytorch
Torch calls itself the Numpy of the neural network world, because it can put the tensor generated by torch in the GPU to accelerate the operation (provided you have a suitable GPU), just like Numpy will put the array in the CPU to accelerate the operation.
Convert numpy array and torch tensor freely in pytorch.
import torch import numpy as np np_data = np.arange(6).reshape((2, 3)) torch_data = torch.from_numpy(np_data) tensor2array = torch_data.numpy() print( '\nnumpy array:', np_data, # [[0 1 2], [3 4 5]] '\ntorch tensor:', torch_data, # 0 1 2 \n 3 4 5 [torch.LongTensor of size 2x3] '\ntensor to array:', tensor2array, # [[0 1 2], [3 4 5]] )
2. Mathematical operations in Torch
# abs absolute value calculation data = [-1, -2, 1, 2] tensor = torch.FloatTensor(data) # Convert to 32-bit floating point tensor print( '\nabs', '\nnumpy: ', np.abs(data), # [1 2 1 2] '\ntorch: ', torch.abs(tensor) # [1 2 1 2] ) # sin trigonometric function sin print( '\ nsin', '\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743] '\ntorch: ', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093] ) # mean mean print( '\nmean', '\nnumpy: ', np.mean(data), # 0.0 '\ntorch: ', torch.mean(tensor) # 0.0 )
# matrix multiplication matrix dot multiplication data = [[1,2], [3,4]] tensor = torch.FloatTensor(data) # Convert to 32-bit floating point tensor # correct method print( '\ nmatrix multiplication (matmul)', '\nnumpy: ', np.matmul(data, data), # [[7, 10], [15, 22]] '\ntorch: ', torch.mm(tensor, tensor) # [[7, 10], [15, 22]] ) # !!!! BELOW IS THE WRONG WAY!!!! data = np.array(data) print( '\nmatrix multiplication (dot)', '\nnumpy: ', data.dot(data), # [[7, 10], [15, 22]] works in numpy '\ntorch: ', tensor.dot(tensor) # torch will be converted to [1,2,3,4].dot([1,2,3,4) = 30.0 )
In the new version (>=0.3.0), there are new changes to tensor.dot(), which can only be used for one-dimensional arrays. So the above will be wrong.
Reference: https://morvanzhou.github.io/tutorials/machine-learning/torch/2-01-torch-numpy/
https://www.jianshu.com/p/5ae644748f21