Generic implementation of residual blocks. This implements a generic residual block from He et al. - Identity Mappings in Deep Residual Networks (2016), https://arxiv.org/abs/1603.05027 which can be further customized via factory functions.
| 47 | |
| 48 | |
| 49 | class ResidualBlock(nn.Module): |
| 50 | """Generic implementation of residual blocks. |
| 51 | |
| 52 | This implements a generic residual block from |
| 53 | |
| 54 | He et al. - Identity Mappings in Deep Residual Networks (2016), |
| 55 | https://arxiv.org/abs/1603.05027 |
| 56 | |
| 57 | which can be further customized via factory functions. |
| 58 | """ |
| 59 | |
| 60 | def __init__(self, residual: nn.Module, shortcut: nn.Module | None = None) -> None: |
| 61 | """Initialize ResidualBlock.""" |
| 62 | super().__init__() |
| 63 | self.residual = residual |
| 64 | self.shortcut = shortcut |
| 65 | |
| 66 | def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 67 | """Apply residual block.""" |
| 68 | delta_x = self.residual(x) |
| 69 | |
| 70 | if self.shortcut is not None: |
| 71 | x = self.shortcut(x) |
| 72 | |
| 73 | return x + delta_x |
| 74 | |
| 75 | |
| 76 | def residual_block_2d( |
no outgoing calls
no test coverage detected