如何将文本数据集(问题和答案)加载到 numpy 数组中以训练 keras 模型
How to load Text Dataset (question and answer) into numpy array for training a keras model
我有一个 数据集,其中包含问题和答案,格式如下:
[question...]?\t[answer...].
示例:
Do you like pizza? Yes its delicious.
...
现在我想用它训练一个keras模型。
但是当我加载它时,我无法将它变成一个 numpy 数组,
因为句子的长度不一样。
在input_text和out_text中,我将问题和答案存储为这样的拆分词:
[["Do", "you", "like", "pizza", "?"]
[ ... ]]
这是我的代码的一部分。 (我也用自制函数把词转成向量)
X_data = []
Y_data = []
for i in range(len(input_text)):
xdata = []
ydata = []
xdata = xdata+[wordtovec(word,wrdvecdic) for word in input_text[i]]
for i in range(len(input_text[i])):
ydata.append([0 for i in range(300)])
xdata.append([0 for i in range(300)])
ydata.append([0 for i in range(300)])
ydata = ydata+[wordtovec(word, wrdvecdic) for word in out_text[i]]
for i in range(len(out_text[i])):
xdata.append([0 for i in range(300)])
X_data.append(xdata)
Y_data.append(ydata)
X_data = np.array(X_data)
Y_data = np.array(Y_data)
也许可以展示如何执行此操作,或者有一个类似数据集的示例 link 以及如何将其加载到 keras 的 numpy 数组中。
感谢您的回复。
我不知道有任何专门针对质量检查的教程,但是 Tensorflow official website 上有一个关于相关问题的很好的教程。
由于我们的训练数据必须是相同的长度,所以我们通常使用填充函数来标准化长度。例如:
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
from tensorflow.python.keras.preprocessing.text import Tokenizer
question = ['thi is test', 'this is another test 2']
answers = ['i am answer', 'second answer is here']
tknizer = Tokenizer()
tknizer.fit_on_texts(question + answers)
question = tknizer.texts_to_sequences(question)
answer = tknizer.texts_to_sequences(answer)
question = pad_sequences(question, value=0, padding='post', maxlen=20)
answer = pad_sequences(answer, value=0, padding='post', maxlen=20)
print(question)
输出:
[[4 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[5 1 6 2 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
在上面的示例中,我们假设最大长度为 20。长度超过 20 的序列将被截断以使其适合所需的长度,而长度小于 20 的序列将在末尾填充 0。
现在,您可以将预处理后的数据输入 Keras:
inps1 = Input(shape=(20,))
inps2 = Input(shape=(20,))
embedding = Embedding(10000, 100)
emb1 = embedding(inps1)
emb2 = embedding(inps2)
# ... rest of the network
pred = Dense(100,'softmax')(prev_layer)
model = Model(inputs=[inps1, inps2],outputs=pred)
model.compile('adam', 'categorical_crossentropy')
model.fit([question, answer], labels, batch_size=128, epoch=100)
我有一个 数据集,其中包含问题和答案,格式如下:
[question...]?\t[answer...].
示例:
Do you like pizza? Yes its delicious.
...
现在我想用它训练一个keras模型。 但是当我加载它时,我无法将它变成一个 numpy 数组, 因为句子的长度不一样。
在input_text和out_text中,我将问题和答案存储为这样的拆分词:
[["Do", "you", "like", "pizza", "?"]
[ ... ]]
这是我的代码的一部分。 (我也用自制函数把词转成向量)
X_data = []
Y_data = []
for i in range(len(input_text)):
xdata = []
ydata = []
xdata = xdata+[wordtovec(word,wrdvecdic) for word in input_text[i]]
for i in range(len(input_text[i])):
ydata.append([0 for i in range(300)])
xdata.append([0 for i in range(300)])
ydata.append([0 for i in range(300)])
ydata = ydata+[wordtovec(word, wrdvecdic) for word in out_text[i]]
for i in range(len(out_text[i])):
xdata.append([0 for i in range(300)])
X_data.append(xdata)
Y_data.append(ydata)
X_data = np.array(X_data)
Y_data = np.array(Y_data)
也许可以展示如何执行此操作,或者有一个类似数据集的示例 link 以及如何将其加载到 keras 的 numpy 数组中。
感谢您的回复。
我不知道有任何专门针对质量检查的教程,但是 Tensorflow official website 上有一个关于相关问题的很好的教程。
由于我们的训练数据必须是相同的长度,所以我们通常使用填充函数来标准化长度。例如:
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
from tensorflow.python.keras.preprocessing.text import Tokenizer
question = ['thi is test', 'this is another test 2']
answers = ['i am answer', 'second answer is here']
tknizer = Tokenizer()
tknizer.fit_on_texts(question + answers)
question = tknizer.texts_to_sequences(question)
answer = tknizer.texts_to_sequences(answer)
question = pad_sequences(question, value=0, padding='post', maxlen=20)
answer = pad_sequences(answer, value=0, padding='post', maxlen=20)
print(question)
输出:
[[4 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[5 1 6 2 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
在上面的示例中,我们假设最大长度为 20。长度超过 20 的序列将被截断以使其适合所需的长度,而长度小于 20 的序列将在末尾填充 0。
现在,您可以将预处理后的数据输入 Keras:
inps1 = Input(shape=(20,))
inps2 = Input(shape=(20,))
embedding = Embedding(10000, 100)
emb1 = embedding(inps1)
emb2 = embedding(inps2)
# ... rest of the network
pred = Dense(100,'softmax')(prev_layer)
model = Model(inputs=[inps1, inps2],outputs=pred)
model.compile('adam', 'categorical_crossentropy')
model.fit([question, answer], labels, batch_size=128, epoch=100)