I’m not sure what you mean by self.module in your title…
The self.modules() method returns an iterable to the many layers or “modules” defined in the model class. This particular piece of code is using that self.modules() iterable to initialize the weights of the different layers present in the model. isinstance() checks if the particular layer “m” is an instance of a conv2d or linear or conv3d layer etc. and initializes the weights accordingly.
Why don’t you check the docs for all these functions to get a better understanding?
@shubhvachher I have another question regarding this. Why initialize the weights manually?
Correct me if I am wrong, the weights are initialized when you create your layers when using the torch.nn modules and these weights are random. Why re-initializing them?
Weights are initialized to small random numbers on creation (it actually depends on the type of layer you’re using but for argument’s sake we’ll just say small random numbers), usually this isn’t a huge deal as your model will eventually learn the correct weights through gradient descent. However, depending on the model complexity this may take a long time or get stuck in a local minimum. You can set the weights manually to “guide” your model to faster convergence if you have an idea on what would work better.
I’m guessing you don’t mean initializing weights with those of a pretrained model but the different types of initializations in literature.
I’m not an expert on this but I think for learning from scratch, the initializations done by pytorch internally are good enough for most tasks. You could do these initializations yourself if subject matter experts in your learning task or information you gathered from your paper review suggests some initialization techniques for that task fair better than others.
You are not alone, there are no stupid questions, learning requires asking questions that are known to others , but not yourself. There was a starting point for all when we asked what a PC was. Personal Caretaker was popular prior to 90’s LOL
I also have to say the brilliance and help on here is incredible. I browse to see if people are being helped and treated fairly, and I must say, out of hundreds of sites this is in the top 5 for active help. Kudos people! A+