mwptoolkit.module.Environment.stack_machine

class mwptoolkit.module.Environment.stack_machine.OPERATIONS(out_symbol2idx)[source]

Bases: object

class mwptoolkit.module.Environment.stack_machine.StackMachine(operations, constants, embeddings, bottom_embedding, dry_run=False)[source]

Bases: object

Parameters
  • constants (list) – Value of numbers.

  • embeddings (tensor) – Tensor of shape [len(constants), dim_embedding]. Embedding of the constants.

  • bottom_embedding (teonsor) – Tensor of shape (dim_embedding,). The embeding to return when stack is empty.

add_variable(embedding)[source]
Tell the stack machine to increase the number of nuknown variables

by 1.

Parameters

embedding (torch.Tensor) – Tensor of shape (dim_embedding). Embedding of the unknown varialbe.

apply_embed_only(operation, embed_res)[source]

Apply operator on stack with embedding operation only.

Parameters
  • operator (mwptoolkit.module.Environment.stack_machine.OPERATION) – One of - OPERATIONS.ADD - OPERATIONS.SUB - OPERATIONS.MUL - OPERATIONS.DIV - OPERATIONS.EQL

  • embed_res (torch.FloatTensor) – Resulted embedding after transformation, with size (dim_embedding,).

Returns

embedding on the top of the stack.

Return type

torch.Tensor

apply_eql(operation)[source]
get_height()[source]

Get the height of the stack.

Returns

height.

Return type

int

get_solution()[source]

Get solution. If the problem has not been solved, return None.

Returns

If the problem has been solved, return result from sympy.solve. If not, return None.

Return type

list

get_stack()[source]
get_top2()[source]

Get the top 2 embeddings of the stack.

Returns

Return tensor of shape (2, embed_dim).

Return type

torch.Tensor

push(operand_index)[source]

Push var to stack.

Parameters

operand_index (int) – Index of the operand. If index >= number of constants, then it implies a variable is pushed.

Returns

Simply return the pushed embedding.

Return type

torch.Tensor