Attr2Seq

Reference:

Li Dong et al. “Learning to Generate Product Reviews from Attributes” in 2017.

class textbox.model.Attribute.attr2seq.Attr2Seq(config, dataset)[source]

Bases: AttributeGenerator

Attribute Encoder and RNN-based Decoder architecture is a basic frame work for Attr2Seq text generation.

encoder(source_idx)[source]
Parameters

source_idx (Torch.Tensor) – source attribute index, shape: [batch_size, attribute_num].

Returns

  • Torch.Tensor: output features, shape: [batch_size, attribute_num, embedding_size].

  • Torch.Tensor: hidden states, shape: [num_dec_layers, batch_size, hidden_size].

Return type

tuple

forward(corpus, epoch_idx=0)[source]

Defines 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.

generate(batch_data, eval_data)[source]

Predict the texts conditioned on a noise or sequence.

Parameters
  • batch_data (Corpus) – Corpus class of a single batch.

  • eval_data – Common data of all the batches.

Returns

Generated text, shape: [batch_size, max_len]

Return type

torch.Tensor

training: bool