Search code examples
parametersdeep-learningpytorchtorch

No Parameters in Net class even after inheriting from nn.Module


For some reason, my Class Net has no parameters, even though i have imported everything correctly., its still showing that net has no parameters or attributes and even on printing net its just prints empty list. Can anyone tell me what's wrong ?

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
def ___init__(self):
    super(Net, self).__init__()
    self.fc1 = nn.Linear(784, 64)
    self.fc2 = nn.Parameter(nn.Linear(64, 64))
    self.fc3 = nn.Parameter(nn.Linear(64, 64))
    self.fc4 = nn.Parameter(nn.Linear(64, 10))
    
def forward(self, x):
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = F.relu(self.fc3(x))
    x = self.fc4(x)
    return F.log_softmax(x, dim=1)

net = Net()
print(net)
print(list(net.parameters()))

Output:

Net()
[]

Solution

  • In the code

    def ___init__(self):
    

    you're using three underscores before init, use two: __init__.