Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv1d_1/convolution/Conv2D

Binary classification problem: I want to have One input layer(optional), One Conv1D layer then output layer of 1 neuron predicting either 1 or 0.
Here is my model:

x_train = np.expand_dims(x_train,axis=1)
x_valid = np.expand_dims(x_valid,axis=1)
#x_train = x_train.reshape(x_train.shape[0], 1, x_train.shape[1])
#x_valid = x_train.reshape(x_valid.shape[0], 1, x_train.shape[1])

model = Sequential()

#hidden layer
model.add(Convolution1D(filters = 1, kernel_size = (3),input_shape=(1,x_train.shape[2])))
#output layer
model.add(Flatten())
model.add(Dense(1, activation = 'softmax'))

sgd = SGD(lr=0.01, nesterov=True, decay=1e-6, momentum=0.9)

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

print('model compiled successfully')
model.fit(x_train, y_train, nb_epoch = nb_epochs, validation_data=(x_valid,y_valid), batch_size=100)

Input shape: x_train.shape = (5,1,133906) which is (batch,steps,channels) respectively. Steps added through expand_dims. Actual size (5,133906) which is 5 samples of time series data of length 133906 sampled randomly sometimes at 2 ms and sometimes at 5 ms.

Error Message: ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv1d_1/convolution/Conv2D' (op: 'Conv2D') with input shapes: [?,1,1,133906], [1,3,133906,1].

How do I resolve this issue? What should the size of x_train and the input_size argument passed inside Conv1D be?

like image 313
Sravani Anne Avatar asked Oct 26 '18 17:10

Sravani Anne


1 Answers

Convolution1D layers takes input in a format of [batch, steps, channels]

Your length of convolution window (kernel size) cannot be larger than number of steps.

Therefore if you want to use your defined input shape of:

x_train.shape = (5,1,133906)

you need to change kernel size to 1

i.e. change line 9 to

model.add(Convolution1D(filters = 1, kernel_size = 1,input_shape=(1,x_train.shape[2])))

However, this will only enable your example to work. Depending on your goals, data type, etc. you might want to try different combinations of your kernel size and dimensions of input data to obtain best results.

like image 101
Sljder Avatar answered Sep 22 '22 01:09

Sljder