Move all weight initializations from private methods to constructors #5331
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The main idiom for weight initialisation in TorchVision is to do it within constructors. Nevertheless, there is limited use of another idiom of placing the initialization in a separate private method and calling it from constructor. Though the two can be considered equivalent (the method is private after all), the use of the latter idiom can lead to gotchas.
It's worth noting that TorchVision users have the appetite for being able to re-initialize the weights (see #3410) and thus some might be tempted to call the private method directly. Unfortunately having such a method currently stumbles upon fundamental FX limitations and can lead to inheritance issues. Thus there are cases (for example after using TorchVision's Feature Extraction util) where if one calls the private method to reinitialize the model, the code won't work or even break. By moving the initialization within the constructor, we make it impossible for someone to shoot themselves in the foot. Until proper support is added for this feature in PyTorch (see pytorch/pytorch#71404), TorchVision should use idioms that minimize the chance of gotchas.