输出各层神经网络权重与偏置

from torch import nn
import torch.nn.functional as f
class SimpleNet(nn.Module):
    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super(SimpleNet, self).__init__()
        self.layer1 = nn.Linear(in_dim, n_hidden_1)
        self.layer2 = nn.Linear(n_hidden_1, n_hidden_2)
        self.layer3 = nn.Linear(n_hidden_2, out_dim)

    def forward(self, x):
        x = self.layer1(x)
        x = f.relu(x)
        x = self.layer2(x)
        x = f.relu(x)
        x = self.layer3(x)
        return x

model = SimpleNet(28 * 28, 300, 100, 10)

for layer in model.modules():
   if isinstance(layer, nn.Linear):
        print(layer.weight)
        print(layer.bias)

你可能感兴趣的:(机器学,人工智能,pytorch)