0
$\begingroup$

I am using R programming language and using Keras API to build a functional 1D CNN.

I have a matrix of my dataset of the following shape rows*features (6000*1024).

The input layer is set using the following code:

input_layer = layer_input(shape = 1024, batch_shape = c(nrow(train_matrix),1024), dtype = 'float64') 

and then I am building a 1d conv layer as follows:

conv1 = input_layer %>% layer_conv_1d(filters = 32, kernel_size = 50, strides = 10, input_shape = 1024, batch_input_shape = list(NULL, 1024) ,dtype = 'float64', activation = 'relu' )

But I get the following error:

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Input 0 is incompatible with layer conv1d: expected ndim=3, found ndim=2

I believe it is due to the fact that 1D cnn layer expects the input in the following form

Input shape: 3D tensor with shape: (batch_size, steps, input_dim)

I understand that I have to reshape my data as (NULL, nrow(train_matrix), 1; as this has been suggested in various answer for the same issue arising for keras when used in Python.

If I am right,

  1. what values should I provide to input layer
  2. how should i reshape my training data?
  3. does that mean I have to reshape the test data as well?

also if my understanding is wrong what should be done otherwise ?

$\endgroup$
0
$\begingroup$

The input layer was producing a 2d tensor, whereas 1d convolutional layer expects a 3d tensor as input. A good explanation of as to why keras expects it and how should you reshape your 2d input to 3d can be found in this answer.

I used Keras R API function k_reshape( 2d_tensor, (list_of_new_dims)) --> k_reshape(input_layer, list(nrow(train_matrix), num_of_feature_vectors, 1).

nrow(train_matrix) - the total number of rows in my matrix (no of samples)

num_of_feature_vectors - total number of columns in matrix (total number of features)

1 - i want a 3d tensor with only 1 element at z axis, therefore z axis is initialised to 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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