获得一层的重量

2024-10-01 17:37:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用PyTorch处理MNIST数据集,在访问代码中生成的权重和偏差时遇到了问题

这是我的密码

from torch import nn
import torch.nn.functional as F
    
class Neural(nn.Module):
  def __init__(self):
      super().__init__()
      
      self.hidden1 = nn.Linear(784,128)
      self.hidden2 = nn.Linear(128,64)
      self.output  = nn.Linear(64,10)
      
  def forward(self,x):
      x=F.relu(self.hidden1(x))
      x=F.relu(self.hidden2(x))
      x=F.softmax(self.output(x))
      return x
    
model= Neural()

当我使用

print(model.fc1.weight)
print(model.fc1.bias)

这就是我得到的错误

AttributeError                            Traceback (most recent call last)
<ipython-input-58-e92de631c798> in <module>()
----> 1 model.fc1.weight
      2 print(model.fc1.bias)

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
    530                 return modules[name]
    531         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 532             type(self).__name__, name))
    533 
    534     def __setattr__(self, name, value):

AttributeError: 'Neural' object has no attribute 'fc1'

Tags: nameimportselfmodelinitdefnntorch

热门问题