源码:
- Convolutional Neural Network
- Deep Residual Network
- Recurrent Neural Network
- Bidirectional Recurrent Neural Network
- Language Model (RNN-LM)
程序流过程分析:
1.引用模块 import ...
2.torch gpu驱动配置 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
3.超参设置 epoch、batch_size、learning_rate ...
4.数据预处理
5.数据加载
6.模型搭建
7.模型初始化
8.损失函数和优化器
9.模型训练
10.模型测试
11.模型保存
一、卷积神经网络模型 (手写数字识别能到98.95 %)
1.引用模块 import ...
2.torch gpu驱动配置 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
3.超参设置 epoch、batch_size、learning_rate ...
4.数据预处理
5.数据加载
6.模型搭建
class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super(ConvNet, self).__init__()
#第一层卷积
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2), #2D卷积
nn.BatchNorm2d(16), #归一化
nn.ReLU(), #激活函数
nn.MaxPool2d(kernel_size=2, stride=2)) #池化
#第二层卷积
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2), #2D卷积
nn.BatchNorm2d(32), #归一化
nn.ReLU(), #激活函数
nn.MaxPool2d(kernel_size=2, stride=2)) #池化
#第三层,全连接层
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
7.模型初始化
model = ConvNet(num_classes).to(device)
8.损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
9.模型训练
10.模型测试
11.模型保存
二、深度残差网络模型 残差网络(图片CIFAR10分类88.33 %)
1.引用模块 import ...
2.torch gpu驱动配置 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
3.超参设置 epoch、batch_size、learning_rate ...
4.数据预处理
5.数据加载
6.模型搭建
# 3x3 convolution
def conv3x3(in_channels, out_channels, stride=1):
return nn.Conv2d(in_channels, out_channels, kernel_size=3,
stride=stride, padding=1, bias=False)
# Residual block
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
super(ResidualBlock, self).__init__()
self.conv1 = conv3x3(in_channels, out_channels, stride)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(out_channels, out_channels)
self.bn2 = nn.BatchNorm2d(out_channels)
self.downsample = downsample
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=10):
super(ResNet, self).__init__()
self.in_channels = 16
self.conv = conv3x3(3, 16)
self.bn = nn.BatchNorm2d(16)
self.relu = nn.ReLU(inplace=True)
self.layer1 = self.make_layer(block, 16, layers[0])
self.layer2 = self.make_layer(block, 32, layers[1], 2)
self.layer3 = self.make_layer(block, 64, layers[2], 2)
self.avg_pool = nn.AvgPool2d(8)
self.fc = nn.Linear(64, num_classes)
def make_layer(self, block, out_channels, blocks, stride=1):
downsample = None
if (stride != 1) or (self.in_channels != out_channels):
downsample = nn.Sequential(
conv3x3(self.in_channels, out_channels, stride=stride),
nn.BatchNorm2d(out_channels))
layers = []
layers.append(block(self.in_channels, out_channels, stride, downsample))
self.in_channels = out_channels
for i in range(1, blocks):
layers.append(block(out_channels, out_channels))
return nn.Sequential(*layers)
def forward(self, x):
out = self.conv(x)
out = self.bn(out)
out = self.relu(out)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.avg_pool(out)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
7.模型初始化
model = ResNet(ResidualBlock, [2, 2, 2]).to(device)
8.损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
9.模型训练
10.模型测试
11.模型保存
三、循环神经网络模型 (手写数字识别97.77 %)
1.引用模块 import ...
2.torch gpu驱动配置 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
3.超参设置 epoch、batch_size、learning_rate ...
4.数据预处理
5.数据加载
6.模型搭建
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
# Set initial hidden and cell states
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
# Forward propagate LSTM
out, _ = self.lstm(x, (h0, c0)) # out: tensor of shape (batch_size, seq_length, hidden_size)
# Decode the hidden state of the last time step
out = self.fc(out[:, -1, :])
return out
7.模型初始化
model = RNN(input_size, hidden_size, num_layers, num_classes).to(device)
8.损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
9.模型训练
10.模型测试
11.模型保存
四、双向循环神经网络模型 (手写数字识别97.45 %)
1.引用模块 import ...
2.torch gpu驱动配置 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
3.超参设置 epoch、batch_size、learning_rate ...
4.数据预处理
5.数据加载
6.模型搭建
class BiRNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(BiRNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True)
self.fc = nn.Linear(hidden_size * 2, num_classes) # 2 for bidirection
def forward(self, x):
# Set initial states
h0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(device) # 2 for bidirection
c0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size).to(device)
# Forward propagate LSTM
out, _ = self.lstm(x, (h0, c0)) # out: tensor of shape (batch_size, seq_length, hidden_size*2)
# Decode the hidden state of the last time step
out = self.fc(out[:, -1, :])
return out
7.模型初始化
model = BiRNN(input_size, hidden_size, num_layers, num_classes).to(device)
8.损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
9.模型训练
10.模型测试
11.模型保存
五、RNN语言模型
1.引用模块 import ...
2.torch gpu驱动配置 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
3.超参设置 epoch、batch_size、learning_rate ...
4.数据预处理
5.数据加载
6.模型搭建
class RNNLM(nn.Module):
def __init__(self, vocab_size, embed_size, hidden_size, num_layers):
super(RNNLM, self).__init__()
self.embed = nn.Embedding(vocab_size, embed_size)
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True)
self.linear = nn.Linear(hidden_size, vocab_size)
def forward(self, x, h):
# Embed word ids to vectors
x = self.embed(x)
# Forward propagate LSTM
out, (h, c) = self.lstm(x, h)
# Reshape output to (batch_size*sequence_length, hidden_size)
out = out.reshape(out.size(0)*out.size(1), out.size(2))
# Decode hidden states of all time steps
out = self.linear(out)
return out, (h, c)
7.模型初始化
model = RNNLM(vocab_size, embed_size, hidden_size, num_layers).to(device)
8.损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
9.模型训练
10.模型测试
11.模型保存