agents.example_seq2seq

Reference implementation for a simplified Seq2Seq model.

Example sequence to sequence agent for ParlAI “Creating an Agent” tutorial. http://parl.ai/static/docs/tutorial_seq2seq.html

class parlai.agents.example_seq2seq.example_seq2seq.EncoderRNN(input_size, hidden_size, numlayers)

Bases: torch.nn.modules.module.Module

Encodes the input context.

__init__(input_size, hidden_size, numlayers)

Initialize encoder.

Parameters
  • input_size – size of embedding

  • hidden_size – size of GRU hidden layers

  • numlayers – number of GRU layers

forward(input, hidden=None)

Return encoded state.

Parameters
  • input – (batchsize x seqlen) tensor of token indices.

  • hidden – optional past hidden state

class parlai.agents.example_seq2seq.example_seq2seq.DecoderRNN(output_size, hidden_size, numlayers)

Bases: torch.nn.modules.module.Module

Generates a sequence of tokens in response to context.

__init__(output_size, hidden_size, numlayers)

Initialize decoder.

Parameters
  • input_size – size of embedding

  • hidden_size – size of GRU hidden layers

  • numlayers – number of GRU layers

forward(input, hidden)

Return encoded state.

Parameters
  • input – batch_size x 1 tensor of token indices.

  • hidden – past (e.g. encoder) hidden state

class parlai.agents.example_seq2seq.example_seq2seq.ExampleSeq2seqAgent(opt, shared=None)

Bases: parlai.core.torch_agent.TorchAgent

Agent which takes an input sequence and produces an output sequence.

This model is based on Sean Robertson’s seq2seq tutorial.

classmethod add_cmdline_args(argparser)

Add command-line arguments specifically for this agent.

__init__(opt, shared=None)

Initialize example seq2seq agent.

Parameters
  • opt – options dict generated by parlai.core.params:ParlaiParser

  • shared – optional shared dict with preinitialized model params

zero_grad()

Zero out optimizer.

update_params()

Do one optimization step.

share()

Share internal states.

v2t(vector)

Convert vector to text.

Parameters

vector – tensor of token indices. 1-d tensors will return a string, 2-d will return a list of strings

vectorize(*args, **kwargs)

Call vectorize without adding start tokens to labels.

train_step(batch)

Train model to produce ys given xs.

Parameters

batch – parlai.core.torch_agent.Batch, contains tensorized version of observations.

Return estimated responses, with teacher forcing on the input sequence (list of strings of length batchsize).

eval_step(batch)

Generate a response to the input tokens.

Parameters

batch – parlai.core.torch_agent.Batch, contains tensorized version of observations.

Return predicted responses (list of strings of length batchsize).