0

I am working on building a 2d convolution network on my dataset.I was running it on a test set with code below :

#reproducible code
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.utils import np_utils
from keras import optimizers
from sklearn.metrics import confusion_matrix
import numpy as np
import time
from keras.layers.convolutional import Conv2D

data = np.random.rand(1000,22)
data.shape
train_X = data[0:data.shape[0],0:12]
train_X.shape
train_y = data[0:data.shape[0],12:data.shape[1]]
train_y.shape
train_X = train_X.reshape((train_X.shape[0], train_X.shape[1], 1))
train_X.shape
neurons = 10
model = Sequential()
model.add(Conv2D(filters=64,input_shape=train_X.shape, activation='relu',kernel_size = 3))
model.add(Flatten())
model.add(Dense(neurons,activation='relu')) # first hidden layer
model.add(Dense(neurons, activation='relu')) # second hidden layer
model.add(Dense(neurons, activation='relu')) # third hidden layer
model.add(Dense(10, activation='softmax'))
sgd = optimizers.SGD(lr=0.05, decay=1e-6, momentum=0.95, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.summary()
model.fit(train_X,train_y, validation_split=0.2, epochs=10, batch_size=100, verbose=0)
model.summary()

My model runs for a while and shows the following summary :

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
================================================================= 
conv2d_1 (Conv2D)            (None, 997, 10, 64)       640       
_________________________________________________________________
flatten_1 (Flatten)          (None, 638080)            0
_________________________________________________________________
dense_1 (Dense)              (None, 10)                6380810
_________________________________________________________________
dense_2 (Dense)              (None, 10)                110
_________________________________________________________________
dense_3 (Dense)              (None, 10)                110
_________________________________________________________________
dense_4 (Dense)              (None, 10)                110
=================================================================
Total params: 6,381,780
Trainable params: 6,381,780
Non-trainable params: 0

It gets stuck at the model.fit and throws up the error shown below.I am wondering how to solve this error.

Traceback (most recent call last):
  File "CNN_test.py", line 65, in <module>
    model.fit(train_X,train_y, validation_split=0.2, epochs=10, batch_size=100, verbose=0)
  File "/usr/local/lib/python3.6/site-packages/keras/engine/training.py", line 1154, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python3.6/site-packages/keras/engine/training.py", line 579, in _standardize_user_data
    exception_prefix='input')
  File "/usr/local/lib/python3.6/site-packages/keras/engine/training_utils.py", line 135, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (999, 12, 1)

2 Answers 2

1

You have no reason to use 2D convolutional layers since your data is 3D. What you're looking for is Conv1D. Also, don't include the n_samples dimension in input_shape.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv1D, Flatten
from tensorflow.keras import optimizers
import numpy as np

data = np.random.rand(1000,22)

train_X = data[0:data.shape[0],0:12]
train_X = train_X.reshape((train_X.shape[0], train_X.shape[1], 1))

train_y = data[0:data.shape[0],12:data.shape[1]]

neurons = 10
model = Sequential()
model.add(Conv1D(filters=64,input_shape=train_X.shape[1:], 
    activation='relu',kernel_size = 3))
model.add(Flatten())
model.add(Dense(neurons,activation='relu')) # first hidden layer
model.add(Dense(10, activation='softmax'))
sgd = optimizers.SGD(lr=0.05, decay=1e-6, momentum=0.95, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
history = model.fit(train_X, train_y, validation_split=0.2, epochs=1, batch_size=100)
Train on 800 samples, validate on 200 samples
100/800 [==>...........................] - ETA: 2s - loss: 11.4786 - acc: 0.0800
800/800 [==============================] - 0s 547us/sample - loss: 55.3883 - acc: 0.1000 
Sign up to request clarification or add additional context in comments.

Comments

0

You need train_X to have a fourth dimension. Just like the error message says.

train_X = train_X.reshape(train_X.shape[0], train_X.shape[1], 1, 1)

3 Comments

It leads to error at model.add stage itself: showing ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5
The problem is input_shape=train_X.shape. It should actually contain 3 dimensions only. And internally keras will add the batch dimension making it 4. Since you probably used input_shape (train_X.shape) with 4 dimensions (batch included), keras is adding the 5th.
I just checked train_X.shape is (1000, 12, 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.