Tur*_*r33 13 python machine-learning huggingface-transformers huggingface-tokenizers
我正在使用
AutoModelForCausalLM和AutoTokenizer来生成文本输出DialoGPT。
无论出于何种原因,即使使用 Huggingface 提供的示例,我也会收到此警告:
正在使用仅解码器架构,但检测到右填充!为了正确的生成结果,请
padding_side='left'在初始化分词器时进行设置。
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
# Let's chat for 5 lines
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt')
# append the new user input tokens to the chat history
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids
# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
# pretty print last ouput tokens from bot
print("DialoGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))
Run Code Online (Sandbox Code Playgroud)
微软在 Huggingface 模型卡上提供的代码
我尝试将 padding_side='left' 添加到标记生成器中,但这不会改变任何内容。显然(从一些阅读来看)DialoGPT 无论如何都希望在右侧填充?我无法弄清楚这一点,当我尝试谷歌搜索时几乎没有结果。
我能够像这样抑制警告:
from transformers.utils import logging
logging.set_verbosity_info()
Run Code Online (Sandbox Code Playgroud)
但这似乎不是最好的答案?
小智 8
此上下文中的填充指的是“tokenizer.eos_token”,并且您当前正在填充到用户输入的右侧,并且错误表示为了获得正确的结果,请在左侧添加填充。你需要这样做:
new_user_input_ids = tokenizer.encode(tokenizer.eos_token + input(">> User:"), return_tensors='pt')
| 归档时间: |
|
| 查看次数: |
15883 次 |
| 最近记录: |