I want to understand why we pass torch.nn.Module as a argument when we define the class for a neural network like GAN's
import torch
import torch.nn as nn
class Generator(nn.Module):
def __init__(self, input_size, hidden_size, output_size, f):
super(Generator, self).__init__()
self.map1 = nn.Linear(input_size, hidden_size)
self.map2 = nn.Linear(hidden_size, hidden_size)
self.map3 = nn.Linear(hidden_size, output_size)
self.f = f
nn module it uses method like forward(input) which returns the output. A simple neural network takes input to add weights and bias to it feed the input through multiple hidden layers and finally returns the output.
PyTorch provides the torch. nn module to help us in creating and training of the neural network. We will first train the basic neural network on the MNIST dataset without using any features from these models.
nn. ModuleList is just a Python list (though it's useful since the parameters can be discovered and trained via an optimizer). While nn. Sequential is a module that sequentially runs the component on the input.
So nn. Sequential is a construction which is used when you want to run certain layers sequentially. It makes the forward to be readable and compact.
This line
class Generator(nn.Module):
simple means the Generator
class will inherit the nn.Module
class, it is not an argument.
However, the dunder init method:
def __init__(self, input_size, hidden_size, output_size, f):
Has self which is why you may consider this as an argument.
Well this is Python class instance self
. There were tinkering battles should it stay or should it go, but Guido, explained in his blog why it has to stay
.
We are essentially defining the class 'Generator' with the nn.Module (with its functionalities). In programming we refer to this as inheritence (with the super(Generator, self).__init__()
).
You can read more here: https://www.w3schools.com/python/python_inheritance.asp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With