python 为NLP任务使用EtherAPI GPT模型

mspsb9vt  于 2022-12-10  发布在  Python
关注(0)|答案(1)|浏览(204)

ElutherAPI发布了许多基于PILE数据集的GPT模型,这些模型与原始GPT模型是等价的,由于它们是在更大的数据集上训练的,我们可以在同一个模型上执行多个NLP任务,而无需重新训练模型,只需很少的提示或通过使用少镜头学习提供一些上下文。
我也在尝试实现同样的目标。但问题是返回的文本有时太大或太短。下面是我的示例代码:

generator = pipeline('text-generation', model='EleutherAI/gpt-neo-1.3B', device=0)
prompt= """[Original]: The diplomatic spat came days after France cut the number of visas it issues for citizens of Algeria and other North African countries.
[Paraphrase]: """
result = generator(prompt, do_sample=True, min_length=10, max_new_tokens=50, top_p=0.9, temperature=1)

结果是这样:

France has been forced to temporarily remove two of its citizens who are on a tourist visa from Algeria and Morocco, which have had a long and acrimonious history over the past decade.
[Original]: The two visa holders, who

正如你所看到的,它给我的结果与输入文本包括,我删除了输入文本,它工作正常,但最后它仍然显示[原始]:提示,如何删除它,并给予完全相同的结果?
我尝试了很多次,甚至提供了上下文,但有时候工作正常,有时候不。我甚至尝试了几个镜头学习与数据:

"""[Original]: Algeria recalled its ambassador to Paris on Saturday and closed its airspace to French military planes a day later after the French president made comments about the northern Africa country. 
[Paraphrase]: Last Saturday, the Algerian government recalled its ambassador and stopped accepting French military airplanes in its airspace. It happened one day after the French president made comments about Algeria.
###
[Original]: President Macron was quoted as saying the former French colony was ruled by a "political-military system" with an official history that was based not on truth, but on hatred of France.
[Paraphrase]: Emmanuel Macron said that the former colony was lying and angry at France. He also said that the country was ruled by a "political-military system".
###
[Original]: The diplomatic spat came days after France cut the number of visas it issues for citizens of Algeria and other North African countries.
[Paraphrase]: Diplomatic issues started appearing when France decided to stop granting visas to Algerian people and other North African people.
###
[Original]: After a war lasting 20 years, following the decision taken first by President Trump and then by President Biden to withdraw American troops, Kabul, the capital of Afghanistan, fell within a few hours to the Taliban, without resistance.
[Paraphrase]:""

我想知道是否有办法传递end_sequence,以便在此之后停止生成,以及参数top_p和temperature,以获得良好的结果?

kkbh8khc

kkbh8khc1#

您的几次学习方法很好,但是您需要在文本生成管道中使用一个名为eos_token_id的特定参数:https://huggingface.co/docs/transformers/v4.25.1/en/main_classes/text_generation#transformers.GenerationMixin.generate
eos_token_id应该是###的令牌等价物。此id取决于您使用的令牌器。EleutherAI/gpt-neo-1.3B在后台使用的令牌器是GPT-2令牌器:https://huggingface.co/docs/transformers/model_doc/gpt2#transformers.GPT2Tokenizer
使用GPT-2标记器进行标记化时,###返回21017
因此,您应该设置eos_token_id=21017,一旦满足###序列,它将停止生成。

相关问题