为了确保这个问题不会被忽略,我在这里重复我的回答:
我遇到了同样的问题。然而,当我阅读这个回复时,我认为如果我不使用任何特殊的生成参数(如 no_repeat_ngram_size
),我会得到相同的结果。不幸的是,似乎在束搜索实现中还有其他差异——或者我漏掉了什么?
要重现:
软件包版本:transformers==4.34.0
, ctranslate2==3.20.0
(这里使用的版本)
- 转换模型
ct2-transformers-converter --model "google/flan-t5-base" --output_dir "ct2-t5-base"
- 代码片段:
import torch
from transformers import T5ForConditionalGeneration, AutoTokenizer
import ctranslate2
device = torch.device("cuda")
model_name = "google/flan-t5-base"
hf_model = T5ForConditionalGeneration.from_pretrained(model_name).eval().to(device)
tokenizer = AutoTokenizer.from_pretrained(model_name)
fast_model = ctranslate2.Translator("ct2-t5-base", device="cuda")
text = "translate English to German: physician assistants are medical providers who are licensed to diagnose and treat illness and disease and to prescribe medication"
def get_out(inp, model):
inputs = tokenizer(inp, return_tensors="pt")
ids = model.generate(**inputs.to(device),
num_beams=3,
min_length=0,
max_length=1024,
)
return tokenizer.batch_decode(ids, skip_special_tokens=True)[0]
def get_out_fast(inp, model):
source = tokenizer.encode(inp)
source = tokenizer.convert_ids_to_tokens(source)
results = model.translate_batch([source],
beam_size=3,
min_decoding_length=0,
max_decoding_length=1024)
target = results[0].hypotheses[0]
return tokenizer.decode(tokenizer.convert_tokens_to_ids(target), skip_special_tokens=True)
res_vanilla = get_out(text, hf_model)
res_fast = get_out_fast(text, fast_model)
print("Vanilla output:", res_vanilla)
print("Ctranslate output:", res_fast)
输出:
Vanilla output: physician assistants sind medical providers, die zu Diagnose und Behandlung von Krankheiten und Krankheiten und zu Verknüpfen von Medikamenten zu ermitteln.
Ctranslate output: physician assistants sind medical providers, die zu Diagnose und Behandlung von Krankheiten und Krankheiten und zu Verknüpfen von Medikamenten zu kaufen sind.
5条答案
按热度按时间t5zmwmid1#
如果使用beam=1运行相同的内容会怎样?
o2rvlv0m2#
输出将是相同的:
nzrxty8p3#
如果你有时间,可以在这些更改之前测试CT2 2.24:
CTranslate2/CHANGELOG.md
39f48f2中的第551行到第552行
| | * 移除选项
normalize_scores
:分数
现在总是被pow(length, length_penalty)
除以length_penalty
,默认为1 || | * 移除选项
allow_early_exit
:只有在不使用惩罚时,波束搜索才会提前退出 |并在有/没有allow_early_exit和length_penalty的情况下进行测试。
nle07wnf4#
好的,我需要使用另一个模型,因为
ctranslate2==2.24.0
不支持t5,这里是我在实验中得到的结果:转换模型:
新的代码片段:
输出:
如果我没有理解您所说的“with/without allow_early_exit and length_penalty”是什么意思,请纠正我。
hiz5n14c5#
正如吉约姆之前提到的,框架之间的束搜索方法通常存在微妙的差异。在我看来,这可能会导致轻微的差异,在两种情况下看起来都很好。