import torch import torch.nn as nn

class MLP(nn.Module): def init(self): super(MLP, self).init() self.linear1 = nn.Linear(32 * 32, 2048) self.sigmoid = nn.Sigmoid() self.linear2 = nn.Linear(2048, 512) self.relu = nn.ReLU() self.linear3 = nn.Linear(512, 20)

    nn.init.uniform_(self.linear1.weight, 0, 1)
    nn.init.uniform_(self.linear2.weight, 0, 1)
    nn.init.uniform_(self.linear3.weight, 0, 1)
    nn.init.constant_(self.linear1.bias, 0)
    nn.init.constant_(self.linear2.bias, 0)
    nn.init.constant_(self.linear3.bias, 0)

def forward(self, x):
    x = x.view(x.size(0), -1)
    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, 1, 32, 32) out = model(x) print('Flatten output shape: ', x.view(x.size(0), -1).shape)

PyTorch 多层感知机 (MLP) 网络设计与实现

原文地址: https://www.cveoy.top/t/topic/nYXt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录