访问由多层感知器构建的神经网络模型的权重的火炬错误

Posted

技术标签:

【中文标题】访问由多层感知器构建的神经网络模型的权重的火炬错误【英文标题】:torch error of accessing weights of a neural network model built by multiple layer perceptrons 【发布时间】:2021-03-04 21:57:47 【问题描述】:

我正在尝试访问由torch 在databricks 上构建的神经网络模型的权重。

代码:

 import torch
 import torch.nn as nn
 import numpy 

 class Feedforward(nn.Module):

     def __init__(self, input_size, hidden_size):
         super(Feedforward, self).__init__()
         self.input_size = input_size
         self.hidden_size  = hidden_size
         self.fc1 = nn.Linear(self.input_size, self.hidden_size)
         self.relu = nn.ReLU()
         self.fc2 = nn.Linear(self.hidden_size, 1)
         self.sigmoid = nn.Sigmoid()

     def forward(self, x):
         hidden = self.fc1(x)
         relu = self.relu(hidden)
         output = self.fc2(relu)
         output = self.sigmoid(output)
         return output

from sklearn.datasets import make_blobs
def blob_label(y, label, loc): # assign labels
    target = numpy.copy(y)
    for l in loc:
        target[y == l] = label
    return target

x_train, y_train = make_blobs(n_samples=40, n_features=2, cluster_std=1.5, shuffle=True)
x_train = torch.FloatTensor(x_train)
y_train = torch.FloatTensor(blob_label(y_train, 0, [0]))
y_train = torch.FloatTensor(blob_label(y_train, 1, [1,2,3]))

x_test, y_test = make_blobs(n_samples=10, n_features=2, cluster_std=1.5, shuffle=True)
x_test = torch.FloatTensor(x_test)
y_test = torch.FloatTensor(blob_label(y_test, 0, [0]))
y_test = torch.FloatTensor(blob_label(y_test, 1, [1,2,3]))

model = Feedforward(2, 10)
criterion = torch.nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)

y_pred = model(x_test)
before_train = criterion(y_pred.squeeze(), y_test)
print('Test loss before training' , before_train.item())

model.train()
epoch = 20
for epoch in range(epoch):
    optimizer.zero_grad()
    # Forward pass
    y_pred = model(x_train)
    # Compute Loss
    loss = criterion(y_pred.squeeze(), y_train)

print('Epoch : train loss: '.format(epoch, loss.item()))
# Backward pass
loss.backward()
optimizer.step()

model.eval()
y_pred = model(x_test)
after_train = criterion(y_pred.squeeze(), y_test) 
print('Test loss after Training' , after_train.item())

代码运行良好。但是,当我尝试访问模型的权重时,我得到了错误:

 model.weight # ModuleAttributeError: 'Feedforward' object has no attribute 'weight'

但是,如果我尝试过

model.fc1.weight

效果很好..

如何获取由多层感知器构建的模型的权重?

此帖Can't init the weights of my neural network PyTorch 对我不起作用。

谢谢

【问题讨论】:

【参考方案1】:

因为有属性的是nn.Linear(),而不是Feedforward,试试改

class Feedforward(nn.Module):

     def __init__(self, input_size, hidden_size):
         super(Feedforward, self).__init__()
         self.input_size = input_size
         self.hidden_size  = hidden_size
         self.fc1 = nn.Linear(self.input_size, self.hidden_size)
         self.relu = nn.ReLU()
         self.fc2 = nn.Linear(self.hidden_size, 1)
         self.sigmoid = nn.Sigmoid()

class Feedforward(nn.Module):

     def __init__(self, input_size, hidden_size):
         super(nn.Linear, self).__init__()
         self.input_size = input_size
         self.hidden_size  = hidden_size
         self.fc1 = nn.Linear(self.input_size, self.hidden_size)
         self.relu = nn.ReLU()
         self.fc2 = nn.Linear(self.hidden_size, 1)
         self.sigmoid = nn.Sigmoid()

【讨论】:

您好,我已经尝试了您的建议。我收到错误“TypeError: super(type, obj): obj must be an instance or subtype of type”。谢谢

以上是关于访问由多层感知器构建的神经网络模型的权重的火炬错误的主要内容,如果未能解决你的问题,请参考以下文章

BP神经网络和感知器有啥区别?

翻译: 4.多层感知器 pytorch

使用多层感知器(MLP)对图像及其性能进行分类

多层感知器对 mnist 数据集进行分类

halcon多层感知器MLP的使用

神经网络入门回顾(感知器多层感知器)