RecurrentModel#

class deeplay.models.recurrentmodel.RecurrentModel(*args, **kwargs)#

Bases: RecurrentNeuralNetwork

Recurrent Neural Network (RNN) model.

This RNN can be configured to be a simple RNN, LSTM, or GRU, with options for bidirectionality, number of layers, and other typical RNN configurations. It supports embedding layers and can be customized with different activation functions for the output layer.

Configurables#

  • in_features (int): The number of expected features in the input. Must be specified.

  • hidden_features (Sequence[int]): The number of features in each hidden layer.

  • out_features (Optional[int]): Number of features in the output layer. If None, the final RNN layer’s output is returned directly.

  • rnn_type (Literal[‘RNN’, ‘LSTM’, ‘GRU’]): Type of RNN. Defaults to ‘RNN’.

  • out_activation (Union[Literal[‘softmax’, ‘sigmoid’, ‘tanh’, ‘relu’, ‘leaky_relu’, ‘gelu’, ‘none’], torch.nn.Module]): Activation function for the output layer. Can be a string specifying the activation type or an instance of a PyTorch Module. Defaults to ‘none’.

  • bidirectional (bool): If True, makes the RNN bidirectional. Defaults to False.

  • batch_first (bool): If True, input and output tensors are provided as (batch, seq, feature). Defaults to True.

  • dropout (float): Dropout value for the outputs of each RNN layer except the last layer. Defaults to 0.

  • embedding (Optional[torch.nn.Embedding]): An embedding layer to be applied to the input data. If None, no embedding is applied.

Properties#

  • input: Returns the input layer of the network.

  • hidden: Returns the hidden layers of the network.

  • output: Returns the output layer of the network.

  • layer: Returns all layers of the network.

  • activation: Returns the activation functions used in the network.

  • normalization: Returns the normalization layers used in the network, if any.

Methods#

  • forward(x, lengths): Defines the forward pass of the RNN.

Notes#

The RNN module is designed to be flexible and configurable, allowing for various RNN types and structures.

Attributes Summary

activation

Return the activations of the network.

dropout

Return the dropout layers of the network.

hidden

Return the hidden layers of the network.

input

Return the input layer of the network.

layer

Return the layers of the network.

normalization

Return the normalizations of the network.

output

Return the last layer of the network.

Methods Summary

configure(*args, **kwargs)

Configures the module with specified arguments.

forward(x)

Define the computation performed at every call.

Attributes Documentation

activation#

Return the activations of the network. Equivalent to .blocks.activation.

dropout#

Return the dropout layers of the network. Equivalent to .blocks.dropout.

hidden#

Return the hidden layers of the network. Equivalent to .blocks[:-1]

input#

Return the input layer of the network. Equivalent to .blocks[0].

layer#

Return the layers of the network. Equivalent to .blocks.layer.

normalization#

Return the normalizations of the network. Equivalent to .blocks.normalization.

output#

Return the last layer of the network. Equivalent to .blocks[-1].

Methods Documentation

configure(*args: Any, **kwargs: Any)#

Configures the module with specified arguments.

This method allows dynamic configuration of the module’s properties and behaviors. It can be used to set or modify the attributes and parameters of the module and, if applicable, its child modules. The method intelligently handles both direct attribute configuration and delegation to child modules’ configure methods.

Parameters#

*argsAny

Positional arguments specifying the configuration settings. When the first argument is a string matching a configurable attribute, the method expects either one or two arguments: the attribute name and, optionally, its value. If the attribute is itself a DeeplayModule, subsequent arguments are passed to its configure method.

**kwargsAny

Keyword arguments for configuration settings. If provided, these are used to update the module’s configuration directly.

Raises#

ValueError

Raised if a configuration key is not recognized as a valid configurable for the module or if the provided arguments do not match the expected pattern for configuration.

Example Usage#

To configure a single attribute: ` module.configure('attribute_name', attribute_value) # or module.configure(attribute_name=attribute_value) `

To configure multiple attributes using keyword arguments: ` module.configure(attribute1=value1, attribute2=value2) `

To configure a child module’s attribute: ` module.configure('child_module_attribute', child_attribute=child_attribute_value) # or module.child_module.configure(child_attribute=child_attribute_value) `

forward(x)#

Define 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.