pytorch 建立前向传播网络的3种方法、其中包含有卷积层、激活层、池化层、全连接层

利用pytorch来构建网络模型,常用的有如下三种方式

前向传播网络具有如下结构:

卷积层--》Relu层--》池化层--》全连接层--》Relu层

对各Conv2d和Linear的解释如下

Conv2d的解释如下
"""
Conv2d(in_channels, out_channels, kernel_size, stride=1,
        padding=0, dilation=1, groups=1, bias=True)
        
in_channels(int) – 输入信号的通道数
out_channels(int) – 卷积产生的通道数
kerner_size(int or tuple) - 卷积核的大小
stride(int or tuple,optional) - 卷积步长,即要将输入扩大的倍数。
padding(int or tuple, optional) - 输入的每一条边补充0的层数,高宽都增加2*padding
"""
Linear函数的解释如下
"""
Linear(in_features, out_features, bias=True)
in_features: size of each input sample,一般输入是[B,*,in_features]
out_features: size of each output sample,经过Linear输出的tensor是[B,*,out_features]
"""

1.建立模型方法

from torch.nn import *
class Network(Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv = Conv2d(3, 16, 3, 1, 1)
        self.dense =Linear(16 * 3, 2)
        self.pool=MaxPool2d(kernel_size=2)
        self.relu=ReLU(inplace=True)#inplace为True,将会改变输入的数据 ,否则不会改变原输入,只会产生新的输出

    #前向传播方法
    def forward(self, x):
        x=self.conv(x)
        x=  self.relu(x)
        x = MaxPool2d(x, 2)
        x = x.view(x.size(0), -1)#设置成为[B,-1]格式
        x=  self.dense(x)
        x = self.relu(x)
        return x
model = Network()
print(model)

模型各参数如下

Network(
  (conv): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (dense): Linear(in_features=48, out_features=2, bias=True)
  (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (relu): ReLU(inplace)
)

2.建立模型方法,通过torch.nn.Sequential建立模型

import torch
class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv = torch.nn.Sequential(
            torch.nn.Conv2d(3, 16, 3, 1, 1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2)
        )
        self.dense = torch.nn.Sequential(
            torch.nn.Linear(16 * 3, 2),
            torch.nn.ReLU()
        )

    def forward(self, x):
        x = self.conv(x)
        x = x.view(x.size(0), -1)
        x = self.dense(x)
        return x
model = Network()
print(model)

模型各参数如下

Network(
  (conv): Sequential(
    (0): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (dense): Sequential(
    (0): Linear(in_features=48, out_features=2, bias=True)
    (1): ReLU()
  )
)

3.建立模型方法,通过torch.nn.Sequential的方法add_module添加操作

import torch
class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.network=torch.nn.Sequential()
        self.network.add_module("conv_{}".format(1),torch.nn.Conv2d(3, 16, 3, 1, 1))
        self.network.add_module("relu_{}".format(1),torch.nn.ReLU())
        self.network.add_module("pool_{}".format(1),torch.nn.MaxPool2d(2))
        self.network.add_module("dense_{}".format(1),torch.nn.Linear(16 * 3, 2))
        self.network.add_module("relu_{}".format(2),torch.nn.ReLU())

    def forward(self, x):
        x = self.network(x)
        return x
model = Network()
print(model)

模型各参数如下

Network(
  (network): Sequential(
    (conv_1): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (relu_1): ReLU()
    (pool_1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (dense_1): Linear(in_features=48, out_features=2, bias=True)
    (relu_2): ReLU()
  )
)