enchanter.addons.layers¶
AutoEncoder¶
- class enchanter.addons.layers.AutoEncoder(shapes: List[int], activation: Union[Callable[[torch.Tensor], torch.Tensor], torch.nn.modules.module.Module] = <built-in method relu of type object>)[source]¶
Bases:
torch.nn.modules.module.ModuleA class that creates an
AutoEncoder.Examples
>>> ae = AutoEncoder([32, 16, 8])
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Methods
forward(x)forward propagation processing.
TemporalConvBlock¶
- class enchanter.addons.layers.TemporalConvBlock(in_features: int, out_features: int, kernel_size: int, stride: int = 1, dilation: int = 1, dropout: float = 0.5, activation: torch.nn.modules.module.Module = ReLU(), final_activation: bool = False)[source]¶
Bases:
torch.nn.modules.module.ModuleTemporal Convolutional Block
- Paper: `An Empirical Evaluation of Generic Convolutional and Recurrent Networks for Sequence Modeling
- Parameters
in_features – the number of input channels
out_features – the number of output channels
kernel_size – kernel size
stride – stride
dilation – dilation rate
dropout – dropout rate
activation – activation function (default: ReLU)
final_activation – If true, apply the activation function after the residual connection
Methods
forward(x)Defines the computation performed at every call.
- forward(x: 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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
CausalConv1d¶
- class enchanter.addons.layers.CausalConv1d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, dilation: int = 1, groups: int = 1, bias: bool = True)[source]¶
Bases:
torch.nn.modules.conv.Conv1dCausal Conv1d
Paper: WaveNet: A Generative Model for Raw Audio
- Parameters
in_channels – the number of input channels
out_channels – the number of output channels
kernel_size – kernel size
stride – stride
dilation – rate of dilation
groups – the number of groups
bias – if true use bias (default: True)
Methods
forward(x)Forward propagation
DenseInterpolation¶
- class enchanter.addons.layers.DenseInterpolation(seq_len: int, factor: int)[source]¶
Bases:
torch.nn.modules.module.Module- Parameters
seq_len – length of input sequence.
factor –
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Methods
forward(x)Apply
Dense Interpolationto the input.
MLP¶
- class enchanter.addons.layers.MLP(shapes: List[int], activation: Union[Callable[[torch.Tensor], torch.Tensor], torch.nn.modules.module.Module] = <built-in method relu of type object>)[source]¶
Bases:
torch.nn.modules.module.ModuleClass to create MLP.
- Parameters
shapes (List[int]) – The number of neurons in each layer of MLP. Assuming an array consisting of int type elements. The value of the 0th element of the given array is treated as the number of input dimensions to the model.
activation (Union[Callable[[torch.Tensor], torch.Tensor], nn.Module]) – Activation Function. Differentiable Callable objects such as
torch.reluandenchanter.addons.Mish()
Examples
>>> import enchanter.addons as addons >>> model = addons.layers.MLP([10, 512, 128, 5], addons.Mish()) >>> print(model) >>> # ModuleList( >>> # (0): Linear(in_features=10, out_features=512, bias=True) >>> # (1): Linear(in_features=512, out_features=128, bias=True) >>> # (2): Linear(in_features=128, out_features=5, bias=True) >>> #)
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Methods
forward(x)forward propagation processing.
PositionalEncoding¶
- class enchanter.addons.layers.PositionalEncoding(d_model: int, seq_len: int, dropout: float = 0.1)[source]¶
Bases:
torch.nn.modules.module.ModulePositional Encodingused in theTransformermodel proposed in Attention is all you need.References
Sequence-to-Sequence Modeling with nn.Transformer and TorchText
- Parameters
d_model – the number of expected features in the encoder/decoder inputs.
seq_len – length of input sequence.
dropout – dropout rate.
Methods
forward(x)Forward propagation
PositionWiseFeedForward¶
- class enchanter.addons.layers.PositionWiseFeedForward(d_model: int, expansion: int = 2)[source]¶
Bases:
torch.nn.modules.module.ModulePositionWise FeedForwardproposed in Attention is all you need. This class uses1x1 Conv1dinternally.- Parameters
d_model – the number of expected features in the Position Wise Feed Forward inputs.
expansion – Magnification rate of the number of hidden dimensions. Default: 2
Examples
>>> import torch >>> import enchanter.addons as addons >>> x = torch.randn(1, 128, 512) # [N, seq_len, features] >>> ff = addons.layers.PositionWiseFeedForward(512) >>> out = ff(x)
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Methods
forward(x)Apply
PositionWiseFeedForwardto the input.
ResidualSequential¶
- class enchanter.addons.layers.ResidualSequential(*args: torch.nn.modules.module.Module)[source]¶
- class enchanter.addons.layers.ResidualSequential(arg: OrderedDict[str, Module])
Bases:
torch.nn.modules.container.SequentialExamples
>>> model = ResidualSequential( >>> Linear(10, 10), >>> ReLU(), >>> Linear(10, 10) >>> )
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Methods
forward(x)Forward propagation
SELayer1d¶
- class enchanter.addons.layers.SELayer1d(in_features: int, reduction: int = 16)[source]¶
Bases:
torch.nn.modules.module.ModuleSqueeze and Excitation block for time series data.
Paper: Squeeze-and-Excitation Networks
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Methods
forward(x)Forward pass
SELayer2d¶
- class enchanter.addons.layers.SELayer2d(in_features: int, reduction: int = 16)[source]¶
Bases:
torch.nn.modules.module.ModuleSqueeze and Excitation block for image data.
Paper: Squeeze-and-Excitation Networks
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Methods
forward(x)Forward pass