【问题标题】:'Net' object has no attribute 'parameters'“网络”对象没有属性“参数”
【发布时间】:2020-08-25 10:35:38
【问题描述】:

我对机器学习还很陌生。我从 youtube 教程中学会了编写此代码,但我不断收到此错误

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/aniket/Desktop/DeepLearning/PythonLearningPyCharm/CatVsDogs.py", line 109, in <module>
    optimizer = optim.Adam(net.parameters(), lr=0.001) # tweaks the weights from what I understand
AttributeError: 'Net' object has no attribute 'parameters'

这是网络类

class Net():
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1,32,5)
        self.conv2 = nn.Conv2d(32,64,5)
        self.conv3 = nn.Conv2d(64,128,5)
        self.to_linear = None
        x = torch.randn(50,50).view(-1,1,50,50)
        self.Conv2d_Linear_Link(x)
        self.fc1 = nn.Linear(self.to_linear, 512)
        self.fc2 = nn.Linear(512, 2)

    def Conv2d_Linear_Link(self , x):
        x = F.max_pool2d(F.relu(self.conv1(x)),(2,2))
        x = F.max_pool2d(F.relu(self.conv2(x)),(2,2))
        x = F.max_pool2d(F.relu(self.conv3(x)),(2,2))

        if self.to_linear is None :
            self.to_linear = x[0].shape[0]*x[0].shape[1]*x[0].shape[2]
        return x

    def forward(self, x):
        x = self.Conv2d_Linear_Link(x)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.softmax(x, dim=1)

这就是函数训练

def train():
    for epoch in range(epochs):
        for i in tqdm(range(0,len(X_train), batch)):
            batch_x = train_X[i:i + batch].view(-1, 1, 50, 50)
            batch_y = train_y[i:i + batch]
            net.zero_grad() # i don't understand why we do this but we do we don't want the probabilites adding up
            output = net(batch_x)
            loss = loss_function(output, batch_y)
            loss.backward()
            optimizer.step()
        print(loss)

以及优化器和损失函数和数据

optimizer = optim.Adam(net.parameters(), lr=0.001) # tweaks the weights from what I understand
loss_function = nn.MSELoss() # gives the loss

【问题讨论】:

  • 请添加与您使用的框架相关的标签 - 是 Tensorflow、Pytorch 还是其他?
  • @desertnaut 完成,这是 Pytorch

标签: deep-learning neural-network pytorch conv-neural-network


【解决方案1】:

你没有继承nn.Module。它应该是这样的:

class Net(nn.Module):
    def __init__(self):
        super().__init__()

这允许您的网络继承nn.Module 类的所有属性,例如parameters 属性。

【讨论】:

    【解决方案2】:

    你可能有拼写问题,你应该看看 Net 有哪些参数。

    【讨论】:

    • 我有一些拼写错误我修复了它们我仍然得到同样的错误。由于我使用的是 Pytorch 并且对 python 还比较陌生,所以我不知道您所说的“查看 Net 哪些参数”是什么意思。
    猜你喜欢
    • 2020-10-02
    • 2018-01-27
    • 1970-01-01
    • 2019-03-11
    • 2023-03-17
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多