Getting the weight of a Layer
I've been working on the MNIST data set using PyTorch and I am having trouble in accessing the weights and biases that is generated in my code.
This is my code
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()

and to access the weight when i use
print(model.fc1.weight)
print(model.fc1.bias)

this is the error i get
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-58-e92de631c798&gt; in &lt;module&gt;()
----&gt; 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(&quot;'{}' object has no attribute '{}'&quot;.format(
--&gt; 532             type(self).__name__, name))
    533 
    534     def __setattr__(self, name, value):

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


You should access weights of a layer via its name, so it will be
print (model.hidden1.weight, model.hidden1.bias)

