import torch
import torch.nn as nn
import torch.nn.functional as F
classNet(nn.Module):def__init__(self):super(Net, self).__init__()#Conv2d是在输入信号(由几个平面图像构成)上应用2维卷积# 1 input image channel, 6 output channels, 3x3 square convolution kernel
self.conv1 = nn.Conv2d(1,6,3)
self.conv2 = nn.Conv2d(6,16,3)# an affine operation: y = Wx + b#affine仿射的
self.fc1 = nn.Linear(16*6*6,120)#16是conv2的输出通道数,6*6是图像维度#(32*32的原图,经conv1卷后是6*30*30,经池化后是6*15*15,经conv2卷后是16*13*13,经池化后是16*6*6)#经过网络层后的维度数计算方式都可以看网络的类的文档来查到
self.fc2 = nn.Linear(120,84)
self.fc3 = nn.Linear(84,10)defforward(self, x):# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)),(2,2))# If the size is a square, you can specify with a single number
x = F.max_pool2d(F.relu(self.conv2(x)),2)
x = x.view(-1, self.num_flat_features(x))#将x转化为元素不变,尺寸为[-1,self.num_flat_features(x)]的Tensor#-1的维度具体是多少,是根据另一维度计算出来的#由于另一维度是x全部特征的长度,所以这一步就是把x从三维张量拉成一维向量
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)return x
defnum_flat_features(self, x):#计算得到x的特征总数(就是把各维度乘起来)
size = x.size()[1:]# all dimensions except the batch dimension
num_features =1for s in size:
num_features *= s
return num_features
net = Net()print(net)
import torch.optim as optim
# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)# in your training loop:
optimizer.zero_grad()#原因见前
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()# Does the update
这个神经网络和第3部分神经网络里的模型相似,只是将数据维度做了修改。 这里的数据特征尺寸在网络层之间的变化是
3
∗
32
∗
32
→
(
c
o
n
v
1
)
6
∗
28
∗
28
→
(
p
o
o
l
)
6
∗
14
∗
14
→
(
c
o
n
v
2
)
16
∗
10
∗
10
→
(
p
o
o
l
)
16
∗
5
∗
5
→
(
f
c
1
)
120
→
(
f
c
2
)
84
→
(
f
c
3
)
10
3*32*32\xrightarrow{(conv1)}6*28*28\xrightarrow{(pool)}6*14*14\xrightarrow{(conv2)}16*10*10\xrightarrow{(pool)}16*5*5\xrightarrow{(fc1)}120\xrightarrow{(fc2)}84\xrightarrow{(fc3)}10
3∗32∗32(conv1)6∗28∗28(pool)6∗14∗14(conv2)16∗10∗10(pool)16∗5∗5(fc1)120(fc2)84(fc3)10
import torch.nn as nn
import torch.nn.functional as F
classNet(nn.Module):def__init__(self):super().__init__()
self.conv1 = nn.Conv2d(3,6,5)
self.pool = nn.MaxPool2d(2,2)
self.conv2 = nn.Conv2d(6,16,5)
self.fc1 = nn.Linear(16*5*5,120)
self.fc2 = nn.Linear(120,84)
self.fc3 = nn.Linear(84,10)defforward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1,16*5*5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)return x
net = Net()
for epoch inrange(2):# loop over the dataset multiple times
running_loss =0.0for i, data inenumerate(trainloader,0):# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()# print statistics
running_loss += loss.item()#loss.item()文档:https://pytorch.org/docs/stable/tensors.html?highlight=item#torch.Tensor.item#Returns the value of this tensor as a standard Python number.if i %2000==1999:# print every 2000 mini-batchesprint('[%d, %5d] loss: %.3f'%(epoch +1, i +1, running_loss /2000))
running_loss =0.0print('Finished Training')
correct =0
total =0with torch.no_grad():for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data,1)
total += labels.size(0)
correct +=(predicted == labels).sum().item()print('Accuracy of the network on the 10000 test images: %d %%'%(100* correct / total))