RankGAN Generator¶
- class textbox.module.Generator.RankGANGenerator.RankGANGenerator(config, dataset)[source]¶
Bases:
UnconditionalGenerator
RankGANGenerator is a generative model with the LSTMs.
- adversarial_loss(ref_data, discriminator_func)[source]¶
Calculate the adversarial generator loss guided by discriminator. The Monte Carlo rollouts methods is utilized to simulate intermediate rewards when a sequence is incomplete. For the partial sequence, the average ranking score is used to approximate the expected future reward.
- Parameters
discriminator_func (function) – The function provided from discriminator to calculated the ranking score.
- Returns
The calculated adversarial loss, shape: [].
- Return type
torch.Tensor
- calculate_loss(corpus, nll_test=False)[source]¶
Calculate the generated loss of corpus.
- Parameters
corpus (Corpus) – The corpus to be calculated.
nll_test (Bool) – Optional; if nll_test is True the loss is calculated in sentence level rather than in word level.
- Returns
The calculated loss of corpus, shape: [].
- Return type
torch.Tensor
- generate(batch_data, eval_data)[source]¶
Generate tokens of sentences using eval_data.
- Parameters
batch_data (Corpus) – Single batch corpus information of evaluation data.
eval_data – Common information of all evaluation data.
- Returns
The generated tokens of each sentence.
- Return type
List[List[str]]
- sample(sample_num)[source]¶
Sample sample_num generated sentence indice.
- Parameters
sample_num (int) – The number to generate.
- Returns
The generated sentence indice, shape: [sample_num, max_length].
- Return type
torch.Tensor
- sample_batch()[source]¶
Sample a batch of generated sentence indice.
- Returns
The generated sentence indice, shape: [batch_size, max_length].
- Return type
torch.Tensor
- training: bool¶