Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convolutional neural network Conv1d input shape

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!

like image 689
FrankyBravo Avatar asked Apr 05 '17 15:04

FrankyBravo


People also ask

How does CNN define input shape?

Input shape has (batch_size, height, width, channels) . Incase of RGB image would have a channel of 3 and the greyscale image would have a channel of 1 . Thought it looks like out input shape is 3D , but you have to pass a 4D array at the time of fitting the data which should be like (batch_size, 10, 10, 3) .

What does a Conv1D layer do?

Conv1D class. 1D convolution layer (e.g. temporal convolution). This layer creates a convolution kernel that is convolved with the layer input over a single spatial (or temporal) dimension to produce a tensor of outputs. If use_bias is True, a bias vector is created and added to the outputs.

What is the input of 1D CNN?

In 1D CNN, kernel moves in 1 direction. Input and output data of 1D CNN is 2 dimensional.

What is the difference between Conv1D and conv2d?

conv1d is used when you slide your convolution kernels along 1 dimensions (i.e. you reuse the same weights, sliding them along 1 dimensions), whereas tf. layers. conv2d is used when you slide your convolution kernels along 2 dimensions (i.e. you reuse the same weights, sliding them along 2 dimensions).


3 Answers

Try:

def cnn_model():
    model = Sequential()                                               
    model.add(Conv1D(filters=1, kernel_size=10 ,strides=10,     
              input_shape=(N_features, 1),kernel_initializer= 'uniform',      
              activation= 'relu')) 
model.flatten()
model.add(Dense(N_features/10, init= 'uniform' , activation= 'relu' ))
....

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 in Conv2D. As @putonspectacles mentioned in his comment - you may set sequence dimension to None in order to make your network input length invariant.

like image 104
Marcin Możejko Avatar answered Nov 04 '22 21:11

Marcin Możejko


@Marcin's answer might work, but might suggestion given the documentation here:

When using this layer as the first layer in a model, provide an input_shape argument (tuple of integers or None, e.g. (10, 128) for sequences of 10 vectors of 128-dimensional vectors, or (None, 128) for variable-length sequences of 128-dimensional vectors.

would be:

model = Sequential()
model.add(Conv1D(filters=1, kernel_size=10 ,strides=10,     
                  input_shape=(None, N_features),kernel_initializer= 'uniform',      
                  activation= 'relu')) 

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.

like image 30
parsethis Avatar answered Nov 04 '22 21:11

parsethis


To input a usual feature table data of shape (nrows, ncols) to Conv1d of Keras, following 2 steps are needed:

xtrain.reshape(nrows, ncols, 1)
# For conv1d statement: 
input_shape = (ncols, 1)

For example, taking first 4 features of iris dataset:

To see usual format and its shape:

iris_array = np.array(irisdf.iloc[:,:4].values)
print(iris_array[:5])
print(iris_array.shape)

The output shows usual format and its shape:

[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]]

(150, 4)

Following code alters the format:

nrows, ncols = iris_array.shape
iris_array = iris_array.reshape(nrows, ncols, 1)
print(iris_array[:5])
print(iris_array.shape)

Output of above code data format and its shape:

[[[5.1]
  [3.5]
  [1.4]
  [0.2]]

 [[4.9]
  [3. ]
  [1.4]
  [0.2]]

 [[4.7]
  [3.2]
  [1.3]
  [0.2]]

 [[4.6]
  [3.1]
  [1.5]
  [0.2]]

 [[5. ]
  [3.6]
  [1.4]
  [0.2]]]

(150, 4, 1)

This works well for Conv1d of Keras. For input_shape (4,1) is needed.

like image 36
rnso Avatar answered Nov 04 '22 22:11

rnso