Reputation: 21
I am new in Deep Learning, and I'm trying to create this simple LSTM architecture in Keras using Google Colab:
The original error was:
ValueError: Error when checking input: expected lstm_2_input to have 3 dimensions, but got array with shape (4982, 12).
Then I tried:
input_shape=train_x.shape[1:]
But I got:
ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2
Then I tried:
X_train = np.reshape(X_train, X_train.shape + (1,))
But I got another error again:
ValueError: Must pass 2-d input
Then I tried:
train_x = np.reshape(train_x, (train_x.shape[0], 1, train_x.shape[1]))
But it didn't work:
Must pass 2-d input
Here is my original code:
df_tea = pd.read_excel('cleaned2Apr2019pt2.xlsx')
df_tea.head()
train_x, valid_x = model_selection.train_test_split(df_tea,random_state=2, stratify=df_tea['offer_Offer'])
train_x.shape #(4982, 12)
valid_x.shape #(1661, 12)
model = Sequential()
model.add(LSTM(32, input_shape=train_x.shape, return_sequences=True))
model.add(LSTM(32, return_sequences=True))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'])
history = model.fit(train_x, valid_x,
epochs=10,
batch_size=128,
validation_split=0.2)
I have looked through several stackoverflow and github suggestions for a similar problem, but none works.
Could someone help me please as I don't understand why all these methods failed.
Upvotes: 1
Views: 1844
Reputation: 396
According to your code, timesteps = 1 (in LSTM terminology), input_dim = 12. Hence you should let
input_shape = (1,12)
A general formula is input_shape = (None, timesteps, input_dim)
or
input_shape = (timesteps, input_dim)
An example:
import numpy as np
from keras.layers import LSTM, Dense
from keras.models import Sequential
n_examples = 4982 #number of examples
n_ft = 12 #number of features
train_x= np.random.randn(n_examples, n_ft)
#valid_x.shape #(1661, 12)
model = Sequential()
model.add(LSTM(32, input_shape=(1, n_ft), return_sequences=True))
model.add(LSTM(32, return_sequences=True))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'])
model.summary()
Upvotes: 2