Rabindra Nath Nandi
Rabindra Nath Nandi

Reputation: 1461

input_shape parameter mismatch error in Convolution1D in keras

I want to classify a dataset using Convulation1D in keras.

DataSet Description:

train dataset size = [340,30] ; no of sample = 340 , sample dimension = 30

test dataset size = [230,30] ; no of sample = 230 , sample dimension = 30

label size = 2

Fist I try by the following code using the information from keras site https://keras.io/layers/convolutional/

batch_size=1
nb_epoch = 10
sizeX=340
sizeY=30
model = Sequential()
model.add(Convolution1D(64, 3, border_mode='same', input_shape=(sizeX,sizeY)))
model.add(Convolution1D(32, 3, border_mode='same'))
model.add(Convolution1D(16, 3, border_mode='same'))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

print('Train...')
model.fit(X_train_transformed, y_train, batch_size=batch_size, nb_epoch=nb_epoch,
          validation_data=(X_test, y_test))
score, acc = model.evaluate(X_test_transformed, y_test, batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)

it gives the following error , ValueError: Error when checking model input: expected convolution1d_input_1 to have 3 dimensions, but got array with shape (340, 30)

Then I have transformed the Train and Test data into 3 dimension from 2 dimension by using the following code ,

X_train = np.reshape(X_train_transformed, (X_train_transformed.shape[0], X_train_transformed.shape[1], 1))
X_test = np.reshape(X_test_transformed, (X_test_transformed.shape[0], X_test_transformed.shape[1], 1))

Then I run the modified following code ,

batch_size=1
nb_epoch = 10
sizeX=340
sizeY=30

model = Sequential()
model.add(Convolution1D(64, 3, border_mode='same', input_shape=(sizeX,sizeY)))
model.add(Convolution1D(32, 3, border_mode='same'))
model.add(Convolution1D(16, 3, border_mode='same'))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

print('Train...')
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch,
          validation_data=(X_test, y_test))
score, acc = model.evaluate(X_test, y_test, batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)

But it shows the error , ValueError: Error when checking model input: expected convolution1d_input_1 to have shape (None, 340, 30) but got array with shape (340, 30, 1)

I am unable to find the dimension mismatch error here.

Upvotes: 0

Views: 432

Answers (2)

malder8
malder8

Reputation: 11

With the release of TF 2.0 and tf.keras, you can fairly easily update your model to work with these new versions. This can be done with the following code:

# import tensorflow 2.0
# keras doesn't need to be imported because it is built into tensorflow
from __future__ import absolute_import, division, print_function, unicode_literals

try:
  %tensorflow_version 2.x
except Exception:
  pass

import tensorflow as tf


batch_size = 1
nb_epoch = 10
# the model only needs the size of the sample as input, explained further below
size = 30

# reshape as you had before
X_train = np.reshape(X_train_transformed, (X_train_transformed.shape[0],                        
    X_train_transformed.shape[1], 1))
X_test = np.reshape(X_test_transformed, (X_test_transformed.shape[0], 
    X_test_transformed.shape[1], 1))

# define the sequential model using tf.keras
model = tf.keras.Sequential([

      # the 1d convolution layers can be defined as shown with the same
      # number of filters and kernel size
      # instead of border_mode, the parameter is padding
      # the input_shape is (the size of each sample, 1), explained below
      tf.keras.layers.Conv1D(64, 3, padding='same', input_shape=(size, 1)),
      tf.keras.layers.Conv1D(32, 3, padding='same'),
      tf.keras.layers.Conv1D(16, 3, padding='same'),

      # Dense and Activation can be combined into one layer
      # where the dense layer has 1 neuron and a sigmoid activation
      tf.keras.layers.Dense(1, activation='sigmoid')
])

# the model can be compiled, fit, and evaluated in the same way
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

print('Train...')
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch,
          validation_data=(X_test, y_test))

score, acc = model.evaluate(X_test, y_test, batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)

The problem you are having comes from the input shape of your model. According to the keras documentation the input shape of the model has to be (batch, step, channels). This means that the first dimension is the number of instances you have. The second dimension is the size of each sample. The third dimension is the number of channels which in your case would only be one. Overall, your input shape would be (340, 30, 1). When you actually define the input shape in the model, you only need to specify the the second and third dimension which means your input shape would be (size, 1). The model already expects the first dimension, the number of instances you have, as input so you do not need to specify that dimension.

Upvotes: 1

Yao Zhang
Yao Zhang

Reputation: 5781

Can you try this?

X_train = np.reshape(X_train_transformed, (1, X_train_transformed.shape[0], X_train_transformed.shape[1]))
X_test = np.reshape(X_test_transformed, (1, X_test_transformed.shape[0], X_test_transformed.shape[1]))

Upvotes: 0

Related Questions