What is the meaning of each element in input_shape of Conv1D in Keras?

3

1

I have a time-series data for 3 classes (each class is 35 second) 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 classification 3 class. But I got stuck as first layer of Conv1D. I have learnt that the input_shape of Convd1D is (batch_size, new_step, input_dim) but honestly I dont know what exactly what each element mean and how can I modify (reshape) my input data into Conv1D layer shape?

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?

I just start to learn about CNN but still not know how Conv1D operate?

I am appreciate your help.

Thank you very much.

Hear is my code (Which still get error because of first Conv1D input_shape):

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


# Encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
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 = (I really dont know what to put in here??))
    model.add(Conv1D(100, 3, activation = 'relu'))
    model.add(MaxPooling1D(3))
    model.add(Conv1D(150, 3, activation = 'relu'))
    model.add(Conv1D(150, 3, activation = 'relu'))
    model.add(GlobalAveragePooling1D())
    model.add(Dense(3, activation = 'softmax'))

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

    #model.summary()

    print('------------------------------------------------------------------------------')
    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)
    loss_per_fold.append(scores[0])

    # Increase fold number
    fold_no = fold_no + 1

Q.H.Chu

Posted 2020-07-07T04:19:38.047

Reputation: 33

Answers

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.
e.g.
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

10xAI

Posted 2020-07-07T04:19:38.047

Reputation: 3 634

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. – Q.H.Chu – 2020-07-07T10:05:24.973

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 – 10xAI – 2020-07-07T10:26:38.890