2

I use a model with a combinaison of GRu and Conv1D. When I want to fit the model I get an error in:

ValueError: Input 0 of layer "sequential_8" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)

The shape of X_train is (223461, 5), whereas the y_train is (223461,)

This is my code:

verbose, epochs, batch_size = 0, 100, 64
n_timesteps, n_features, n_outputs = X_train.shape[0], X_train.shape[1], y_train.shape[0]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(GRU(64))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(n_outputs, activation='softmax'))
opt = Adam(learning_rate=0.01)
model.compile(loss='categorical_crossentropy', optimizer=opt , metrics=['accuracy'])
model.summary()

The output of summary is:

Model: "sequential_8"
_____  Layer (type)                Output Shape              Param #
=====  conv1d_8 (Conv1D)           (None, 223459, 64)        1024
        max_pooling1d_8 (MaxPooling  (None, 111729, 64)       0           1D)

        gru_7 (GRU)                 (None, 64)                24960
        dropout_14 (Dropout)        (None, 64)                0
        flatten_6 (Flatten)         (None, 64)                0
        dense_14 (Dense)            (None, 128)               8320
        dropout_15 (Dropout)        (None, 128)               0
        dense_15 (Dense)            (None, 223461)            28826469

===== Total params: 28,860,773 Trainable params: 28,860,773 Non-trainable params: 0
_____

and here where I face the error:

model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0)
fedorqui
  • 275,237
  • 103
  • 548
  • 598
MexcelsiorB
  • 29
  • 1
  • 5

1 Answers1

1

According to your model, your training data x_train and y_train is just a piece of data.

So your training data have to expand the dimension, like this:

X_train = X_train[None,:]
y_train = y_train[None,:]

Or use tensorflow function to do this :

X_train = tf.expand_dims(X_train, axis=0)
y_train = tf.expand_dims(y_train, axis=0)

The output shape of the model will be (1,223461)

If the output is not what you expected, it means your model design is wrong.

Denis Savenko
  • 829
  • 1
  • 13
  • 29
  • This is the [architecture](https://imgur.com/a/99P5m6S) that I want to use in my model is it right how I made it ? I want to make a binary classification of numerical data. And my classes are 0 and 1 . @AugustusHsu – MexcelsiorB Apr 12 '22 at 15:08
  • I think the architecture is fine. But I want to check your dataset first. Dose the shape of X_train (223461, 5) mean you have 223461 data with 5 sequence and 1 dimension? or 1 data with 223461 sequence and 5 dimension? – Augustus Hsu Apr 13 '22 at 02:03
  • yes yes you could check my colab [here](https://colab.research.google.com/drive/1BSdnj_1TtNSrZKJSjNAUmWvW5n2Cyobr?usp=sharing) please . yes I want to mean 223461 sequence and 5 dimension . @Augustus – MexcelsiorB Apr 13 '22 at 23:59
  • OK. Now you have `X_train`, but what do you want to predict? For example: If you want to predict the next timestamp of `vmcategory`. You need to set the `n_timesteps` and modify the `X_train` as (batch, n_timesteps, 5) to predict the `vmcategory`. Please check [WindowGenerator](https://www.tensorflow.org/tutorials/structured_data/time_series) to understand what does it do. – Augustus Hsu Apr 14 '22 at 16:50
  • Also, I modify your code to [this](https://colab.research.google.com/drive/1mgsoD6ira4YAFgnLWJ5V5fkDFURJeWhl?usp=sharing)(I'm not sure if this is what you want.). It just is a sketch, you can change architecture, loss function or parameter(like n_timesteps). – Augustus Hsu Apr 14 '22 at 17:00
  • i want to classify not predict . i have two categories in "target = **vmcategory**" the two categories are "Delay-insensitive" = **0** and "Interactive" = **1** using 5 features – MexcelsiorB Apr 16 '22 at 17:15
  • the problem is that i used the reshape method or even .expand_dims method but the accuracy stuck at zero and don't change : `Epoch 96/100 1/1 [==============================] - 7s 7s/step - loss: 1.0687e-14 - accuracy: 0.0000e+00` – MexcelsiorB Apr 16 '22 at 17:19