c19ut
c19ut

Reputation: 59

Reshaping 2D data for Convolution Neural Network (Keras)

I have a dataset that is N_Samples by N_features [N_samples, N_features] and a corresponding labelset that is [N_samples, N_labels] I want to use Conv1D or Conv2D from keras but I don't know how to reshape the data to fit it

The dataset is around 100,000 samples with 32 features and label dataset is the same length with 6 label classes (100000, 6)

model = Sequential()

model.add(Conv1D(64, kernel_size=3, activation=’relu’, input_shape=(None,N_features,1)))

# (i would add other layers after this but right now I don't have any)

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

model.fit(X_train, y_train, batch_size=32, epochs=3)

model.predict(X_test)

Upvotes: 0

Views: 804

Answers (1)

Mikhail Berlinkov
Mikhail Berlinkov

Reputation: 1624

If you want to use Conv1D you just need to add a channel dimension with size 1, that is X_train.reshape(-1, X.shape[1], 1). If you wish to use Conv2D you may reshape it as X_train.reshape(-1, 8, 4, 1) or in any similar way so that the product of the second and the third dimension would be equal to the number of features.

Upvotes: 1

Related Questions