nntoolbox.components.components module

class nntoolbox.components.components.BiasLayer(shape: Tuple[int, ], init: float = 0.0)[source]

Bases: torch.nn.modules.module.Module

Add a trainable bias vector to input:

y = x + bias

forward(input: torch.Tensor) → torch.Tensor[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class nntoolbox.components.components.HighwayLayer(in_features: int, main: torch.nn.modules.module.Module, gate: Optional[torch.nn.modules.module.Module] = None)[source]

Bases: torch.nn.modules.module.Module

Highway layer:

y = T(x) * H(x) + (1 - T(x)) * x

Reference:

https://arxiv.org/pdf/1505.00387.pdf

forward(input)[source]
Parameters

input – (batch_size, in_features)

Returns

output: (batch_size, in_features)

training: bool
class nntoolbox.components.components.LambdaLayer(fn: Callable[[torch.Tensor], torch.Tensor])[source]

Bases: torch.nn.modules.module.Module

Implement a quick layer wrapper for a function

Useful for stateless layer (e.g without parameters)

forward(input: torch.Tensor) → torch.Tensor[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class nntoolbox.components.components.LinearlyAugmentedFF(in_features: int, out_features: int, activation: Callable[[...], torch.nn.modules.module.Module] = <class 'torch.nn.modules.linear.Identity'>)[source]

Bases: torch.nn.modules.module.Module

Based on https://link.springer.com/chapter/10.1007/978-3-642-35289-8_13

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class nntoolbox.components.components.MLP(in_features: int, out_features: int, hidden_layer_sizes: Sequence[int] = (512,), activation: Callable[[...], torch.Tensor] = <class 'torch.nn.modules.activation.ReLU'>, bn_final: bool = False, drop_ps=(0.5, 0.5), use_batch_norm: bool = True)[source]

Bases: torch.nn.modules.container.Sequential

Implement a generic multilayer perceptron

training: bool
class nntoolbox.components.components.ModifyByLambda(module: torch.nn.modules.module.Module, fn: Callable[[torch.Tensor], torch.Tensor])[source]

Bases: torch.nn.modules.module.Module

forward(input: torch.Tensor) → torch.Tensor[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class nntoolbox.components.components.QuadraticPolynomialLayer(in_features: int, out_features: int, rank: int, sqrt: bool = False, bias: bool = False, eps: float = 1e-06)[source]

Bases: torch.nn.modules.module.Module

h(x) = sigma( sum_k(A_k x)^2 + bx + c)

References:

Bergstra et al. “Quadratic Polynomials Learn Better Image Features.” http://www.iro.umontreal.ca/~lisa/publications2/index.php/attachments/single/205 (dead link, use web archive)

Joseph Turian, James Bergstra and Yoshua Bengio. “Quadratic Features and Deep Architectures for Chunking.” https://www.aclweb.org/anthology/N09-2062

forward(input: torch.Tensor) → torch.Tensor[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class nntoolbox.components.components.ResidualLinearBlock(in_features: int, activation: Callable[[...], torch.nn.modules.module.Module] = <class 'torch.nn.modules.activation.ReLU'>, bias: bool = True, use_dropout: bool = False, drop_rate: float = 0.5)[source]

Bases: torch.nn.modules.module.Module

A two-layer linear block with residual connection:

y = f(w_2f(w_1 x + b_1) + b_2) + x

forward(input)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class nntoolbox.components.components.ScalingLayer(scale: float = 0.1)[source]

Bases: nntoolbox.components.components.LambdaLayer

References:

Christian Szegedy et al. “Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning.” https://arxiv.org/pdf/1602.07261.pdf

training: bool
class nntoolbox.components.components.SquareUnitLinear(in_features, out_features, bias: bool = True)[source]

Bases: torch.nn.modules.linear.Linear

Augment input with square units:

g(x) = W concat([x, x^2]) + b

Reference:

Flake, Gary. “Square Unit Augmented, Radially Extended, Multilayer Perceptrons.” Neural Network: Tricks of the Trade

forward(input)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

in_features: int
out_features: int
weight: torch.Tensor