利用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() ) )