I'm attempting to perform a sentiment classification using CNN. The error seems to be related to the input_shape parameters.
The x data consists of arrays of integers created using tokenizer.texts_to_sequences.
? x_train.shape
(4460, 20)
? x_trains.shape[0]
array([ 49, 472, 4436, 843, 756, 659, 64, 8, 1328, 87, 123,
352, 1329, 148, 2996, 1330, 67, 58, 4437, 144])
The y data consist of one hot encoded values for classification.
y_train.shape
(4460, 2)
y_train[0]
array([1., 0.], dtype=float32)
here is the model:
model.add(layers.Conv1D(filters=256, kernel_size=3, activation='relu', input_shape=(max_seqlen,)))
model.add(layers.SpatialDropout1D(0.2))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(100, activation='relu'))
model.add(layers.Dense(num_classes, activation="softmax"))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=3, batch_size=512,
validation_data=(x_val, y_val), class_weight=label_weights)
An error is thrown adding the Conv1D layer. The message is:
"Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=2"
I have no idea what I'm doing wrong. Any help is greatly appreciated.
Conv1D takes a 2D input (I don't know why this is the case). As your input is only 1D, your dimensions don't match up. I'm afraid that you will probably have to stick to other keras layer types, or alternatively reshape your data so that it is (4460, 20, 1), allowing you to pass a conv1D over it.
Related
model.add(LSTM(100, input_shape=(156,156, 3), return_sequences=True)) #error
model.add(LSTM(Embedding(8192, 256)))
model.add(LSTM(SpatialDropout1D(0.3)))
model.add(LSTM(256, dropout=0.3, recurrent_dropout=0.3))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(5, activation='softmax'))
ValueError: Input 0 of layer "lstm_10" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)
The shapes of my data:
x_train shape : (1532, 156, 156, 3) y_train shape : (1532,)
x_test shape : (384, 156, 156, 3) y_test shape : (384,)
Trying to build a cnn-lstm model for a project. The LSTM layer as mentioned is throwing an error.
So when I was trying to train a model with LSTM, I have reshaped my input data to (1000, 96, 1), and output data to (1000, 24, 1), which means I want to predict futural 24 data with previous 96 data.
When I add a timedistributed dense layer as the last layer, I get an error:
ValueError: Error when checking target: expected time_distributed_1 to have shape (96, 1) but got array with shape (24, 1)
So what's wrong?
Here are my codes:
modelA.add(LSTM(units=64, return_sequences=True,
input_shape=[xProTrain_3D.shape[1], xProTrain_3D.shape[2]]))
modelA.add(LSTM(units=128, return_sequences=True))
modelA.add(Dropout(0.25))
modelA.add(Dropout(0.25))
modelA.add(LSTM(units=256, return_sequences=True))
modelA.add(Dropout(0.25))
modelA.add(LSTM(units=128, return_sequences=True))
modelA.add(Dropout(0.25))
modelA.add(LSTM(units=64, return_sequences=True))
modelA.add(Dropout(0.25))
modelA.add(TimeDistributed(Dense(units=1, activation='relu', input_shape=(24, 1))))
modelA.compile(optimizer='Adam',
loss='mse',
metrics=['mse'])
modelA.summary()
modelA.fit(x=xProTrain_3D, y=yProTrain_3D, epochs=epoch, batch_size=batch_size)
By the way, the input shape is (1000, 96, 1) and output shape is (1000, 24, 1)
I have the following code snippet:
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(X_train.shape[0], 1, X_train.shape[2]), return_sequences=False))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, epochs=100, batch_size=32)
y_pred = model.predict(X_test)
However, I get the following error:
ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4
The original shapes of X_train and X_test:
X_train: 1483, 13
X_test: 360, 13
and after reshaping they become:
X_train: 1483, 1, 13
X_test: 360, 1, 13
I know this might be a duplicate, but none of the answers online seem to work for me.
input_shape=(X_train.shape[0], 1, X_train.shape[2]) is wrong.
An LSTM should have 2D input shapes (which means 3D internal tensors).
l
- The input shape must contain (sequence_length, features_per_step).
- This means the internal shape will be (free_batch_size, sequence_length, features_per_step)
Your data then must be 3D, ok, but the input_shape should be 2D.
Now, sequence_length is absolutely necessary for a recurrent layer to work, if you have sequence_length = 1 it's totaly useless, unless you are going for stateful=True which involves much more complicated code.
I am trying to fit my data into a conv2d+lstm layers but I got an error in the last dense layer
i already tried to reshape but it gives me the same error .. and because I am new in python I couldn't understand how to fix my error My model is about combining cnn with lstm layer and i have 2892 training images and 1896 testing images with total 4788 images each image with size 128*128
And here is the final model summary
Here some of my code
cnn_model = Sequential()
cnn_model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(128,128,3)))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(32, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(64, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(128, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Flatten())
model = Sequential()
model.add(cnn_model)
model.add(Reshape((4608, 1)))
model.add(LSTM(16, return_sequences=True, dropout=0.5))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
X_data = np.array(X_data)
X_datatest = np.array(X_datatest)
X_data= X_data.astype('float32') / 255.
X_datatest = X_datatest.astype('float32') / 255.
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest))
The error I expected to be in the dense layer as its outputs the following error
Traceback (most recent call last): File
"C:\Users\bdyssm\Desktop\Master\LSTMCNN2.py", line 212, in
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest)) File
"C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py",
line 952, in fit
batch_size=batch_size) File "C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py",
line 789, in _standardize_user_data
exception_prefix='target') File "C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training_utils.py",
line 128, in standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected dense_1 to have 3
dimensions, but got array withshape (2892, 128, 128, 3)
and this is the cnn_model summary
My problem here that I want to make the number of input channels in python equals to dimension of filters
i already tried to reshape but it gives me the same error .. and because I am new in python I couldn't understand how to fix my error
My model is about combining cnn with lstm layer and i have 2892 training images and 1896 testing images with total 4788 images each image with size 128*128
here some code of what i had tried
cnn_model = Sequential()
cnn_model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(128,128,3)))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(32, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(64, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(128, (3, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Flatten())
model = Sequential()
model.add(TimeDistributed(cnn_model, input_shape=(1,128, 128,3)))
model.add(LSTM(16, return_sequences=True, dropout=0.5))
model.add(Dense(1, activation='softmax'))
model.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy'])
X_data = np.array(X_data)
X_datatest = np.array(X_datatest)
X_data= X_data.astype('float32') / 255.
X_datatest = X_datatest.astype('float32') / 255.
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest))
when trying the previuos code the following error showed up
Traceback (most recent call last): File
"C:\Users\bdyssm\Desktop\Master\LSTMCNN2.py", line 219, in
hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest)) File
"C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py",
line 952, in fit
batch_size=batch_size) File "C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py",
line 751, in _standardize_user_data
exception_prefix='input') File "C:\Users\bdyssm\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training_utils.py",
line 128, in standardize_input_data
'with shape ' + str(data_shape)) ValueError: Error when checking input: expected time_distributed_1_input to have 5 dimensions, but got
array with shape (2892, 28, 28, 3)
This is the model summary
This is the cnn_model summary
The problem is that your cnn_model has changed the shape of your signal to have 128 channels insteal of 3 color channels, but you are not taking this into account when declaring the input shape of model.
Examine the output shape of cnn_model with cnn_model.summary() and make sure to have input shape of model equal to the output shape of cnn_model.