I have time-series data for 3 classes (each class is 35 seconds) as I extract each 1 second for 95 feature extracting, so my final data has shape (105,95) (rows for time and column for feature). I am currently building a 1D-CNN model for the classification 3 class. But I got stuck as the first layer of Conv1D.

I have learnt that the input_shape of Convd1D is (batch_size, new_step, input_dim), but honestly, I don't know what exactly each element mean and how I can modify (reshape) my input data into Conv1D layer shape.

And one more question, I know that CNN requires a fixed input size. But I split my data into train and validation data as k parameter = 5, meaning that my data will become:

Train = (84,95) and Validation = (21,95).

So with the same model, how can we train and validate data with different sizes?

Here is my code (Which still gets an error because of the first Conv1D input_shape):

X = dataset[:, 0:95].astype(float)
Y = dataset[:, 95].astype(int)

# Encode class values as integers
encoder = LabelEncoder()
encoder_Y = encoder.transform(Y)

y = np_utils.to_categorical(encoder_Y)

acc_per_fold = []
loss_per_fold = []

fold_no = 1

kfold = KFold(n_splits = 5, shuffle = True)
for train, test in kfold.split(X,y):

    model = Sequential()
    model.add(Conv1D(100, 3, activation = 'relu', input_shape = ()) #  input_shape = (I really dont know what to put in here??)
    model.add(Conv1D(100, 3, activation = 'relu'))
    model.add(Conv1D(150, 3, activation = 'relu'))
    model.add(Conv1D(150, 3, activation = 'relu'))
    model.add(Dense(3, activation = 'softmax'))

    model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])


    print(f'Training for {fold_no}...')

    history = model.fit(X[train], y[train], batch_size = 7, epochs = 150, verbose = 1)

    scores = model.evaluate(X[test], y[test], verbose = 0)
    print(f'Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%')
    acc_per_fold.append(scores[1] * 100)

    # Increase fold number
    fold_no = fold_no + 1

I just started to learn about CNN but still do not know how Conv1D operate.

I appreciate your help.

Thank you very much.


1 Answer 1


X[train].shape[0] - This is the number of instances. Let's say it is M
X[train].shape[1] - This is the shape of each instance. Each instance is (1 x N)

Since input instances are of 1-D, the input data become m x N.
Had it been 2-D, it would have been m x Nx x Ny

And one more question, I know that CNN required fixed input size. But I split my data into train and validation data as k parameter = 5, mean that my data will become: Train = (84,95) and Validation = (21,95). So with the same model how can we train and validate data with difference size

Size means the size of each instance which is same in this case i.e (1,95). Just the number of instances are different i.e. 21 in Test and 84 in Train.

TimeSeries data -
In TS learning, our initial data is just a time-based sequence of individual datapoint. We then make it Feature and Target by using a certain points (e.g. M) as Feature and (M+1)th as Target. M is identified based on TS characteristics.
1,2,3,4,5 will be (If I use 2 Feature)
1,2(X) --> 3(Y)
2,3(X) --> 4(Y)
3,4(X) --> 5(Y) etc.

These are 3 train records.

May read this too - Machinelearningmastery

  • $\begingroup$ First of all, thank you for answer my question. Say if I am wrong but I think that input=shape of Conv1D is a 2D shape matrix so that we can aplly filter on that input. Because of that I think the input shape should be (84,95) as Train data and (21,95) as Validation Data. $\endgroup$
    – Q.H.Chu
    Jul 7, 2020 at 10:05
  • $\begingroup$ Convolution will not happen across instances. It happens on instances individually. I assumed the problem as univariate. So, it will be a 1-D Image and 1-D convolution(For analogy). The kernel will slide in 1-D. Read the link. It has explained most of the stuff $\endgroup$
    – 10xAI
    Jul 7, 2020 at 10:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.