我想使用 Huggingface Transformers 來實作一個聊天機器人。目前,我有如下所示的代碼。變壓器模型已經考慮了過去用戶輸入的歷史。
在構建聊天機器人時,我還需要考慮其他什么(附加代碼)嗎?
其次,如何修改我的代碼以使用 TensorFlow 而不是 PyTorch 運行?
稍后,我還計劃在其他資料上對模型進行微調。我還計劃測驗不同的模型,例如 BlenderBot 和 GPT2。我認為容易測驗此不同的模式應該是在更換相應的模型AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")和AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
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)))
uj5u.com熱心網友回復:
這是將DialoGPT模型與 Tensorflow結合使用的示例:
from transformers import TFAutoModelForCausalLM, AutoTokenizer, BlenderbotTokenizer, TFBlenderbotForConditionalGeneration
import tensorflow as tf
chat_bots = {
'BlenderBot': [BlenderbotTokenizer.from_pretrained('facebook/blenderbot-400M-distill'), TFT5ForConditionalGeneration.from_pretrained('facebook/blenderbot-400M-distill')],
'DialoGPT': [AutoTokenizer.from_pretrained("microsoft/DialoGPT-small"), TFAutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")],
}
key = 'DialoGPT'
tokenizer, model = chat_bots[key]
for step in range(5):
new_user_input_ids = tokenizer.encode(input(">> User:") tokenizer.eos_token, return_tensors='tf')
if step > 0:
bot_input_ids = tf.concat([chat_history_ids, new_user_input_ids], axis=-1)
else:
bot_input_ids = new_user_input_ids
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
print(key ": {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))
>> User:How are you?
DialoGPT: I'm here
>> User:Why are you here
DialoGPT: I'm here
>> User:But why
DialoGPT: I'm here
>> User:Where is here
DialoGPT: Where is where?
>> User:Here
DialoGPT: Where is here?
如果您想比較不同的聊天機器人,您可能需要調整它們的解碼器引數,因為它們并不總是相同的。例如,使用BlenderBot和 a max_lengthof 50 你會用當前代碼得到這種回應:
>> User:How are you?
BlenderBot: ! I am am great! how how how are are are???
一般來說,您應該問自己哪些特殊字符對聊天機器人很重要(取決于您的域),哪些字符應該/可以省略?
您還應該嘗試不同的解碼方法,例如貪婪搜索、波束搜索、隨機采樣、top-k 采樣和核采樣,并找出最適合您的用例的方法。有關此主題的更多資訊,請查看此帖子
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/363851.html
標籤:Python 张量流 无印良品 聊天机器人 拥抱变形金刚
上一篇:嘗試將兩個MapDataset組合成一個MapDataset
下一篇:修復CNN過擬合
