2

I have the following simple data set. It consists of 9 features and it is a binary classification problem. An example of the feature vectors are show below. Each row has its corresponding 0,1 label.

30,82,1,2.73,172,117,2,2,655.94
30,174,1,5.8,256,189,3,2,587.28
98.99,84,2,0.84,577,367,3,2,1237.34
30,28,1,0.93,38,35,2,1,112.35
...

I know CNNs are used extensively for image classification, but I'm trying to apply it to the data set I've at hand. I'm trying to apply 5 filters each of size 2. I've been stuck with getting the network to be built in the right way given the shape of this data. Here is my function that builds the network.

def make_network(num_features,nb_classes):
   model = Sequential()
   model.add(Convolution1D(5,2,border_mode='same',input_shape=(1,num_features)))
   model.add(Activation('relu'))
   model.add(Convolution1D(5,2,border_mode='same'))
   model.add(Activation('relu'))
   model.add(Flatten())
   model.add(Dense(2))
   model.add(Activation('softmax'))

I will also finally call a testing function to test the accuracy of the model I've created. The following function tries to achieve that

def train_model(model, X_train, Y_train, X_test, Y_test):

    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.3, nesterov=True)
    model.compile(loss='binary_crossentropy', optimizer=sgd)
    model.fit(X_train, Y_train, nb_epoch=100, batch_size=10,
              validation_split=0.1, verbose=1)

    print('Testing...')
    res = model.evaluate(X_test, Y_test,
                         batch_size=batch_size, verbose=1, show_accuracy=True)
    print('Test accuracy: {0}'.format(res[1]))

When I make the model and pass it the training function I get the following error

Using Theano backend.
Traceback (most recent call last):
  File "./cnn.py", line 69, in <module>
    train_model(model,x_train,y_train,x_test,y_test)
  File "./cnn.py", line 19, in train_model
    validation_split=0.1, verbose=1)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 413, in fit
    sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1011, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 938, in _standardize_user_data
    exception_prefix='model input')
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 96, in standardize_input_data
    str(array.shape))
Exception: Error when checking model input: expected convolution1d_input_1 to have 3:(None, 1, 9) dimensions, but got array with shape (4604, 9)

I'm new to Keras. I'm trying to adapt code from here. Any help or pointers would be much appreciated. Thanks in advance.

broccoli
  • 4,738
  • 10
  • 42
  • 54

1 Answers1

1

Your code model.add(Convolution1D(5,2,border_mode='same',input_shape=(1,num_features))) defines that the input should be in shape (batch_size, 1, num_features). However, X_train as well as X_test might be in shape (batch_size, 9), which is inconsistent.

def train_model(model, X_train, Y_train, X_test, Y_test):
    X_train = X_train.reshape(-1, 1, 9)
    X_test = X_test.reshape(-1, 1, 9)

    ....
Van
  • 3,749
  • 1
  • 15
  • 15