banhxeo.model.classic.rnn module

class banhxeo.model.classic.rnn.RNNConfig(*, vocab_size: int | None = None, embedding_dim: int, hidden_size: int, bidirectional: bool = False, num_layers: int = 1, dropout: float = 0.0, bias: bool = False)[source]

Bases: NeuralModelConfig

hidden_size: int
bidirectional: bool
num_layers: int
dropout: float
bias: bool
check_config() Self[source]
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class banhxeo.model.classic.rnn.RNNCell(input_size: int, hidden_size: int, bias: bool)[source]

Bases: Module

__init__(input_size: int, hidden_size: int, bias: bool)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(input_t: Tensor, h_prev: Tensor | None = None) Tensor[source]

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.

class banhxeo.model.classic.rnn.RNN(vocab: Vocabulary, embedding_dim: int, hidden_size: int, bias: bool = False)[source]

Bases: NeuralLanguageModel

Use batch_first as default.

__init__(vocab: Vocabulary, embedding_dim: int, hidden_size: int, bias: bool = False)[source]

Initializes the NeuralLanguageModel.

Parameters:
  • model_config – The configuration object, instance of NeuralModelConfig or its subclass.

  • vocab – The Vocabulary instance.

forward(input_ids: Integer[Tensor, 'batch seq'], attention_mask: Integer[Tensor, 'batch seq'] | None = None, **kwargs)[source]

Defines the computation performed at every call.

Subclasses must implement this method. It should take tensors as input (e.g., input_ids, attention_mask) and return a dictionary of output tensors (e.g., logits, hidden_states, loss if computed).

Parameters:
  • *args – Variable length argument list for model inputs.

  • **kwargs – Arbitrary keyword arguments for model inputs.

Returns:

A dictionary where keys are string names of outputs (e.g., “logits”, “last_hidden_state”) and values are the corresponding `torch.Tensor`s.

Raises:

NotImplementedError – If the subclass does not implement this method.