mwptoolkit.model.Seq2Seq.dns

class mwptoolkit.model.Seq2Seq.dns.DNS(config, dataset)[source]

Bases: Module

Reference:

Wang et al. “Deep Neural Solver for Math Word Problems” in EMNLP 2017.

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

calculate_loss(batch_data: dict) float[source]

Finish forward-propagating, calculating loss and back-propagation.

Parameters

batch_data – one batch data.

Returns

loss value.

batch_data should include keywords ‘question’, ‘ques len’ and ‘equation’

convert_idx2symbol(output, num_list)[source]
convert_in_idx_2_out_idx(output)[source]
convert_out_idx_2_in_idx(output)[source]
decode(output)[source]
decoder_forward(encoder_outputs, encoder_hidden, decoder_inputs, target=None, output_all_layers=False)[source]
encoder_forward(seq_emb, seq_length, output_all_layers=False)[source]
filter_END()[source]
filter_op()[source]
forward(seq, seq_length, target=None, output_all_layers=False) Tuple[Tensor, Tensor, Dict[str, Any]][source]
Parameters
  • seq (torch.Tensor) – input sequence, shape: [batch_size, seq_length].

  • seq_length (torch.Tensor) – the length of sequence, shape: [batch_size].

  • target (torch.Tensor | None) – target, shape: [batch_size, target_length], default None.

  • output_all_layers (bool) – return output of all layers if output_all_layers is True, default False.

:return : token_logits:[batch_size, output_length, output_size], symbol_outputs:[batch_size,output_length], model_all_outputs. :rtype: tuple(torch.Tensor, torch.Tensor, dict)

init_decoder_inputs(target, device, batch_size)[source]
model_test(batch_data: dict) tuple[source]

Model test.

Parameters

batch_data – one batch data.

Returns

predicted equation, target equation.

batch_data should include keywords ‘question’, ‘ques len’, ‘equation’ and ‘num list’.

predict(batch_data: dict, output_all_layers=False)[source]

predict samples without target. :param dict batch_data: one batch data. :param bool output_all_layers: return all layer outputs of model. :return: token_logits, symbol_outputs, all_layer_outputs

rule1_filter()[source]

if r_t−1 in {+, −, ∗, /}, then rt will not in {+, −, ∗, /,), =}.

rule2_filter()[source]

if r_t-1 is a number, then r_t will not be a number and not in {(, =)}.

rule3_filter()[source]

if rt−1 is ‘=’, then rt will not in {+, −, ∗, /, =,)}.

rule4_filter()[source]

if r_t-1 is ‘(’ , then r_t will not in {(,), +, -, *, /, =}).

rule5_filter()[source]

if r_t−1 is ‘)’, then r_t will not be a number and not in {(,)};

rule_filter_(symbols, token_logit)[source]
Parameters
  • symbols (torch.Tensor) – [batch_size]

  • token_logit (torch.Tensor) – [batch_size, symbol_size]

Returns

[batch_size]

Return type

symbols of next step (torch.Tensor)

training: bool