pytorch入门学习

来源:https://www.pytorchtutorial.com/pytorch-note1-what-is-pytorch/

参数

   pytorch中有两种变量类型,一种是Tensor,一种是Variable。
Tensor:就像ndarray一样,一维Tensor叫Vector,二维Tensor叫Matrix,三维及以上称为Tensor。
Variable:是Tensor的一个wrapper,不仅保存了值,而且保存了这个值的creator,需要BP的网络都是Variable参与运算

1
2
3
4
import torch
x = torch.Tensor(2,3,4)
print x
print x.size()

输出的结果分别是

1
2
3
4
5
6
7
8
tensor([[[0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000]],

[[0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000]]])
(2, 3, 4)

1
2
3
4
5
6
a = torch.rand(2,3,4)
b = torch.rand(2,3,4)
_=torch.add(a,b, out=x)
print a
print b
print x

两者相加,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
tensor([[[0.7871, 0.1009, 0.8869, 0.1064],
[0.1737, 0.2351, 0.1654, 0.0021],
[0.1983, 0.6648, 0.1687, 0.9858]],

[[0.5143, 0.0651, 0.4567, 0.8626],
[0.8941, 0.2482, 0.0877, 0.9383],
[0.1543, 0.9448, 0.9151, 0.0840]]])
tensor([[[0.5534, 0.0053, 0.2048, 0.8548],
[0.5115, 0.7653, 0.6341, 0.6142],
[0.2852, 0.4144, 0.8994, 0.2073]],

[[0.1986, 0.2728, 0.6145, 0.6404],
[0.8327, 0.5578, 0.2717, 0.2645],
[0.8938, 0.1658, 0.9930, 0.2194]]])
tensor([[[1.3405, 0.1062, 1.0916, 0.9611],
[0.6852, 1.0004, 0.7995, 0.6163],
[0.4835, 1.0792, 1.0681, 1.1931]],

[[0.7129, 0.3379, 1.0711, 1.5030],
[1.7267, 0.8060, 0.3594, 1.2028],
[1.0481, 1.1106, 1.9082, 0.3034]]])

自动求导

pytorch的自动求导工具包在torch.autograd中

1
2
3
4
5
6
7
from torch.autograd import Variable
x = torch.rand(5)
x = Variable(x,requires_grad = True)
y = x*2
grads = torch.FloatTensor([1,2,3,4,5])
y.backward(grads)
print(x.grad)

输出为:

1
tensor([0.6657, 0.3089, 0.6539, 0.0688, 0.5952], requires_grad=True)

神经网络

使用torch.nn包中的工具来构建神经网络 需要以下几步:
1.定义神经网络的权重,搭建网络结构
2.遍历整个数据集进行训练
3.将数据输入神经网络
4.计算loss
5.计算网络权重的梯度
6.更新网络权重:weight = weight + learning_rate*gradient
网络整体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):#需要继承这个类
def __init__(self):
super(Net, self).__init__()
#建立了两个卷积层,self.conv1, self.conv2,注意,这些层都是不包含激活函数的
self.conv1 = nn.Conv2d(1, 6, 5) # 1 input image channel, 6 output channels, 5x5 square convolution kernel
self.conv2 = nn.Conv2d(6, 16, 5)
#三个全连接层
self.fc1 = nn.Linear(16*5*5, 120) # an affine operation: y = Wx + b
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)

def forward(self, x): #注意,2D卷积层的输入data维数是 batchsize*channel*height*width
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2)) # Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv2(x)), 2) # If the size is a square you can only specify a single number
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x

def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features

net = Net()

# create your optimizer
optimizer = optim.SGD(net.parameters(), lr = 0.01)

# in your training loop:
for i in range(num_iteations):
optimizer.zero_grad() # zero the gradient buffers,如果不归0的话,gradients会累加

output = net(input) # 这里就体现出来动态建图了,你还可以传入其他的参数来改变网络的结构

loss = criterion(output, target)
loss.backward() # 得到grad,i.e.给Variable.grad赋值
optimizer.step() # Does the update,i.e. Variable.data -= learning_rate*Variable.grad

0%