The following is my code:
The shape of my X_train is TensorShape([600, 22000, 5])
The shape of my Y_train is (600, 9)
Is there an error with the type of data that I am using for this time-series problem?
model = Sequential()
model.add(LSTM(256,return_sequences=True,input_shape=(22000, 5)))
model.add(Dense(9, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
#print(model.summary())
model.fit(allfileswow[:600], features_a1[:600], epochs=100,verbose=0)
Hi just like #Djinn mentioned add a flatten layer to make everything 1-D
model = Sequential()
model.add(LSTM(256,return_sequences=True,input_shape=(22000, 5)))
model.add(Flatten())
model.add(Dense(9, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
Try removing 'return_sequences=True' as you don't need full sequence as the output for the next layer.
Refer lstm
Related
I have some pixel movement data and it has 5 features and 3715489 training samples. I keep getting this error and I don't know what I should make the input_shape for the LSTM.
X_train shape is (3715489,5). Do I need to reshape this?
y_train shape is (3715489, 8)
Here is my code:
model = Sequential()
model.add(LSTM(256,return_sequences=True, input_shape=(5,)))
model.add(Dense(8, activation='sigmoid'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, epochs=100,batch_size=320)
As mentioned in the error,
ValueError: Input 0 of layer lstm_27 is incompatible with the layer: expected ndim=3, found ndim=2
The LSTM layer expects 3D inputs with a shape of (batch_size, timesteps, input_dim). You can pass (timesteps, input_dim) for input_shape argument. But you are setting input_shape (5,). This shape does not include timesteps dimension. Adding an extra dimension to input_shape will solve the problem.
#Reshape data
x_train = tf.reshape(x_train,(-1, 1, 5))
y_train = tf.reshape(y_train,(-1, 1, 8))
model = Sequential()
model.add(tf.keras.layers.LSTM(256,return_sequences=True, input_shape=(1,5)))
model.add(tf.keras.layers.Dense(8, activation='sigmoid'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, epochs=1,batch_size=320)
Please refer to this link for more information. Thank you!
This my script:
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
model = Sequential()
model.add(Embedding(vocab_size, embedding_dim, input_length=train_padded.shape[1]))
model.add(Conv1D(48, 5, activation='relu', padding='valid'))
model.add(GlobalMaxPooling1D())
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(5, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
epochs = 100
batch_size = 32
history = model.fit(train_padded, training_labels, shuffle=True ,
epochs=epochs, batch_size=batch_size,
validation_split=0.2,
callbacks=[ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.0001),
EarlyStopping(monitor='val_loss', mode='min', patience=3, verbose=1),
EarlyStopping(monitor='val_accuracy', mode='max', patience=3, verbose=1)])
I have an error:
ValueError: Shapes (None, 4) and (None, 5) are incompatible
Have you any idea please.
This is the original notebook: https://github.com/snymanje/MultiClass-Text-Classification-with-Tensorflow/blob/master/MultiClass_Text_Classification_with_Tensorflow.ipynb
When I added my database, I git this error when fitting the model.
Thank you
Looks like your labels don't tie to your model.
Try changing this line:
model.add(Dense(4, activation='softmax'))
I know that similar questions were asked before, but the solutions didn't helped me.
I have the following model:
model = Sequential()
# CNN
model.add(Conv2D(filters=16, kernel_size=2, input_shape=(40, 2000, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
# CNN
model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
# CNN
model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
# CNN
model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
model.add(GlobalAveragePooling2D())
model.add(Dense(num_labels, activation='softmax'))
optimizer = optimizers.SGD(lr=0.002, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
I'm trying to fit the model:
model.fit(X_train, y_train_hot, batch_size=10, epochs=50,
validation_data=(X_test, y_test_hot))
where
X_train.shape = {tuple:3} (246, 40, 2000)
from other post I read (Keras input_shape for conv2d and manually loaded images) it seems that my input is right.
But I'm getting the following error:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 40, 2000]
What am I missing ? and how can I fix it ?
As you saw from the link you posted you must reshape your data to be 4dim.
X_train = X_train.reshape(246, 40, 2000, 1)
or
X_train = X_train.reshape(-1, 40, 2000, 1)
4D: [batch_size, img_height, img_width, number_of_channels]
The error is that you did not include the 4-th axis(i.e. axis=3 or axis -1 for that matter).
Here, you can see the following:
expected min_ndim=4, found ndim=3. Full shape received: [None, 40,
2000]
The None translates to the batch size, which of course is variable and set to None.
Then, you have 40, 2000, which should correspond to height and width respectively.
At the same time, remember that you wrote in your code that the input shape your network expects is input_shape=(40, 2000, 1) not (40,2000).
You need to explicitly add the "color"/"number of channels" axis, the 3-rd channel in this case, so you need to either use reshape or expand_dims to achieve that.
For demonstrative purposes, suppose that X is of shape (40,2000), then reshape X to X = X.reshape(40,2000,1)
I have a cnn network, which I'm trying to test.
I'm getting errors about the input, and I can't figure why
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(99,13,1)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, batch_size=4, epochs=10, verbose=1, validation_data=(x_test, y_test))
where:
x_train / test .shape = {tuple: 3}(30, 99, 13)
y_train / test shape = {tuple: 1}30
Error:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 99, 13]
What's wrong and how can I fix it ?
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(99,13,1)))
Model waiting for shape [batch, (99,13,1)], you are trying to feed [batch, 99, 13].
I think you need change your input to input_shape=(99,13) in order to match train data
Conv2D requires 4D+ shape
Expand dimension of your data to batch,99,13,1
Here is my code:
model = Sequential()
model.add(Conv1D(32, kernel_size=3,
activation='relu',
input_shape=(14,1)))
model.add(MaxPooling1D(pool_size=1))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='softmax'))
model.summary()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(X_train.values, y_train.values,
batch_size=4,
epochs=1,
verbose=2,
validation_data=(X_test.values,y_test.values))
And error is:
Error when checking input: expected conv1d_35_input to have 3 dimensions, but got array with shape (13166, 14)
As suggested by other posts, I tweaked with flatten layer before output layer but that did not work.
My X_train.values.shape gives (13166, 14)
Any suggestion how should I fix this?
You need to reshape the X_train.values from (13166, 14) to (13166, 14, 1) as your input shape of CNN network is (None, 14, 1).
This may solve your problem:
X_train.values.reshape([-1,14,1])