View more, visit my tutorial page: https://morvanzhou.github.io/tutorials/ My Youtube Channel: https://www.youtube.com/user/MorvanZhou
Dependencies:
Details about math operation in torch can be found in: http://pytorch.org/docs/torch.html#math-operations
import torch
import numpy as np
# convert numpy to tensor or vise versa
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]]
)
numpy array: [[0 1 2] [3 4 5]] torch tensor: 0 1 2 3 4 5 [torch.LongTensor of size 2x3] tensor to array: [[0 1 2] [3 4 5]]
# abs
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 32-bit floating point
print(
'\nabs',
'\nnumpy: ', np.abs(data), # [1 2 1 2]
'\ntorch: ', torch.abs(tensor) # [1 2 1 2]
)
abs numpy: [1 2 1 2] torch: 1 2 1 2 [torch.FloatTensor of size 4]
tensor.abs()
1 2 1 2 [torch.FloatTensor of size 4]
# 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]
)
sin numpy: [-0.84147098 -0.90929743 0.84147098 0.90929743] torch: -0.8415 -0.9093 0.8415 0.9093 [torch.FloatTensor of size 4]
tensor.sigmoid()
0.2689 0.1192 0.7311 0.8808 [torch.FloatTensor of size 4]
tensor.exp()
0.3679 0.1353 2.7183 7.3891 [torch.FloatTensor of size 4]
# mean
print(
'\nmean',
'\nnumpy: ', np.mean(data), # 0.0
'\ntorch: ', torch.mean(tensor) # 0.0
)
mean numpy: 0.0 torch: 0.0
# matrix multiplication
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data) # 32-bit floating point
# correct method
print(
'\nmatrix multiplication (matmul)',
'\nnumpy: ', np.matmul(data, data), # [[7, 10], [15, 22]]
'\ntorch: ', torch.mm(tensor, tensor) # [[7, 10], [15, 22]]
)
matrix multiplication (matmul) numpy: [[ 7 10] [15 22]] torch: 7 10 15 22 [torch.FloatTensor of size 2x2]
# incorrect method
data = np.array(data)
print(
'\nmatrix multiplication (dot)',
'\nnumpy: ', data.dot(data), # [[7, 10], [15, 22]]
'\ntorch: ', tensor.dot(tensor) # this will convert tensor to [1,2,3,4], you'll get 30.0
)
matrix multiplication (dot) numpy: [[ 7 10] [15 22]] torch: 30.0
Note that:
torch.dot(tensor1, tensor2) → float
Computes the dot product (inner product) of two tensors. Both tensors are treated as 1-D vectors.
tensor.mm(tensor)
7 10 15 22 [torch.FloatTensor of size 2x2]
tensor * tensor
1 4 9 16 [torch.FloatTensor of size 2x2]
tensor.dot(tensor)
30.0