'ValueError: Input 0 of layer "sequential_35" is incompatible with the layer: expected shape=(None, 800, 1, 100), found shape=(None, 1, 100)
Hi Im currently working on the below program but im getting below error: ValueError: Input 0 of layer "sequential_35" is incompatible with the layer: expected shape=(None, 800, 1, 100), found shape=(None, 1, 100). I need to convert it to 4D output to run the program. Please help. Advance Thank You.
tok = Tokenizer(num_words=max_words)
tok.fit_on_texts(X)
print('Found %s unique tokens.' % len(tok.word_index))
X = tok.texts_to_sequences(X.values)
X = sequence.pad_sequences(X, maxlen=max_len)
print('Shape of data tensor:', X.shape)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.15)
le = LabelEncoder()
Y_train_enc = le.fit_transform(Y_train)
Y_train_enc = np_utils.to_categorical(Y_train_enc)
Y_test_enc = le.transform(Y_test)
Y_test_enc = np_utils.to_categorical(Y_test_enc)
def malware_model(act_func="softsign"):
model = Sequential()
model.add(Embedding(257, 128, input_shape=(max_words, 1, max_len)))
model.add(ConvLSTM2D(filters=12, kernel_size=(1, 2),
dropout=0.1, activation='relu', return_sequences=True))
model.add(ConvLSTM2D(filters=12, kernel_size=(
1, 2), dropout=0.1, activation='relu'))
model.add(ConvLSTM1D(filters=12, kernel_size=(
1), dropout=0.1, activation='relu'))
model.add(Dense(128, activation=act_func))
model.add(Dropout(0.1))
model.add(Dense(256, activation=act_func))
model.add(Dropout(0.1))
model.add(Dense(128, activation=act_func))
model.add(Dropout(0.1))
model.add(Dense(1, name='out_layer', activation="linear"))
return model
model = malware_model()
print(model.summary())
model.compile(loss='mse', optimizer="rmsprop",
metrics=['accuracy'])
history = model.fit(tf.expand_dims(X_train, axis=1), batch_size=1000, epochs=10,
validation_data=(X_test, Y_test), verbose=1)`
ValueError: Input 0 of layer "sequential_35" is incompatible with the layer: expected shape=(None, 800, 1, 100), found shape=(None, 1, 100)'
Solution 1:[1]
Looks like issue with train data shape. Make sure it should match with input shape of first layer.
Below code snippet works
import tensorflow as tf
max_words= 800
max_len =100
model = tf.keras.Sequential()
model.add(tf.keras.layers.Embedding(257, 128, input_shape=(max_words, 1, max_len)))
model.add(tf.keras.layers.ConvLSTM2D(filters=12, kernel_size=(1, 2),
dropout=0.1, activation='relu', return_sequences=True))
model.add(tf.keras.layers.ConvLSTM2D(filters=12, kernel_size=(
1, 2), dropout=0.1, activation='relu'))
model.add(tf.keras.layers.ConvLSTM1D(filters=12, kernel_size=(
1), dropout=0.1, activation='relu'))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.1))
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dropout(0.1))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.1))
model.add(tf.keras.layers.Dense(1, name='out_layer', activation="linear"))
model.summary()
Output
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 800, 1, 100, 128) 32896
conv_lstm2d_2 (ConvLSTM2D) (None, 800, 1, 99, 12) 13488
conv_lstm2d_3 (ConvLSTM2D) (None, 1, 98, 12) 2352
conv_lstm1d_1 (ConvLSTM1D) (None, 98, 12) 1200
dense (Dense) (None, 98, 128) 1664
dropout (Dropout) (None, 98, 128) 0
dense_1 (Dense) (None, 98, 256) 33024
dropout_1 (Dropout) (None, 98, 256) 0
dense_2 (Dense) (None, 98, 128) 32896
dropout_2 (Dropout) (None, 98, 128) 0
out_layer (Dense) (None, 98, 1) 129
=================================================================
Total params: 117,649
Trainable params: 117,649
Non-trainable params: 0
_______________________
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TFer |
