PyTorch
Network.parameters()
programming error
Python
deep learning

pytorch Network.parameters missing 1 required positional argument 'self'

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The error saying Network.parameters() is missing the required positional argument self usually means you called an instance method on the class instead of on a model instance. In PyTorch, parameters() belongs to an nn.Module object, so you need model.parameters(), not Network.parameters().

Why the Error Happens

In Python, methods defined on a class become bound methods only when you call them through an instance. If you call them directly on the class, Python expects you to supply the instance manually.

That is why this fails:

python
optimizer = torch.optim.Adam(Network.parameters(), lr=1e-3)

Network here is the class, not the constructed model object.

The Correct Pattern

Instantiate the network first, then access its parameters.

python
1import torch
2import torch.nn as nn
3
4class Network(nn.Module):
5    def __init__(self):
6        super().__init__()
7        self.linear = nn.Linear(10, 1)
8
9    def forward(self, x):
10        return self.linear(x)
11
12model = Network()
13optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

Now model.parameters() works because model is an actual instance of the class.

Check That the Class Inherits from nn.Module

A second important requirement is that your model class must inherit from nn.Module and call super().__init__() correctly.

python
class Network(nn.Module):
    def __init__(self):
        super().__init__()

If you skip that inheritance or initialization step, other PyTorch module behaviors will break even if the self error is fixed.

Understand What parameters() Returns

parameters() yields an iterator over the learnable tensors registered on the module. Optimizers use that iterator to know which weights to update during training.

That means parameters() is not some static class description. It depends on the concrete module instance and the layers stored inside it. This is another reason calling it on the class object does not make sense.

A Typical Novice Mistake

People often write the optimizer line too early, before they have instantiated the model, because they are thinking of the class name as if it were the model itself. In Python, classes and objects are different values, and PyTorch expects the object.

A reliable order is:

  1. define the model class,
  2. create the model instance,
  3. pass model.parameters() to the optimizer.

The Same Mistake Appears in Other PyTorch Calls

This pattern is not unique to parameters(). The same class-versus-instance confusion shows up with methods such as train(), eval(), and state_dict(). If you ever see a method complaining about self, it is worth checking whether you accidentally called a module method on the class name instead of the constructed object.

A Good Mental Checklist

When setting up training code, keep this sequence in mind:

  1. define the network class,
  2. instantiate the model,
  3. move it to device if needed,
  4. create the optimizer from model.parameters(),
  5. start training.

That order prevents a whole family of setup mistakes, not only this specific error.

Common Pitfalls

  • Calling parameters() on the class instead of the instance.
  • Forgetting to instantiate the model before building the optimizer.
  • Not inheriting from nn.Module correctly.
  • Omitting super().__init__() in the model constructor.
  • Confusing module classes with configured model objects during training setup.

Summary

  • The self error usually means parameters() was called on the class, not the instance.
  • Use model = Network() and then model.parameters().
  • Make sure the model inherits from nn.Module and initializes properly.
  • parameters() depends on the concrete module instance and its registered layers.
  • In PyTorch setup code, the model object comes before the optimizer.
  • If a method asks for self, check whether you forgot the instance.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.