1

I'm trying to make some LSTM+CNN hybrid for my college project and here's my code

def model_robo():
  grid=tf.keras.Input(shape=(1,10,12),dtype=tf.float32)
  print(grid.shape)
  cnn_result=tf.keras.layers.TimeDistributed(Conv2D(1,kernel_size=(3,3),data_format="channels_first"))(grid)
  cnn_result=tf.keras.layers.TimeDistributed(MaxPooling2D(2,2))(cnn_result)
  cnn_result=tf.keras.layers.TimeDistributed(flatten())(cnn_result)
  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])
  optim=tf.keras.optimizers.Adam(lr=0.001,amsgrad=False)
  model.compile(optimizer=optim,loss='mae')
  return model

but when I'm trying to call the model with

model=model_robo()

it gives:

ValueError: The channel dimension of the inputs should be defined. Found None

which stems from:

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

I've tried searching for answer but I think most error comes from the images format like here or here while I don't really use one (the CNN input is numerical matrix put together to form an "image"). the would-be input is 2159x1x10x12 matrix and the grid.shape result is (none,1,10,12)

how can i fix this? let me know if you need additional information and thanks!

Kaveh
  • 4,618
  • 2
  • 20
  • 33
  • I'm not sure it is a bug or something else, but to get rid of `channels_first`, you may use reshape layer like `grid = tf.keras.layers.Reshape((10,12,1))(grid)`. Then you can remove `channels_first` argument. – Kaveh Aug 30 '21 at 08:25
  • 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)` now but it probably works. It delete the second dimension for some reason. I'll make a new thread once I properly read the existing solutions. thanks – Ikhwan Nuttaqwa Aug 30 '21 at 10:00

1 Answers1

1

It works after I delete the channels_first (and changed it to Conv1D but that's another matters. see here for detail)

My hypothesis is channels_first took the first dimension (None) as the channel instead of the second one. API docs said first channel should be reserved for batch size but like the comment said, it's probably a bug