I am working with keras for text classification. After pre-processing and vectorization my train and validation data details is like bellow:
print(X_train.shape, ',', X_train.ndim, ',', type(X_train))
print(y_train.shape, ',', y_train.ndim, ',', type(y_train))
print(X_valid.shape, ',', X_valid.ndim, ',', type(X_valid))
print(y_valid.shape, ',', y_valid.ndim, ',', type(y_valid))
print(data_dim)
output is:
(14904,) , 1 , <class 'numpy.ndarray'>
(14904,) , 1 , <class 'numpy.ndarray'>
(3725,) , 1 , <class 'numpy.ndarray'>
(3725,) , 1 , <class 'numpy.ndarray'>
15435
then model definition is:
model = Sequential()
model.add(LSTM(100, input_shape=(data_dim,1 ), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(200))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics = ['accuracy'])
model.summary()
model summury:
model fitting:
model.fit(X_train,y_train, validation_data = (X_valid, y_valid),
batch_size=batch_size, epochs=epochs)
Why does this error occur?
----> 1 model.fit(X_train,y_train, validation_data = (X_valid, y_valid),
2 batch_size=batch_size, epochs=epochs)
...
...
ValueError: Input 0 of layer sequential is incompatible with the layer:
expected ndim=3, found ndim=2. Full shape received: [None, 1]
I finally overcame the problem with the help of this kaggle notebook.
I change data dimensions to:
print(X_train.shape)
print(y_train.shape)
print(X_valid.shape)
print(y_valid.shape)
print(X_test.shape)
print(y_test.shape)
print(data_dim)
########################## output ###########################
(14904, 15435)
(14904,)
(3725, 15435)
(3725,)
(5686, 15435)
(5686,)
15435
and then reshape data to:
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_valid = np.reshape(X_valid, (X_valid.shape[0], 1, X_valid.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
########################## output ###########################
(14904, 1, 15435)
(3725, 1, 15435)
(5686, 1, 15435)
finally change LSTM
input_shape
to:
model.add(LSTM(units=50, input_shape=(1, data_dim), return_sequences=True))
now, model summary is:
There is no problem right now and model.fit
executes fine.