kedarps
kedarps

Reputation: 861

Incompatible shapes error while training ConvNet using Keras+Tensorflow

I am trying to build a simple convolutional neural network to classify time series into one-of-six classes. I am having an issue with training the network due to incompatible shapes error.

In the following code, n_feats = 1000, n_classes = 6.

Fs = 100
input_layer = Input(shape=(None, n_feats), name='input_layer')
conv_layer = Conv1D(filters=32, kernel_size=Fs*4, strides=int(Fs/2), padding='same', activation='relu', name='conv_net_coarse')(input_layer)
conv_layer = MaxPool1D(pool_size=4, name='c_maxp_1')(conv_layer)
conv_layer = Dropout(rate=0.5, name='c_dropo_1')(conv_layer)
output_layer = Dense(n_classes, name='output_layer')(conv_layer)

model = Model(input_layer, output_layer)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
print(model.summary())

Here is the model summary.

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_layer (InputLayer)     (None, None, 1000)        0         
_________________________________________________________________
conv_net_coarse (Conv1D)     (None, None, 32)          12800032  
_________________________________________________________________
c_maxp_1 (MaxPooling1D)      (None, None, 32)          0         
_________________________________________________________________
c_dropo_1 (Dropout)          (None, None, 32)          0         
_________________________________________________________________
output_layer (Dense)         (None, None, 6)           198       
=================================================================
Total params: 12,800,230
Trainable params: 12,800,230
Non-trainable params: 0
_________________________________________________________________
None

When I run, model.fit(X_train, Y_train), where X_train shape is (30000, 1, 1000) and Y_train shape is (30000, 1, 6), I get incompatible shapes error:

InvalidArgumentError (see above for traceback): Incompatible shapes: [32,0,6] vs. [1,6,1]
     [[Node: output_layer/add = Add[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](output_layer/Reshape_2, output_layer/Reshape_3)]]
     [[Node: metrics_1/acc/Mean/_197 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_637_metrics_1/acc/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

If I remove the MaxPool1D and Dropout layers, the model trains just fine. Am I not specifying those layers correctly?

Any help would be appreciated!

Upvotes: 1

Views: 639

Answers (1)

Marcin Możejko
Marcin Możejko

Reputation: 40506

So - the problem lies in two facts:

  1. The input shape should be (number_of_examples, timesteps, features) where the feature is something recorded per time step. This means that you should reshape your data to (number_of_examples, 1000, 1) as your time sequence has 1000 timesteps and 1 feature.
  2. As you are solving classification task - you need to squash your input to vector (from a sequence). I'd advice you to use Flatten before Dropout layer.

Upvotes: 2

Related Questions