This repository was archived by the owner on Apr 1, 2021. It is now read-only.
Add modules for easily constructing residual networks #33
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.
Here are two modules that implement residual blocks in a nice object-oriented way. They subclass the container modules, and they accept modules as positional arguments similarly to
nn.Sequential
. They also pretty-print nicely, again due to subclassing the container modules. No more self-implemented convoluted logic in separately-defined__init__()
andforward()
The
ResidualBlock
module just takes a sequence of modules and adds a 'shortcut connection' between its input and its output. In other words its final output is the sum of the output of its last module and its original input. I also provide aResidualBlockWithShortcut
module, which lets you customize the shortcut connection, for instance to make sure it is the same shape as the output of the main branch.No more self-implemented convoluted easily-gotten-wrong network topology in separately-defined
__init__()
andforward()
methods, when all you want is a standard ResNet! The code looks like this:and the model looks like this when printed:
The references for residual blocks are: Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun, "Deep Residual Learning for Image Recognition" (https://arxiv.org/abs/1512.03385), and "Identity Mappings in Deep Residual Networks" (https://arxiv.org/abs/1603.05027).