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.
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:
Network here is the class, not the constructed model object.
The Correct Pattern
Instantiate the network first, then access its parameters.
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.
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:
- define the model class,
- create the model instance,
- 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:
- define the network class,
- instantiate the model,
- move it to device if needed,
- create the optimizer from
model.parameters(), - 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.Modulecorrectly. - Omitting
super().__init__()in the model constructor. - Confusing module classes with configured model objects during training setup.
Summary
- The
selferror usually meansparameters()was called on the class, not the instance. - Use
model = Network()and thenmodel.parameters(). - Make sure the model inherits from
nn.Moduleand 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
- PyTorch predict single example
- Pytorch RuntimeError CUDA out of memory with a huge amount of free memory
- Pytorch RuntimeError reduce failed to synchronize cudaErrorAssert device-side assert triggered
- PyTorch torch.no_grad vs torch.inference_mode
- PyTorch Optimizer AdamW and Adam with weight decay
- Pytorch RuntimeError expected scalar type Float but found Byte
- Query DynamoDB with a hash key and a range key with Boto3
- Query whether Python's threading.Lock is locked or not
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.