Skip to content
Advertisement

Value error in convolutional neural network due to data shape

I am trying to predict the of number peaks in time series data by using a CNN and keep on getting a data shape error. My data looks as follows:

  • X = list of 520 lists (each is a time series) of various lengths (shortest = 137 elements, longest = 2297 elements)
  • y = list with 520 elements, each being the number of peaks for the respective time series

Due to the various lengths of the time series, I padded X. The shapes of X_train and X_test, after converting them from numpy arrays to tensors are:

  • X_train.shape = TensorShape([390, 2297])
  • X_test.shape = TensorShape([130, 2297])

I am new to keras and I am very unsure about the input_size in the first Conv1D layer. According to this post (Keras/Tensorflow Conv1D expected input shape) I chose it as (2297, 1) or (520, 1), but none of them works. The documentation of Keras says that the input shape should be (batch_size, feature_size, channels), where batch_size is omitted though.

from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import Adam

#for structure of X and y, see explanation above

X_padded = tf.keras.preprocessing.sequence.pad_sequences(X)

X_train, X_test, y_train, y_test = train_test_split(X_padded, y, test_size=0.25, random_state=33)


X_train = tf.convert_to_tensor(X_train)
X_test = tf.convert_to_tensor(X_test)
y_train = tf.convert_to_tensor(y_train)
y_test = tf.convert_to_tensor(y_test)

model = keras.Sequential()
model.add(Conv1D(filters=16, kernel_size=3, activation = 'relu', strides = 1, padding = 'same', input_shape=(2297, 1)))
model.add(Dropout(0.1))
model.add(Conv1D(filters=32, kernel_size=3, activation = 'relu', strides = 1, padding = 'same'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(9, activation='softmax')) # '9' because there are 9 possible peak counts in the data

model.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])

progress = model.fit(X_train, y_train, epochs = 15, validation_data = (X_test, y_test), verbose=1)

Error:

 ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 2297]

What might be the issue here?

Advertisement

Answer

I was able to solve it. The correct input shape is given here Convolutional neural network Conv1d input shape in the answer of user ‘rnso’.

I shaped my X_train and X_test (being numpy.arrays) as

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

and stated the input_shape in the Conv1D statement as input_shape=(ncols, 1)

input_shape=(2297, 1)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement