I am trying to create a CNN to classify data. My Data is X[N_data, N_features] I want to create a neural net capable of classifying it. My problem is concerning the input shape of a Conv1D for the keras back end.
I want to repeat a filter over.. let say 10 features and then keep the same weights for the next ten features. For each data my convolutional layer would create N_features/10 New neurones. How can i do so? What should I put in input_shape?
def cnn_model():
model = Sequential()
model.add(Conv1D(filters=1, kernel_size=10 ,strides=10,
input_shape=(1, 1,N_features),kernel_initializer= 'uniform',
activation= 'relu'))
model.flatten()
model.add(Dense(N_features/10, init= 'uniform' , activation= 'relu' ))
Any advice? thank you!
To input a usual feature table data of shape
(nrows, ncols)
toConv1d
ofKeras
, following 2 steps are needed:For example, taking first 4 features of
iris
dataset:To see usual format and its shape:
The output shows usual format and its shape:
Following code alters the format:
Output of above code data format and its shape:
This works well for
Conv1d
ofKeras
. Forinput_shape
(4,1)
is needed.@Marcin's answer might work, but might suggestion given the documentation here:
would be:
Note that since input data (N_Data, N_features), we set the number of examples as unspecified (None). The
strides
argument controls the size of of the timesteps in this case.Try:
And reshape your
x
to shape(nb_of_examples, nb_of_features, 1)
.EDIT:
Conv1D
was designed for a sequence analysis - to have convolutional filters which would be the same no matter in which part of sequence we are. The second dimension is so called features dimension where you could have a vector of multiple features at each of timesteps. One may think about sequence dimension the same as spatial dimensions and feature dimension the same as channel dimension or color dimension inConv2D
. As @putonspectacles mentioned in his comment - you may set sequence dimension toNone
in order to make your network input length invariant.