多层感知机 (MLP) 网络设计与实现
import torch import torch.nn as nn
class MLP(nn.Module): def init(self): super(MLP, self).init() self.flatten = nn.Flatten() self.linear1 = nn.Linear(32323, 2048) self.sigmoid = nn.Sigmoid() self.linear2 = nn.Linear(2048, 512) self.relu = nn.ReLU() self.linear3 = nn.Linear(512, 20)
def forward(self, x):
x = self.flatten(x)
x = self.linear1(x)
print('Linear output shape: ', x.shape)
print(' Linear weight's mean: ', torch.mean(self.linear1.weight))
print(' Linear bias's mean: ', torch.mean(self.linear1.bias))
x = self.sigmoid(x)
print('Sigmoid output shape: ', x.shape)
x = self.linear2(x)
print('Linear output shape: ', x.shape)
print(' Linear weight's mean: ', torch.mean(self.linear2.weight))
print(' Linear bias's mean: ', torch.mean(self.linear2.bias))
x = self.relu(x)
print('ReLU output shape: ', x.shape)
x = self.linear3(x)
print('Linear output shape: ', x.shape)
print(' Linear weight's mean: ', torch.mean(self.linear3.weight))
print(' Linear bias's mean: ', torch.mean(self.linear3.bias))
return x
model = MLP() x = torch.randn(1, 3, 32, 32) output = model(x) print('Flatten output shape: ', output.shape)
原文地址: https://www.cveoy.top/t/topic/nYXH 著作权归作者所有。请勿转载和采集!