# 学习笔记之——基于pytorch的卷积神经网络

pytorch中文网：https://www.pytorchtutorial.com/

对于全连接神经网络，网络参数太多了。如，对于一张28*28的图片输入，第一个隐含层的单个神经元的权重数目就达28*28=784个。若多设置几层隐含层、输入图片再大一点，参数量十分庞大。

卷积层中滤波器的参数是通过学习得到的。

与神经元链接的空间大小叫神经元的感受野（receptive field）。感受野的大小即filters size（滤波器的尺寸）。而感受野的深度必须和输入输入的深度一致。输出的感受野深度等于the number of filters

CNN——参数共享、稀疏链接（局部链接）

``````import torch
import numpy as np
import torch.nn as nn

#define the model
class SimpleCNN(nn.Module):
"""docstring for SimpleCNN"""
def __init__(self):
super(SimpleCNN, self).__init__()
layer1=nn.Sequential()#Container class, We can add some basic modules in it.
self.layer1=layer1

layer2=nn.Sequential()
self.layer2=layer2

layer3=nn.Sequential()
self.layer3=layer3

layer4=nn.Sequential()
self.layer4=layer4

def forward(self,x):
conv1=self.layer1(x)
conv2=self.layer2(conv1)
conv3=self.layer3(conv2)
fc_input=conv3.view(conv3.size(0),-1)#A multi line Tensor is spliced into a row.
fc_out=self.layer4(fc_input)
return fc_out

model=SimpleCNN()
print(model)``````

run之后的结果：

``````
for param in model.named_parameters():#get the name of the layyer, and the Iterator of parameters
print(param[0])``````

https://www.pytorchtutorial.com/docs/torchvision/torchvision-models/

``````import torch
from torch import optim
import torch.nn as nn
from torchvision import datasets,transforms

torch.manual_seed(1)    # reproducible

#Hyperparameters
batch_size=50
learning_rate=1e-3
EPOCH=1

#Data preprocessing
data_tf=transforms.Compose([transforms.ToTensor(),transforms.Normalize([0.5],[0.5])])#take all of the preprocessing together
#.ToTensor():Standardization of Image
#normalization,Subtract the mean, divide by variance.

test_data=datasets.MNIST(root='./MNIST_data',train=False,transform=data_tf)

#####################################################################################################################
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()

nn.BatchNorm2d(16),
nn.ReLU(),)#inplace=True,Changing the input data

nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2,stride=2),)#32*12*12

nn.BatchNorm2d(64),
nn.ReLU(),)

nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2,stride=2),)#128*4*4

self.fc=nn.Sequential(nn.Linear(128*4*4,1024),
nn.ReLU(),
nn.Linear(1024,128),
nn.ReLU(),
nn.Linear(128,10),)

def forward(self,x):

x=self.layer1(x)
x=self.layer2(x)
x=self.layer3(x)
x=self.layer4(x)
x=x.view(x.size(0),-1)
output=self.fc(x)

return output
###########################################################################################################

#train
model=CNN()
print(model)

if torch.cuda.is_available():
model=model.cuda()

criterion=nn.CrossEntropyLoss()

for epoch in range(EPOCH):
if torch.cuda.is_available():
img=Variable(img).cuda()#Nodes with a volatile attribute of True will not be derivation. and default is False
label=Variable(label).cuda()
else:
img=Variable(img)
label=Variable(label)
output=model(img)
loss=criterion(output,label)

#backward pass
loss.backward()
#update parameters
optimizer.step()

#test
model.eval()#evaluation Pattern,
#The dropout is turned off during the test, and the parameters in the BN are also used to retain the parameters during training,
#so the test should enter the evaluation mode.``````

（参考：https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/02-intermediate/convolutional_neural_network/main.py#L35-L56）

``````import torch
import torch.nn as nn
import torchvision
#It includes the popular data set, model structure and commonly used image conversion tools.
import torchvision.transforms as transforms

#Device configuration
device=torch.device('cuda:0'if torch.cuda.is_available() else 'cpu')

#Hyper parameters
num_epochs=6
num_classes=10#number 0~9
batch_size=100
learning_rate=0.001

#MNIST dataset
test_dataset=torchvision.datasets.MNIST(root='./MNIST_data',train=False,transform=transforms.ToTensor())

#data loader or you can call it data Preprocessing
#According to batch size, it is encapsulated into Tensor.
#After that, Variable is only needed to be input into the model.

##########################################################
#define the CNN
class ConvNet(nn.Module):
def __init__(self,num_classes=10):
super(ConvNet,self).__init__()#input 1*28*28
self.layer1=nn.Sequential(
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2,stride=2)#16*14*14
)

self.layer2=nn.Sequential(
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2,stride=2)#32*7*7
)

self.fc=nn.Linear(7*7*32,num_classes)

def forward(self,x):
out=self.layer1(x)
out=self.layer2(out)
out=out.reshape(out.size(0),-1)
out=self.fc(out)

return out

model=ConvNet(num_classes).to(device)#this Sentence is see wherether CPU or GPU speed up

#loss and optimizer
criterion=nn.CrossEntropyLoss()

#traian the model
total_step=len(train_loader)#all of the train data, each itertation is the number of batch_size. the
for epoch in range(num_epochs):
images=images.to(device)
labels=labels.to(device)

#Forward pass
outputs=model(images)
loss=criterion(outputs,labels)

#backward and optimize
loss.backward()
optimizer.step()

if(i+1)%100==0:
print('Epoch[{}/{}],Step[{}/{}],Loss:{:.4f}'
.format(epoch+1,num_epochs,i+1,total_step,loss.item()))

#################################################################################
#test the model
model.eval()# eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
correct=0
total=0
images=images.to(device)
labels=labels.to(device)

outputs=model(images)
_,predicted=torch.max(outputs.data,1)#Returns the maximum value on the dimension=1.
total+=labels.size(0)
correct += (predicted == labels).sum().item()

print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))

# Save the model checkpoint
#torch.save(model.state_dict(), 'model.ckpt')``````

https://www.cnblogs.com/king-lps/p/8570021.html