【问题标题】:How to run huggingface Helsinki-NLP models如何运行拥抱脸赫尔辛基 NLP 模型
【发布时间】:2021-12-30 17:41:30
【问题描述】:

我正在尝试使用来自huggingface 的 Helsinki-NLP 模型,但是 我找不到任何有关如何操作的说明。 自述文件是计算机生成的,不包含解释。 有人能给我指出一个入门指南,或者展示一个如何运行像 opus-mt-en-es 这样的模型的例子吗?

【问题讨论】:

    标签: machine-learning nlp huggingface-transformers


    【解决方案1】:

    在模型页面here 上有一个在Transformers 中使用 链接,您可以使用该链接查看将其加载到transformers 包中的代码,如下所示:

    from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
    
    tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-es-en")
    model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-es-en")
    

    然后像使用任何变压器模型一样使用它:

    inp = "Me llamo Wolfgang y vivo en Berlin"
    input_ids = tokenizer(inp, return_tensors="pt").input_ids
    outputs = model.generate(input_ids=input_ids, num_beams=5, num_return_sequences=3)
    print("Generated:", tokenizer.batch_decode(outputs, skip_special_tokens=True))
    

    输出:

    Generated: ['My name is Wolfgang and I live in Berlin', 'My name is Wolfgang and I live in Berlin.', "My name's Wolfgang and I live in Berlin."]
    

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 2022-01-17
      • 2021-08-08
      • 1970-01-01
      • 2020-11-06
      • 2021-12-21
      • 2020-05-30
      • 2021-10-05
      • 2011-12-19
      相关资源
      最近更新 更多