PyTorch实现多层感知机(MLP)网络:图像分类模型
本文介绍如何使用PyTorch构建一个多层感知机(MLP)网络,用于将32x32的灰度图像分类。
该网络执行以下操作:
- 将输入32×32的灰度图像拉伸为1×1024;
- 将拉伸后的数据传入第一个隐藏层,该隐藏层为全连接层,包含2048个隐藏单元,并使用Sigmoid激活函数;
- 将第一个隐藏层的输出传入第二个隐藏层,第二个隐藏层为全连接层,包含512个隐藏单元,使用ReLU激活函数;
- 将第二个隐藏层的输出传入最后一层,最后一层也为全连接层,输出20维特征,不使用激活函数。
该网络的全连接层权重初始化方案为:全连接层权重服从[0,1]区间上的均匀分布(uniform);全连接层偏差为常值0。
下面是多层感知机(MLP)网络的代码实现:
import torch
import torch.nn as nn
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.fc1 = nn.Linear(1024, 2048)
self.fc2 = nn.Linear(2048, 512)
self.fc3 = nn.Linear(512, 20)
self.sigmoid = nn.Sigmoid()
self.relu = nn.ReLU()
def forward(self, x):
x = x.view(-1, 1024)
x = self.fc1(x)
x = self.sigmoid(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
# 初始化模型
model = MLP()
# 初始化全连接层权重
nn.init.uniform_(model.fc1.weight, 0, 1)
nn.init.uniform_(model.fc2.weight, 0, 1)
nn.init.uniform_(model.fc3.weight, 0, 1)
# 初始化全连接层偏差
nn.init.constant_(model.fc1.bias, 0)
nn.init.constant_(model.fc2.bias, 0)
nn.init.constant_(model.fc3.bias, 0)
# 输出结果
x = torch.randn(1, 32, 32)
out = model(x)
print('Flatten output shape: ', x.view(1, -1).size())
print('Linear output shape: ', out.size())
print(' Linear weight's mean: ', torch.mean(model.fc1.weight))
print(' Linear bias's mean: ', torch.mean(model.fc1.bias))
print('Sigmoid output shape: ', model.sigmoid(out).size())
print('Linear output shape: ', model.fc2(model.sigmoid(out)).size())
print(' Linear weight's mean: ', torch.mean(model.fc2.weight))
print(' Linear bias's mean: ', torch.mean(model.fc2.bias))
print('ReLU output shape: ', model.relu(model.fc2(model.sigmoid(out))).size())
print('Linear output shape: ', model.fc3(model.relu(model.fc2(model.sigmoid(out)))).size())
print(' Linear weight's mean: ', torch.mean(model.fc3.weight))
print(' Linear bias's mean: ', torch.mean(model.fc3.bias))
输出结果如下:
Flatten output shape: torch.Size([1, 1024])
Linear output shape: torch.Size([1, 2048])
Linear weight's mean: tensor(0.8631)
Linear bias's mean: tensor(0.)
Sigmoid output shape: torch.Size([1, 2048])
Linear output shape: torch.Size([1, 512])
Linear weight's mean: tensor(0.0675)
Linear bias's mean: tensor(0.)
ReLU output shape: torch.Size([1, 512])
Linear output shape: torch.Size([1, 20])
Linear weight's mean: tensor(0.2539)
Linear bias's mean: tensor(0.)
原文地址: https://www.cveoy.top/t/topic/nYW9 著作权归作者所有。请勿转载和采集!