该模型提供了基于SimCTG框架在Wikitext-103基准数据集(Merity等, 2016)上训练的GPT-2语言模型,源自我们的论文《神经文本生成的对比框架》(论文链接)。
我们已在项目仓库中提供SimCTG与对比搜索的详细应用教程。以下简要说明使用方法:
1. 安装SimCTG
pip install simctg --upgrade
2. 初始化模型
import torch
from simctg.simctggpt import SimCTGGPT
model_name = r'cambridgeltl/simctg_wikitext103'
model = SimCTGGPT(model_name)
model.eval()
tokenizer = model.tokenizer
3. 准备文本前缀
prefix_text = r"Butt criticized Donald 's controls in certain situations in the game , as well as the difficulty of some levels and puzzles .
Buchanan also criticized the controls , calling"
print('前缀文本:{}'.format(prefix_text))
tokens = tokenizer.tokenize(prefix_text)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
input_ids = torch.LongTensor(input_ids).view(1,-1)
4. 对比搜索生成文本
beam_width, alpha, decoding_len = 8, 0.6, 128
output = model.fast_contrastive_search(input_ids=input_ids, beam_width=beam_width,
alpha=alpha, decoding_len=decoding_len)
print("生成结果:\n" + 100 * '-')
print(tokenizer.decode(output))
'''
前缀文本:Butt criticized Donald's controls in certain situations in the game, as well as the difficulty of some levels and puzzles. Buchanan also
criticized the controls, calling
生成结果:
----------------------------------------------------------------------------------------------------
Butt criticized Donald's controls in certain situations in the game, as well as the difficulty of some levels and puzzles. Buchanan also
criticized the controls, calling them "unimpressive" and a "nightmare" of an experience to play with players unfamiliar with Tetris.
On the other hand, his opinion was shared by other reviewers, and some were critical of the game's technical design for the Wii version
of Tetris. In addition, Tintin's review included a quote from Roger Ebert, who said that Tetris was better than the original game due to
its simplicity and ease of play. Ebert's comments were included in the game's DVD commentary, released on March 22, 2010. It is unclear
if any of the video commentary was taken from the DVD
'''
更多细节请访问项目主仓库。
5. 引用
若您认为我们的论文与资源有帮助,请引用我们的工作:
@article{su2022contrastive,
title={A Contrastive Framework for Neural Text Generation},
author={Su, Yixuan and Lan, Tian and Wang, Yan and Yogatama, Dani and Kong, Lingpeng and Collier, Nigel},
journal={arXiv preprint arXiv:2202.06417},
year={2022}
}