Nid*_*utt 6 nlp python-3.x huggingface-transformers
我正在尝试使用 Huggingface 将内容从英语转换为印地语。这是代码片段
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-hi")
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-hi")
text = "Hello my friends! How are you doing today?"
tokenized_text = tokenizer.prepare_seq2seq_batch([text])
# Perform translation and decode the output
translation = model.generate(**tokenized_text)
translated_text = tokenizer.batch_decode(translation, skip_special_tokens=True)[0]
# Print translated text
print(translated_text)
Run Code Online (Sandbox Code Playgroud)
我在尝试调用“模型”上生成的方法时收到此错误。
属性错误:“列表”对象没有属性“大小”。
我正在 Transformer 版本 4.3.3 上运行。
cro*_*oik 16
该模型需要 pytorch 张量而不是 python 列表。只需添加return_tensors=\'pt\'到prepare_seq2seq:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n\ntokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-hi")\n\nmodel = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-hi")\ntext = "Hello my friends! How are you doing today?"\ntokenized_text = tokenizer.prepare_seq2seq_batch([text], return_tensors=\'pt\')\n\n# Perform translation and decode the output\ntranslation = model.generate(**tokenized_text)\ntranslated_text = tokenizer.batch_decode(translation, skip_special_tokens=True)[0]\n\n# Print translated text\nprint(translated_text)\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n\xe0\xa4\x86\xe0\xa4\xaa \xe0\xa4\x86\xe0\xa4\x9c \xe0\xa4\x95\xe0\xa5\x88\xe0\xa4\xb8\xe0\xa5\x87 \xe0\xa4\x95\xe0\xa4\xb0 \xe0\xa4\xb0\xe0\xa4\xb9\xe0\xa5\x87 \xe0\xa4\xb9\xe0\xa5\x88\xe0\xa4\x82?\nRun Code Online (Sandbox Code Playgroud)\n