flambe.nn.rnn

Module Contents

class flambe.nn.rnn.RNNEncoder(input_size: int, hidden_size: int, n_layers: int = 1, rnn_type: str = 'lstm', dropout: float = 0, bidirectional: bool = False, layer_norm: bool = False, highway_bias: float = 0, rescale: bool = True, enforce_sorted: bool = False)[source]

Bases: flambe.nn.module.Module

Implements a multi-layer RNN.

This module can be used to create multi-layer RNN models, and provides a way to reduce to output of the RNN to a single hidden state by pooling the encoder states either by taking the maximum, average, or by taking the last hidden state before padding.

Padding is delt with by using torch’s PackedSequence.

rnn

The rnn submodule

Type:nn.Module
forward(self, data: Tensor, state: Optional[Tensor] = None, mask: Optional[Tensor] = None)[source]

Performs a forward pass through the network.

Parameters:data (Tensor) – The input data, as a float tensor
Returns:
  • Tensor – The encoded output, as a float tensor
  • Tensor – The encoded state, as a float tensor
class flambe.nn.rnn.PooledRNNEncoder(input_size: int, hidden_size: int, n_layers: int = 1, rnn_type: str = 'lstm', dropout: float = 0, bidirectional: bool = False, layer_norm: bool = False, highway_bias: float = 0, rescale: bool = True, pooling: str = 'last')[source]

Bases: flambe.nn.module.Module

Implement an RNNEncoder with additional pooling.

This class can be used to obtan a single encoded output for an input sequence. It also ignores the state of the RNN.

forward(self, data: Tensor, state: Optional[Tensor] = None, mask: Optional[Tensor] = None)[source]

Perform a forward pass through the network.

Parameters:data (torch.Tensor) – The input data, as a float tensor
Returns:The encoded output, as a float tensor
Return type:torch.Tensor