import torch.nn as nn

class MLP(nn.Module): def init(self, input_size, hidden_size_1, hidden_size_2, output_size): super(MLP, self).init() self.flatten = nn.Flatten() self.fc1 = nn.Linear(input_size, hidden_size_1) self.sigmoid = nn.Sigmoid() self.fc2 = nn.Linear(hidden_size_1, hidden_size_2) self.relu = nn.ReLU() self.fc3 = nn.Linear(hidden_size_2, output_size)

def forward(self, x):
    x = self.flatten(x)
    x = self.fc1(x)
    print("Linear output shape: \t", x.shape)
    print("\t Linear weight's mean: \t", x.mean().item())
    print("\t Linear bias's mean: \t", self.fc1.bias.mean().item())
    x = self.sigmoid(x)
    print("Sigmoid output shape: \t", x.shape)
    x = self.fc2(x)
    print("Linear output shape: \t", x.shape)
    print("\t Linear weight's mean: \t", x.mean().item())
    print("\t Linear bias's mean: \t", self.fc2.bias.mean().item())
    x = self.relu(x)
    print("ReLU output shape: \t", x.shape)
    x = self.fc3(x)
    print("Linear output shape: \t", x.shape)
    print("\t Linear weight's mean: \t", x.mean().item())
    print("\t Linear bias's mean: \t", self.fc3.bias.mean().item())
    return x

model = MLP(input_size=32323, hidden_size_1=2048, hidden_size_2=512, output_size=20) print(model

1请设计一个多层感知机mlp网络。我希望看到的的输出结果:Flatten output shape 	 torchSize1 1024Linear output shape 	 torchSize1 2048	 Linear weights mean 	 tensor08631	 Linear biass mean 	 tensor0Sigmoid output shape 	 torchSize

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

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