MCPcopy Create free account
hub / github.com/apple/ml-sharp / ResidualBlock

Class ResidualBlock

src/sharp/models/blocks.py:49–73  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

47
48
49class 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
76def residual_block_2d(

Callers 2

residual_block_2dFunction · 0.85
_residual_blockMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected