Ikhwan Nuttaqwa
Ikhwan Nuttaqwa

Reputation: 51

Conv2D lost a dimension from tensor. resulting in incompatible dimension error

I'm trying to make some LSTM+CNN hybrid for my college project.

Basically, I have 2 kind of data: a grid which is 10x12 matrix containing 12 technical indicators for stock's price for the last 10 days, and the price itself

The idea is to process the grid with CNN then use the output as LSTM's input along with the price

here's my code

def model_robo():
  grid=tf.keras.Input(shape=(10,12,1),dtype=tf.float32) #grid input
  #processing with CNN
  cnn_result=tf.keras.layers.TimeDistributed(Conv2D(1,kernel_size=(3,3),data_format="channels_first"))(grid) #here's the error 
  cnn_result=tf.keras.layers.TimeDistributed(MaxPooling2D(2,2))(cnn_result)
  cnn_result=tf.keras.layers.TimeDistributed(flatten())(cnn_result)
  #processing with LSTM
  lstm_input=Concatenate()([price,cnn_result])
  masked_position=Masking(mask_value=-1)(lstm_input)
  result=LSTM(50, name='LSTM')(masked_position)
  prediction=(TimeDistributed(Dense(1,activation="relu")))(result)
  model=tf.keras.Model(inputs=[grid,price],outputs=[prediction])
  return model

but when I'm trying to call the model with

model=model_robo()

it gives:

ValueError: Input 0 of layer conv2d_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 12, 1)

which stems from:

cnn_result=tf.keras.layers.TimeDistributed(Conv2D(1,kernel_size=(3,3),data_format="channels_first"))(grid)

I've also tried to put:

grid=tf.keras.layers.Reshape((10,12,1))(grid)

above the error line, no luck.

i've check the grid's dimension with print(grid.shape) and it gives me (None,10,12,1) which I'm sure is the correct dimension but the code delete the second dimension for some reason.

does anyone know how to fix this? let me know if you need additional information and thank you

Upvotes: 2

Views: 230

Answers (1)

Ikhwan Nuttaqwa
Ikhwan Nuttaqwa

Reputation: 51

After reviewing what each function does again I realized I shouldn't use TimeDistributed because each batch already represent one period of time.

changing the line into cnn_result=tf.keras.layers.Conv2D(1,kernel_size=(3,3))(grid) fix the problem.

Upvotes: 1

Related Questions