Common layers, blocks and utils.

identity[source]

identity(x)

class Identity[source]

Identity()

exist[source]

exist(x)

ifnone[source]

ifnone(x, default)

scale[source]

scale(x)

unscale[source]

unscale(x)

trainable_parameters[source]

trainable_parameters(m:Module)

class FullyConnected[source]

FullyConnected(d_in:int, d_out:int, bn=False, preact=False, activation=ReLU) :: Sequential

A sequential container. Modules will be added to it in the order they are passed in the constructor. Alternatively, an OrderedDict of modules can be passed in. The forward() method of Sequential accepts any input and forwards it to the first module it contains. It then "chains" outputs to inputs sequentially for each subsequent module, finally returning the output of the last module.

The value a Sequential provides over manually calling a sequence of modules is that it allows treating the whole container as a single module, such that performing a transformation on the Sequential applies to each of the modules it stores (which are each a registered submodule of the Sequential).

What's the difference between a Sequential and a :class:torch.nn.ModuleList? A ModuleList is exactly what it sounds like--a list for storing Module s! On the other hand, the layers in a Sequential are connected in a cascading way.

Example::

# Using Sequential to create a small model. When `model` is run,
# input will first be passed to `Conv2d(1,20,5)`. The output of
# `Conv2d(1,20,5)` will be used as the input to the first
# `ReLU`; the output of the first `ReLU` will become the input
# for `Conv2d(20,64,5)`. Finally, the output of
# `Conv2d(20,64,5)` will be used as input to the second `ReLU`
model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )

# Using Sequential with OrderedDict. This is functionally the
# same as the above code
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

class MLP[source]

MLP(d_in:int, d_out:int, d_h:int, n_layers:int, hiddens:Sequence[T_co]=None, bn:bool=False, preact:bool=False) :: Sequential

Multi-layer perceptron

model = MLP(5, 10, 16, n_layers=3)
x = torch.randn(4, 5)
out = model(x)
assert out.shape == (4, 10)

class Conv2dBlock[source]

Conv2dBlock(c_in:int, c_out:int, ks:int, stride:int=1, padding:int=None, activation=ReLU, preact=False) :: Sequential

Convolutional block. If preact is True will be BN-ACT-CONV as prposed in https://arxiv.org/abs/1603.05027

bs, c_in, c_out, h, w = 4, 3, 8, 4, 4 
conv = Conv2dBlock(c_in, c_out, 3, 2)
x = torch.randn(bs, c_in, h, w)
out = conv(x)
assert out.shape == (bs, c_out, (h+1)//2, (w+1)//2)

class ConvTranspose2dBlock[source]

ConvTranspose2dBlock(c_in:int, c_out:int, ks:int, stride:int=1, padding:int=None, activation=ReLU, preact=False) :: Sequential

Convolutional block. If preact is True will be BN-ACT-CONV as prposed in https://arxiv.org/abs/1603.05027

bs, c_in, c_out, h, w = 4, 16, 8, 10, 10 
conv = ConvTranspose2dBlock(c_in, c_out, 4, 2)
x = torch.randn(bs, c_in, h, w)
out = conv(x)
assert out.shape == (bs, c_out, h*2, w*2)

class ResBlock[source]

ResBlock(c_in:int, c_out:int, ks:Union[int, Tuple], stride:int=1, padding:int=None, activation=ReLU) :: Module

Convolutional block with skip connection

bs, c_in, c_out, h, w = 4, 3, 8, 24, 24 
conv = ResBlock(c_in, c_out, 3, 1)
x = torch.randn(bs, c_in, h, w)
out = conv(x)
assert out.shape == (bs, c_out, h, w)

class ChanLayerNorm[source]

ChanLayerNorm(d:int, **kwargs) :: Module

Channelwise LayerNorm

x = torch.randn(1, 3, 2, 2)
m = ChanLayerNorm(3)
out = m(x)
mu = out.mean(1)
assert torch.allclose(mu+1, torch.ones_like(mu))

class ConvNet[source]

ConvNet(c_in:int, ks:int=3, n_layers=4, channels:Sequence[T_co]=None, preact=False) :: Sequential

Stack of Conv2dBlocks

model = ConvNet(1)
model
ConvNet(
  (0): Conv2dBlock(
    (0): Conv2d(1, 8, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
    (1): BatchNorm2d(8, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): ReLU(inplace=True)
  )
  (1): Conv2dBlock(
    (0): Conv2d(8, 16, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
    (1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): ReLU(inplace=True)
  )
  (2): Conv2dBlock(
    (0): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
    (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (2): ReLU(inplace=True)
  )
  (3): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
)
class ResNet(nn.Module):

    def __init__(self, c_in):
        super().__init__()
        self.net = nn.Sequential(
            Conv2dBlock(c_in, 256, 4, 2),
            nn.Conv2d(256, 256, 4, 2, 1),
            ResBlock(256, 256, (3,1), 1, activation=Identity),
            ResBlock(256, 256, (3,1), 1, activation=Identity)
        )
        
    def forward(self, x):
        return self.net(x)