Linear Layers#

Classes#

class Activation(*, activation='ELU', shapes=None, activation_kwargs=None, **kwargs)[source]#

Bases: BaseSingleLayer

Activation layer constructor.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
activationstr, Optional

Which activation function to use from PyTorch, default = ‘ELU’

shapesShapes | NoneType, Optional

Shape of the outputs from each layer, only required if tracking layer outputs is necessary

activation_kwargsdict[str, Any] | NoneType, Optional

Additional keyword arguments to pass to the activation function

**kwargs

Leftover parameters to pass to base layer for checking

class Linear(net_out, shapes, *, features=None, layer=None, factor=None, batch_norm=False, flatten_target=False, dropout=0, activation='SELU', **kwargs)[source]#

Bases: BaseSingleLayer

Linear layer constructor.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
net_outlist[int]

Shape of the network’s output

shapesShapes

Shape of the outputs from each layer

featuresint | NoneType, Optional

Number of output features for the layer, if factor is provided, features will not be used

layerint | NoneType, Optional

If factor is not None, which layer for factor to be relative to, if None, network output will be used

factorfloat | NoneType, Optional

Output features is equal to the factor of the network’s output, or if layer is provided, which layer to be relative to, will be used if provided, else features will be used

batch_normbool, Optional

If batch normalisation should be used, default = False

flatten_targetbool, Optional

If the target should be flattened so that features is equal to the product of the target multiplied by factor, if factor is provided, default = False

dropoutfloat, Optional

Probability of dropout, default = 0

activationstr | NoneType, Optional

Which activation function to use from PyTorch, default = ‘SELU’

**kwargs

Leftover parameters to pass to base layer for checking

class OrderedBottleneck(shapes, *, min_size=0, **kwargs)[source]#

Bases: BaseLayer

Information-ordered bottleneck to randomly change the size of the bottleneck in an autoencoder to encode the most important information in the first values of the latent space.

See Information-Ordered Bottlenecks for Adaptive Semantic Compression by Ho et al. (2023).

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

min_sizeint

Minimum gate size

descriptionstr

Description of the layer

Parameters:
shapesShapes

Shape of the outputs from each layer

min_sizeint, Optional

Minimum gate size, default = 0

**kwargs

Leftover parameters to pass to base layer for checking

extra_repr()[source]#

Displays layer parameters when printing the network

Returns:
str

Layer parameters

forward(x, *_, **__)[source]#

Forward pass of the information-ordered bottleneck layer

Parameters:
xTensor

Input tensor with shape (N,…,Z) and type float, where N is the batch size and Z is the latent space where Z > min_size

Returns:
Tensor

Input zeroed from a random index to the last value along the dimension Z with shape (N,…,Z) and type float

class Sample(idx, shapes, **kwargs)[source]#

Bases: BaseLayer

Samples random values from a Gaussian distribution for a variational autoencoder, mean and standard deviation are the first and second half of the input channels with the last channel ignored if there are an odd number.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

sample_layerNormal

Layer to sample values from a Gaussian distribution

Parameters:
idxint

Layer number

shapesShapes

Shape of the outputs from each layer

**kwargs

Leftover parameters to pass to base layer for checking

forward(x, net, *_, **__)[source]#

Forward pass of the sampling layer for a variational autoencoder

Parameters:
xTensor

Input tensor with shape (N,C,…) | (N,Z) and type float, where N is the batch size, and either the channels dimension, C, or latent, Z, containing the mean and standard deviation

netNetwork

Parent network that this layer is part of

Returns:
Tensor

Output tensor sampled from the input tensor split into mean and standard deviation with shape (N,C/2,…) | (N,Z/2) and type float

to(*args, **kwargs)[source]#

See torch.nn.Module.to().

class Upsample(idx, shapes, *, shape=None, scale=2, mode='nearest', **kwargs)[source]#

Bases: BaseSingleLayer

Constructs an upsampler.

Attributes:
groupint

Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks

descriptionstr

Description of the layer

layersSequential

Layers to loop through in the forward pass

Parameters:
idxint

Layer number

shapesShapes

Shape of the outputs from each layer

shapelist[int] | NoneType, Optional

Shape of the output, will be used if provided, else scale will be used

scalefloat | tuple[float] | tuple[float, …], Optional

Factor to upscale all or individual dimensions, first dimension is ignored, won’t be used if shape is provided, default = 2

mode{‘nearest’, ‘linear’, ‘bilinear’, ‘bicubic’, ‘trilinear’}

What interpolation method to use for upsampling

**kwargs

Leftover parameters to pass to base layer for checking