ner.nn.models.rnn module#

class ner.nn.models.rnn.RNN(embedding_dim: int, hidden_dim: int, output_dim: int, num_layers: int = 1, bias: bool = True, nonlinearity: str = 'tanh')#

Bases: Module

A multi-layer recurrent neural network with ReLU, tanh, or PReLU nonlinearity to an input sequence.

Parameters:
embedding_dimint

Number of dimensions of an input embedding.

hidden_dimint

Number of dimensions of the hidden layer(s).

output_dimint

Number of dimensions for the output layer.

num_layersint, default: 1

Number of layers in the multi-layer RNN model.

biasbool, default: True

If set to False, the input-to-hidden and hidden-to-hidden transformations will not include bias. Note: the hidden-to-output transformation remains unaffected by bias.

nonlinearity{“tanh”, “relu”, “prelu”}, default: “tanh”

Name of the nonlinearity to be applied during the forward pass.

_initial_hidden_states(batch_size: int, init_zeros: bool = False, device: device = device(type='cpu')) List[Tensor]#

Returns a list of num_layers number of initial hidden states.

Parameters:
batch_sizeint

The processing batch size.

init_zerosbool, default: False

If False, the hidden states will be initialized using the normal distribution; otherwise, they will be initialized as all zeros.

device: torch.device

The device to be used in storing the initialized tensors.

Returns:
List[torch.Tensor]

List holding tensors of initialized initial hidden states of shape (num_layers, batch_size, hidden_dim).

forward(embeddings: Tensor) Tensor#

Computes a forward pass of the given input embeddings through the RNN model.

Parameters:
embeddingstorch.Tensor

Input tensor of embeddings of shape (batch_size, max_length, embedding_dim).

Returns:
torch.Tensor

Output tensor resulting from forward pass of shape (batch_size, max_length, output_dim).