mwptoolkit.module.Decoder.rnn_decoder¶
- class mwptoolkit.module.Decoder.rnn_decoder.AttentionalRNNDecoder(embedding_size, hidden_size, context_size, num_dec_layers, rnn_cell_type, dropout_ratio=0.0)[source]¶
Bases:
Module
Attention-based Recurrent Neural Network (RNN) decoder.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(input_embeddings, hidden_states=None, encoder_outputs=None, encoder_masks=None)[source]¶
Implement the attention-based decoding process.
- Parameters
input_embeddings (torch.Tensor) – source sequence embedding, shape: [batch_size, sequence_length, embedding_size].
hidden_states (torch.Tensor) – initial hidden states, default: None.
encoder_outputs (torch.Tensor) – encoder output features, shape: [batch_size, sequence_length, hidden_size], default: None.
encoder_masks (torch.Tensor) – encoder state masks, shape: [batch_size, sequence_length], default: None.
- Returns
output features, shape: [batch_size, sequence_length, num_directions * hidden_size]. hidden states, shape: [batch_size, num_layers * num_directions, hidden_size].
- Return type
tuple(torch.Tensor, torch.Tensor)
Initialize initial hidden states of RNN.
- Parameters
input_embeddings (torch.Tensor) – input sequence embedding, shape: [batch_size, sequence_length, embedding_size].
- Returns
the initial hidden states.
- Return type
torch.Tensor
- training: bool¶
- class mwptoolkit.module.Decoder.rnn_decoder.BasicRNNDecoder(embedding_size, hidden_size, num_layers, rnn_cell_type, dropout_ratio=0.0)[source]¶
Bases:
Module
Basic Recurrent Neural Network (RNN) decoder.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(input_embeddings, hidden_states=None)[source]¶
Implement the decoding process.
- Parameters
input_embeddings (torch.Tensor) – target sequence embedding, shape: [batch_size, sequence_length, embedding_size].
hidden_states (torch.Tensor) – initial hidden states, default: None.
- Returns
output features, shape: [batch_size, sequence_length, num_directions * hidden_size]. hidden states, shape: [batch_size, num_layers * num_directions, hidden_size].
- Return type
tuple(torch.Tensor, torch.Tensor)
Initialize initial hidden states of RNN.
- Parameters
input_embeddings (torch.Tensor) – input sequence embedding, shape: [batch_size, sequence_length, embedding_size].
- Returns
the initial hidden states.
- Return type
torch.Tensor
- training: bool¶
- class mwptoolkit.module.Decoder.rnn_decoder.SalignedDecoder(operations, dim_hidden=300, dropout_rate=0.5, device=None)[source]¶
Bases:
Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(context, text_len, operands, stacks, prev_op, prev_output, prev_state, number_emb, N_OPS)[source]¶
- Parameters
context (torch.Tensor) – Encoded context, with size [batch_size, text_len, dim_hidden].
text_len (torch.Tensor) – Text length for each problem in the batch.
operands (list of torch.Tensor) – List of operands embeddings for each problem in the batch. Each element in the list is of size [n_operands, dim_hidden].
stacks (list of StackMachine) – List of stack machines used for each problem.
prev_op (torch.LongTensor) – Previous operation, with size [batch, 1].
prev_arg (torch.LongTensor) – Previous argument indices, with size [batch, 1]. Can be None for the first step.
prev_output (torch.Tensor) – Previous decoder RNN outputs, with size [batch, dim_hidden]. Can be None for the first step.
prev_state (torch.Tensor) – Previous decoder RNN state, with size [batch, dim_hidden]. Can be None for the first step.
- Returns
op_logits: Logits of operation selection. arg_logits: Logits of argument choosing. outputs: Outputs of decoder RNN. state: Hidden state of decoder RNN.
- Return type
tuple(torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor)
- pad_and_cat(tensors, padding)[source]¶
Pad lists to have same number of elements, and concatenate those elements to a 3d tensor.
- Parameters
tensors (list of list of Tensors) – Each list contains list of operand embeddings. Each operand embedding is of size (dim_element,).
padding (Tensor) – Element used to pad lists, with size (dim_element,).
- Returns
Length of lists in tensors. tensors (Tensor): Concatenated tensor after padding the list.
- Return type
n_tensors (list of int)
- training: bool¶