How do I train my LSTM on 1-dimensional sequences?
Clash Royale CLAN TAG#URR8PPP
How do I train my LSTM on 1-dimensional sequences?
I am trying to train an LSTM using sentences that were previously encoded as vectors. What I want to do is to use the LSTM layer to map a question to an answer.
Now, I get the input shape for the LSTM using X_list[0].shape
, but Keras expects X_list
to be three-dimensional.
X_list[0].shape
X_list
Here is my code:
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 Answer
1
You can change your data's into three dimension
For example, if you have data of shape (1000,3250)
i.e you have 1000 samples of 1D array of size 3250. You can reshape into (1000,1,3250)
and train your model in it. You can use numpy to resize your data
(1000,3250)
(1000,1,3250)
import numpy as np
data = np.resize(data,(data.shape[0],1,data.shape[1]))
did you change the input_shape parameter in the first layer. if not set it as
input_shape=X_list.shape[1:]
– Hari Krishnan
Aug 13 at 5:15
input_shape=X_list.shape[1:]
Well, yes - but your code seems to have resolved the ValueError.
– Montana Burr
Aug 13 at 5:25
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So, confusingly, I've resized my X_list and now I have a 3-dimensional shape. However, Keras is now complaining that my input is 4-dimensional.
– Montana Burr
Aug 13 at 5:13