服装设计网站哪个好近期国际热点大事件
模型搭建与复现
- 神经网络的模版
- 神经网络中各种常见的结构
- 组建可复用的网络模块
神经网络模版
需要有以下元素组成
class Model(nn.Module):def __init__(self):super().__init__(self)self.conv1 = nn.Conv2d(1, 20, 5)self.conv2 = nn.Conv2d(20, 20, 5)def forwart(self, x):x = F.relu((self.conv1(x)))return F.relu(self.conv2(x))
模型搭建
import torch
import torch.nn as nnm = nn.Linear(2, 3)
input = torch.randn(5, 2)
print(input)
output = m(input)
print(output)
print(output.size())
图片仅供学习用,如有侵权请联系我删除
激活函数
- Sigmoid
- ReLU
- Softmax
- 随机失活Dropout
综合案例
复现LeNet-5
import torch.nn.functional as Fclass Model(nn.Module):def __init__(self):super().__init__(self)self.conv1 = nn.Conv2d(1, 6, kernel_size=5)self.pool1 = nn.AvgPool2d(kernel_size=2, stride=2)self.conv2 = nn.Conv2d(6, 16, kernel_size=5)self.pool2 = nn.AvgPool2d(kernel_size=2, stride=2)self.conv3 = nn.Conv2d(16, 120, kernel_size=5)self.linear1 = nn.Linear(120, 84)self.linear2 = nn.Linear(84, 10)def forward(self, x):x = self.conv1(x)x = F.tanh(x)x = self.pool1(x)x = self.conv2(x)x = F.tanh(x)x = self.pool2(x)x = self.conv3(x)x = F.tanh(x)x = x.view(x.size(0), -1) # 将batch展平# 接下来全连接x = self.linear1(x)x = F.tanh(x)x = self.linear2(x)return x