一、一些簡單的運算
1、numpy與torch數據形式轉換
import torch
import numpy as np
np_data = np.arange(6).reshape(2,3)
torch_data = torch.from_numpy(np_data)#numpy轉torch
torch2array = torch_data.numpy()#torch轉numpy
print(
'\nnumpy',np_data,
'\ntorch',torch_data,
'\ntorch2array',torch2array
)
輸出
numpy [[0 1 2] [3 4 5]] torch 0 1 2 3 4 5 [torch.LongTensor of size 2x3] torch2array [[0 1 2] [3 4 5]]
2、絕對值
import torch
import numpy as np
#abs
data = [-1,-2,1,2]
tensor = torch.FloatTensor(data) #32it
print(
'\nabs',
'\nnumpy',np.abs(data),
'\ntorch',torch.abs(tensor),
)
abs numpy [1 2 1 2] torch 1 2 1 2 [torch.FloatTensor of size 4]
3、矩陣相乘
import torch
import numpy as np
#abs
data = [[1,2],[3,4]]
tensor = torch.FloatTensor(data) #32it
print(
'\nabs',
'\nnumpy',np.matmul(data,data),
'\ntorch',torch.mm(tensor,tensor),
)
matmul numpy [[ 7 10] [15 22]] torch 7 10 15 22 [torch.FloatTensor of size 2x2]
二、variable
import torch from torch.autograd import Variable import numpy as np #matmul data = [[1,2],[3,4]] tensor = torch.FloatTensor(data) #32it variable = Variable(tensor,requires_grad=True)#requires控制是否計算梯度 t_out = torch.mean(tensor*tensor) v_out = torch.mean(variable*variable) print(t_out) print(v_out) v_out.backward() #v_out = 1/4*sum(var*var) #d(v_out)/d(var) = 1/4*2variable = variable/2 print(variable.grad)
7.5 Variable containing: 7.5000 [torch.FloatTensor of size 1] Variable containing: 0.5000 1.0000 1.5000 2.0000 [torch.FloatTensor of size 2x2]
這里輸出variable.grad的意思是指輸出v_out對variable求導所得的梯度值。
如果你想看variable里有上面數據,並將它們轉換成numpy的形式
print(variable.data) print(variable.data.numpy())
[torch.FloatTensor of size 2x2] 1 2 3 4 [torch.FloatTensor of size 2x2] [[ 1. 2.] [ 3. 4.]]
