0

I'm very new to Keras and I'm tying to implement a CNN using 1D convolutions for binary classification on the raw time series data. Each training example has 160 time steps and I have 120 training examples. The training data is of shape (120,160). Here is the code:

X_input = Input((160,1))

X = Conv1D(6, 5, strides=1, name='conv1', kernel_initializer=glorot_uniform(seed=0))(X_input)
X = Activation('relu')(X)
X = MaxPooling1D(2, strides=2)(X)

X = Conv1D(16, 5, strides=1, name='conv2', kernel_initializer=glorot_uniform(seed=0))(X)
X = Activation('relu')(X)
X = MaxPooling1D(2, strides=2)(X)
X = Flatten()(X)
    
X = Dense(120, activation='relu', name='fc1', kernel_initializer=glorot_uniform(seed=0))(X)

X = Dense(84, activation='relu', name='fc2', kernel_initializer=glorot_uniform(seed=0))(X)
    
X = Dense(2, activation='sigmoid', name='fc3', kernel_initializer=glorot_uniform(seed=0))(X)

model = Model(inputs=X_input, outputs=X, name='model')


X_train = X_train.reshape(-1,160,1)   # shape (120,160,1)
t_train = y_train.reshape(-1,1,1)     # shape (120,1,1)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train)

The error that I get is expected fc3 to have 2 dimensions, but got array with shape (120, 1, 1).

I tried removing each layer and just leaving the 'conv1' component but I get the error expected conv1 to have shape (156, 6) but got array with shape (1, 1). It seems like my input shape is wrong; however, looking at other examples it seems that this worked for other people.

jpatt
  • 1

1 Answers1

0

I think the issue is not your inputs, but rather your targets.

The output of the model is 2 dimensions, but when it checks against the targets, it realizes that the targets are in an array with shape (120, 1, 1).

You can try changing the y_train reshape line as follows (fyi, it also seems that you accidentally typed t_train instead of y_train):

y_train = y_train.reshape(-1,1)

Also, it seems that you probably want to use 1 instead of 2 for the last Dense layer (see Difference between Dense(2) and Dense(1) as the final layer of a binary classification CNN?)

JJ101
  • 171
  • 5
  • Fyi, if this answered your question, you can click the check mark next to the answer. Otherwise it will continue to show up as an unanswered question. – JJ101 Aug 05 '20 at 15:01