如何在一维序列上训练我的LSTM?

时间:2018-08-13 04:56:47

标签: python keras

我正在尝试使用以前编码为矢量的句子来训练 LSTM 。我想做的是使用 LSTM 层将问题映射到答案。

现在,我使用X_list[0].shape获得了 LSTM 的输入形状,但是Keras期望X_list是三维的。

这是我的代码:

questions = [question.ljust(maxLenQs) for question in question]
question_ngram = n_gram.ngramStrings(questions,2)
print("Finished getting the ngrams.")
X_list = np.array(question_ngram)
print("Type of X_list: " + str(X_list.dtype))
maxLenAs = max([len(answer) for answer in answers])
Y_list = [answer.ljust(maxLenAs) for answer in answers]
Y_list = [answer.split(" ") for answer in Y_list]
vocabulary = set()
print("Beginning one-hot encoding.")
from keras.preprocessing import text
Y_list = np.array([text.one_hot(answer,len(vocabulary)) for answer in answers])
print("Finished one-hot encoding.")
# Expected number of dimensions: 2
# import sklearn.preprocessing
assertionMessage = "Assertion failed: X_list: " + str(len(X_list)) + " Y_list " + str(len(Y_list))
assert len(X_list) > 0, assertionMessage
print("Building neural network")
# Define our neural network.
from keras.models import Sequential
from keras.layers import Dense,LSTM,Dropout
from keras.callbacks import ModelCheckpoint
model = Sequential()
# Train our model.
# Each X represents columns (is our X this word/that word?) 
# Each X includes one word from the answer (or None if we're talking about the first word)
# Train our model.
# Each X represents columns (is our X this word/that word?) 
# Each X includes one word from the answer (or None if we're talking about the first word)
dimensions = 100
print("Loaded model")
model.add(LSTM(100,input_shape=X_list[0].shape,return_sequences=True))
print("X list shape: ",X_list.shape)

1 个答案:

答案 0 :(得分:0)

您可以将数据更改为三维

例如,如果您拥有形状为(1000,3250)的数据,即您有1000个大小为3250的一维数组的样本。您可以将其重塑为(1000,1,3250)并在其中训练模型。您可以使用numpy调整数据大小

import numpy as np
data = np.resize(data,(data.shape[0],1,data.shape[1]))