Home > Enterprise >  How can I train a CNN model as I am unable to train the CNN Model
How can I train a CNN model as I am unable to train the CNN Model

Time:03-18

Original dataset shape is (343889, 80) and last column is of Labels. The training and testing set split is done

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=10)
  1. shape of - training dataset (240722, 80)
  2. shape of - training Labels (240722,)
  3. shape of - testing dataset (103167, 80)
  4. shape of - testing Labels (103167,)

The model is given below

inputShape = (240722,80)
# Now Working currently
model = Sequential()
#model.add(Flatten())
model.add(Input(shape=inputShape))
#model.add(Dense(1, activation='relu'))
model.add(Conv1D(filters=20, kernel_size=10, activation='relu'))
#model.add(MaxPooling1D(pool_size=79))
#model.add(Flatten())
model.add(Dense(9))
model.compile(optimizer='adam', loss='mse')

The Summary of the model is

Model: "sequential_31"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d_32 (Conv1D)          (None, 240713, 20)        16020     
                                                                 
 dense_21 (Dense)            (None, 240713, 9)         189       
                                                                 
=================================================================
Total params: 16,209
Trainable params: 16,209
Non-trainable params: 0
_________________________________________________________________

When we run the model.fit() command it gives the following error.

model.fit(X_train, y_train, epochs=5, verbose=0)

Error received is

ValueError                                Traceback (most recent call last)
<ipython-input-132-cfd36b37e182> in <module>()
----> 1 model.fit(X_train, y_train, epochs=5, verbose=0)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 0 of layer "sequential_31" is incompatible with the layer: expected shape=(None, 240722, 80), found shape=(None, 80)

CodePudding user response:

Keras expects that your model will recieve a batch of tensors with: shape=(None, 240722, 80). Since you set the input shape to inputShape = (240722,80).

The None marker denotes a dimension of unknown size, often the batch size. Shapes in Keras models are about the individual points of data and not the batched data.

Change your input shape to: inputShape = (80,)

CodePudding user response:

The shape of model.add(Input(shape=shape)) should be of (80,).

  • Related