numpy 和 torch 对比
- numpy、torch的相互转换
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(np_data)
print(torch_data)
print(tensor2array)
运行结果:
[[0 1 2]
[3 4 5]]
tensor([[0, 1, 2],
[3, 4, 5]], dtype=torch.int32)
[[0 1 2]
[3 4 5]]
- 数学运算
# abs 绝对值计算
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor
print(
'\nabs',
'\nnumpy: ', np.abs(data), # [1 2 1 2]
'\ntorch: ', torch.abs(tensor) # [1 2 1 2]
)
# sin 三角函数 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 均值
print(
'\nmean',
'\nnumpy: ', np.mean(data), # 0.0
'\ntorch: ', torch.mean(tensor) # 0.0
)
- 矩阵运算
# matrix multiplication 矩阵点乘
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor
# correct method
print(
'\nmatrix multiplication (matmul)',
'\nnumpy: ', np.matmul(data, data), # [[7, 10], [15, 22]] 或者用 data.dot(data)
'\ntorch: ', torch.mm(tensor, tensor) # [[7, 10], [15, 22]] 在torch中dot是限于一维数据,会将数据展平之后再运算
)
变量 Variable
- 变量Variable的定义
from torch.autograd import Variable
tensor = torch.FloatTensor([[1,2],[3,4]])
variable = Variable(tensor, requires_grad=True)
-
变量Variable与tensor的区别
tensor 不能计算梯度,而Variable可以
v_out = torch.mean(variable*variable)
v_out.backward()
print(variable.grad)
- 获取Variable里的数据
print(variable) # Variable 形式
"""
Variable containing:
1 2
3 4
[torch.FloatTensor of size 2x2]
"""
print(variable.data) # tensor 形式
"""
1 2
3 4
[torch.FloatTensor of size 2x2]
"""
print(variable.data.numpy()) # numpy 形式
"""
[[ 1. 2.]
[ 3. 4.]]
"""
激励函数
import torch
import torch.nn.functional as F # 激励函数都在这
from torch.autograd import Variable
x = torch.tensor([[1,2],[3,4]]).numpy()
# relu函数
y_relu = F.relu(x)
# sigmoid函数
y_sigmoid = F.sigmoid(x)
# tanh函数
y_tanh = F.tanh(x)
# softplus函数
y_softplus = F.softplus(x)
# softmax函数
y_softmax = F.softmax(x)