input error of lstm with keras - keras

I meet exactly the same question as you. I also followed the guide of #td2014 but finally an error appears. My input shape is (24443, 124, 30), my lstm layer is set as follows:
model = Sequential()
model.add(LSTM(4, input_shape = (1, 30), return_sequences = True))
model.add(Dense(1))
model.add(Activation('softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'adam')
model.fit(X_train, y_train, epoch = 1, batch_size = 124, verbose = 2)
The error I get is "Error when checking input : expected lstm_4_input have shape (None, 1, 30) but got array with shape (24443, 124, 30)"
Do you have some suggestions for that?

Related

Building a cnn-lstm model, getting this error and have no idea how to go about it

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.

ValueError: Shapes (None, 1) and (None, 90) are incompatible

I want to build a deep RNN where my x_train and my y_train. When I execute the code below:
print(X_train_fea.shape, y_train_fea.shape)
X_train_res = np.reshape(X_train_fea,(10510,10,1))
y_train_res = np.reshape(y_train_fea.to_numpy(),(-1,1))
print(X_train_res.shape, y_train_res.shape)
result:
(10510, 10) (10510,)
(10510, 10, 1) (10510, 1)
and
model = Sequential([
LSTM(90, input_shape=(10,1)),
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
When I fit the model
history = model.fit(X_train_res, y_train_res,epochs=5)
I got
ValueError: Shapes (None, 1) and (None, 90) are incompatible
Looks like y_train_res comprise of integer indices not one-hot vectors. If so you have to use sparse_categorical_crossentropy:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
and change its shape to 1D:
y_train_res = np.reshape(y_train_fea.to_numpy(),(-1,))

ValueError: A target array with shape (32, 5) was passed for an output of shape (None, 4) while using as loss categorical_crossentropy

I am trying to train a model on a dataset of 40x40 grayscale images and I am getting this error:
ValueError: A target array with shape (32, 5) was passed for an output of shape (None, 4) while using as loss categorical_crossentropy. This loss expects targets to have the same shape as the output.
I don't know where the array of (32, 5) coming from as it should be (32, 4), so I didn't know what to change. Any suggestion?
image_generator = ImageDataGenerator(#rescale = 1/255,
shear_range = 0.3,
zoom_range = 0.1,
rotation_range = 30,
width_shift_range = 0.08,
height_shift_range = 0.08,
horizontal_flip = True,
fill_mode = 'nearest',
)
train_image_generator = image_generator.flow_from_directory('/data1/mypath/generated-images/train',
target_size = (40,40),
color_mode = 'grayscale',
batch_size = 32,
class_mode = 'categorical')
test_image_generator = image_generator.flow_from_directory('/data1/mypath/generated-images/test',
target_size = (40,40),
color_mode = 'grayscale',
batch_size = 32,
class_mode = 'categorical',
shuffle = False)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3),input_shape=(40,40, 1), activation='relu', padding='same'))
model.add(BatchNormalization(axis=-1))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(32, kernel_size=(3,3),activation='relu', padding='same'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(4))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_loss',patience=5)
model.fit_generator(train_image_generator, epochs=150,
validation_data = test_image_generator,
callbacks=[early_stopping]) ```
Check the number of classes in your dataset.
The array (32,5) is coming from the shape of your dataset.
The shape of Y(label), is showing as 5 classes, and you have declared it as 4 in the last output layer as (None, 4). Which means the code is reading 4 classes from the image path provided.

How should I fit the sample data in model combining CNN and LSTM

I have a sample data of page visits of one page for 803 days. I have extracted features from data like mean, median etc and final shape of data is (803, 25). I had taken a train set of 640 and a test set of 160. I am trying to use CNN+LSTM model using Keras. But I am getting an error in model.fit method.
I have tried permute layer and changed input shapes but still not able to fix it.
trainX.shape = (642, 1, 25)
trainY.shape = (642,)
testX.shape = (161, 1, 25)
testY.shape = (161,)
'''python
# Basic layer
model = Sequential()
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), strides=1, padding='SAME', input_shape = (642, 25, 1), activation = 'relu')))
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Convolution2D(32, 3, 3, activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(Permute((2, 3), input_shape=(1, 25)))
model.add(LSTM(units=54, return_sequences=True))
# To avoid overfitting
model.add(Dropout(0.2))
# Adding 6 more layers
model.add(LSTM(units=25, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=54))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(units = 1, activation='relu', kernel_regularizer=regularizers.l1(0.0001))))
model.add(PReLU(weights=None, alpha_initializer="zero")) # add an advanced activation
model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
model.fit(trainX, trainY, epochs = 50, batch_size = 32)
predictions = model.predict(testX)
'''
#Runtime Error
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-218-86932db86d0b> in <module>()
42
43 model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
---> 44 model.fit(trainX, trainY, epochs = 50, batch_size = 32)
Error - IndexError: list index out of range
The input_shape requires a tuple of length 4 when it using TimeDistributed with Conv2D
see https://keras.io/layers/wrappers/
input_shape=(10, 299, 299, 3))
Dont you think your data field is too little? usually, CNN+LSTM is meant for a more complicated task with thousands of sequential images/videos.

Is there a way to fix the dense layer shape when adding conv2d layer with lstm?

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

Resources