【发布时间】:2019-03-18 06:33:22
【问题描述】:
我正在尝试使用 Python 中的 Tensorflow 构建一个具有 seq2seq 神经网络实现的聊天机器人。我以前从未做过 seq2seq,而且我的大部分研究都没有什么帮助。
我不会直截了当地要求序列到序列聊天机器人的代码。相反,我的问题是如何最好地准备一个单词列表作为网络的输入。我还没有深入了解代码,但是我制作了一个脚本,该脚本将从文件中加载训练数据并对其进行标记。
但是,显然 Tensorflow 神经网络不能接受字符串作为输入。我需要将这些字符串转换为网络知道如何处理的数据;即数字。
到目前为止,这是我的代码;希望 cmets 让您了解我在哪里:
#Import dependencies
import tensorflow as tf
#Fetch and preprocess data
#Define a tokenizer function
def tokenize(string):
tokenized_list = []
tmp_indx = 0
for i in range(len(string)):
if string[i] in "?.,!;":
tokenized_list.append(string[tmp_indx:i])
tokenized_list.append(string[i])
tmp_indx = i+1
elif string[i] == " ":
tokenized_list.append(string[tmp_indx:i])
tmp_indx = i+1
#A quick and dirty way out :/
tokenized_list = [x for x in tokenized_list if x!=""]
return tokenized_list
raw_file_data = ""
with open("training_dialogue.txt") as file:
raw_file_data = file.read()
raw_file_data = raw_file_data.split("\n")
#Train data as list of values like so: [query, target_response]
train_data = []
for i in range(0,len(raw_file_data)):
if i%2!=0:
#Perform the most basic tokenization algorithm
query = tokenize(raw_file_data[i-1])
target_response = tokenize(raw_file_data[i])
train_data.append([query, target_response])
#Now that I have a list of tokens in the form of strings, I need to map these to numbers somehow
#Load encoder and decoder networks
#Define hyperparameters
#Train them on the data
如果有人能告诉我如何以某种方式将这些单词处理成数字,那就太好了。我还需要能够将它们从数字变成文字。
【问题讨论】:
-
您可能会发现this answer 很有用。
-
嗯...这很有趣。我没有意识到 TF 有这种能力。
-
我也只是想我可能会使用声音序列而不是单词。这个聊天机器人应该是类似于 Siri 或 Google 助理的东西,你可以在那里说话,他们可以回应。
-
目前我打算使用谷歌语音识别将语音转换为文本,通过网络运行,然后使用文本到语音。但是,您认为将声音数据作为序列简单地输入是否可行?
-
...并发出声音作为响应。抱歉,我不是要发送垃圾邮件,我只是一直用完字符。
标签: python tensorflow neural-network recurrent-neural-network preprocessor