【问题标题】:How to use huggingface T5 model to test translation task?如何使用Huggingface T5模型测试翻译任务?
【发布时间】:2020-06-16 05:02:07
【问题描述】:

我看到 T5model 存在两个配置 - T5ModelTFT5WithLMHeadModel。我想测试这个翻译任务(例如en-de),因为它们在谷歌的原始仓库中显示。有没有办法可以使用这个模型从拥抱脸来测试翻译任务。我在文档方面没有看到任何与此相关的示例,并且想知道如何提供输入并获得结果。

任何帮助表示赞赏

【问题讨论】:

  • 我幼稚的方法是执行以下操作,看看它是否有效 - from transformers import T5Tokenizer, T5WithLMHeadModel tokenizer = T5Tokenizer.from_pretrained('t5-small') model = T5WithLMHeadModel.from_pretrained('t5-small') #正如他们原始论文中所建议的 input_ids = torch.tensor(tokenizer.encode("translate English to German: That is good. target:")).unsqueeze(0) outputs = model(input_ids=input_ids) scores, attn = outputs[:2] indexes = torch.argmax(scores,dim=2) predicted_token = tokenizer.convert_ids_to_tokens(indexes[0])
  • 这给了我以下结果:['.', '.', 'German', '.', 'German', 'is', ',', '.' , '', ':', '']
  • 请在问题中包含您的“幼稚方法”,而不是 cmets。

标签: python-3.x tensorflow2.0 huggingface-transformers


【解决方案1】:

您可以使用 T5ForConditionalGeneration 来翻译您的文本...

!pip install transformers

from transformers import T5Tokenizer, T5ForConditionalGeneration

tokenizer = T5Tokenizer.from_pretrained('t5-small')

model = T5ForConditionalGeneration.from_pretrained('t5-small', return_dict=True)

input = "My name is Azeem and I live in India"

# You can also use "translate English to French" and "translate English to Romanian"
input_ids = tokenizer("translate English to German: "+input, return_tensors="pt").input_ids  # Batch size 1

outputs = model.generate(input_ids)

decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)

print(decoded)

截至今天,T5WithLMHeadModel 不受 Transformers 支持。

【讨论】:

  • 谢谢!! T5Tokenizer 不工作。它返回无。但是 T5TokenizerFast 工作正常!
  • 但是要使用T5翻译新的语言?
【解决方案2】:

T5 是一个预训练模型,可以在机器翻译等下游任务上进行微调。因此,当我们要求它翻译时,我们预计会出现乱码——它还没有学会如何做到这一点。

【讨论】:

    猜你喜欢
    • 2015-11-15
    • 2021-04-05
    • 2022-08-11
    • 2021-10-08
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多