https://pytorch.org/tutorials/beginner/introyt/modelsyt_tutorial.html

学习pytorch的使用,如何构建一个最简单的网络模型

Tips: 一般把网络中具有可学习参数的层(如全连接层、卷积层等)放在构造函数init()中; ReLU、dropout、BatchNormanation层 在forward方法里面可以使用nn.functional来代替。forward方法是必须要重写的,它是实现模型的功能,实现各个层之间的连接关系的核心。
只要在nn.Module的子类中定义了forward函数,backward函数就会自动被实现(利用autograd)。

torch.nn.Module

As a simple example, here’s a very simple model with two linear layers and an activation function. We’ll create an instance of it and ask it to report on its parameters:

  1. import torch
  2. class TinyModel(torch.nn.Module):
  3. def __init__(self):
  4. super(TinyModel,self).__init__()
  5. self.linear1 = torch.nn.Linear(100,200)
  6. self.activation = torch.nn.ReLU()
  7. self.linear2 = torch.nn.Linear(200,10)
  8. self.softmax = torch.nn.Softmax()
  9. def forward(self,x):
  10. x = self.linear1(x)
  11. x = self.activation(x)
  12. x = self.linear2(x)
  13. x = self.softmax(x)
  14. return x
  15. tinymodel = TinyModel()
  16. print("The model:")
  17. print(tinymodel)

This shows the fundamental structure of a Pytorch model: there is an__init__() method that defines the layers and other components of a model, and a forward() method where the computation gets done.

Convolutional Layers

卷积层用于处理具有高度空间相关性的数据,它们在计算机视觉中非常常用

Convolutional layers are built to handle data with a high degree of spatial correlation. They are very commonly used in computer vision, where they detect close groupings of features which the compose into high-level features. They pop up in other contexts too - for example , in NLP applications, where the a word’s immediate context can affect the meaning of a sentece.

  1. import torch.functional as F
  2. from torch import nn
  3. class LeNet(nn.Module):
  4. def __init__(self):
  5. super(LeNet,self).__init__()
  6. self.conv1 = nn.Conv2d(1,6,5)
  7. self.conv2 = nn.Conv2d(6,16,3)
  8. # an affine operation: y = Wx + b
  9. self.fc1 = nn.Linear(16*6*6,120)
  10. self.fc2 = nn.Linear(120,84)
  11. self.fc3 = nn.Linear(84,10)
  12. def forward(self,x):
  13. # Max pooling over a (2,2) window
  14. x = F.max_pool2d(F.relu(self.conv1(x)),(2,2))
  15. x = F.max_pool2d(F.relu(self.conv2(x)),2)
  16. x = x.view(-1,self.num_flat_features(x))
  17. x = F.relu(self.fc1(x))
  18. x = F.relu(self.fc2(x))
  19. x = self.fc3(x)
  20. return x
  21. def num_flat_features(self,x):
  22. size = x.size()[1:] # all dimensions except the batch dimension
  23. num_features = 1
  24. for s in size:
  25. num_features *=s
  26. return num_features

input image channel (black&white), 6 output channels, 5x5 square convolution

卷积层的第一个输入为input_channels, 我们想要学到的特征数量取决于卷积层的数量,也就是output_channels

  • LeNet5 is meant to take in a 1x32x32 black&white image. The first argument to a convolution layer’s constructor is the number of input channels. Here, it is 1. If we were building this model to look at 3-color channels, it would be 3.
  • A convolutional layer is like a window that scans over the image, looking for a pattern it recognizes. These patterns are called features, and one of the parameters of a convolution layer is the number of features we would like it to learn. This is the second argument to the constructor is the number of output features.
  • The third argument is the window or kernel size

The output of a convolutional layer is an activation map - a spatial representation of the presence of features in the input tensor.

conv1 will give us an output tensor of 6x28x28; 6 is the number of features, and 28 is the height and width of our map.

output of convolution —>ReLU activation function—> max pooling layer
We then pass the output of the convolution through a ReLU activation function( more in activation functions later), then through a max pooling layer.

最大池化 —> 低分辨率版本的activation map
The max pooling layer takes features near each other in the activation map and groups them together. It does this by reducing the tensor, merging every 2x2 groups of cells in the output into a single cell, and assigning that cell the maximum value of the 4 cells that went into it. This gives us a lower-resolution version of the activation map, with dimensions 61414.