title: TensorFlow input shape error at Dense output layer is contradictory to what model.summary() says
body: <p>I am playing around with an NLP problem (sentence classification) and decided to use HuggingFace's TFBertModel along with Conv1D, Flatten, and Dense layers. I am using the functional API and my model compiles. However, during model.fit(), I get a shape error at the output Dense layer.</p>
  <p>Model definition:</p>
  <pre><code># Build model with a max length of 50 words in a sentence
  max_len = 50
  def build_model():
      bert_encoder = TFBertModel.from_pretrained(model_name)
      input_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name=&quot;input_word_ids&quot;)
      input_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name=&quot;input_mask&quot;)
      input_type_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name=&quot;input_type_ids&quot;)
      
      # Create a conv1d model. The model may not really be useful or make sense, but that's OK (for now).
      embedding = bert_encoder([input_word_ids, input_mask, input_type_ids])[0]
      conv_layer = tf.keras.layers.Conv1D(32, 3, activation='relu')(embedding)
      dense_layer = tf.keras.layers.Dense(24, activation='relu')(conv_layer)
      flatten_layer = tf.keras.layers.Flatten()(dense_layer)
      output_layer = tf.keras.layers.Dense(3, activation='softmax')(flatten_layer)
      
      model = tf.keras.Model(inputs=[input_word_ids, input_mask, input_type_ids], outputs=output_layer)
      model.compile(tf.keras.optimizers.Adam(lr=1e-5), loss='sparse_categorical_crossentropy', 
                    metrics=['accuracy'])
      return model
  
  # View model architecture
  model = build_model()
  model.summary()
  
  Model: &quot;model&quot;
  __________________________________________________________________________________________________
  Layer (type)                    Output Shape         Param #     Connected to                     
  ==================================================================================================
  input_word_ids (InputLayer)     [(None, 50)]         0                                            
  __________________________________________________________________________________________________
  input_mask (InputLayer)         [(None, 50)]         0                                            
  __________________________________________________________________________________________________
  input_type_ids (InputLayer)     [(None, 50)]         0                                            
  __________________________________________________________________________________________________
  tf_bert_model (TFBertModel)     ((None, 50, 768), (N 177853440   input_word_ids[0][0]             
                                                                   input_mask[0][0]                 
                                                                   input_type_ids[0][0]             
  __________________________________________________________________________________________________
  conv1d (Conv1D)                 (None, 48, 32)       73760       tf_bert_model[0][0]              
  __________________________________________________________________________________________________
  dense (Dense)                   (None, 48, 24)       792         conv1d[0][0]                     
  __________________________________________________________________________________________________
  flatten (Flatten)               (None, 1152)         0           dense[0][0]                      
  __________________________________________________________________________________________________
  dense_1 (Dense)                 (None, 3)            3459        flatten[0][0]                    
  ==================================================================================================
  Total params: 177,931,451
  Trainable params: 177,931,451
  Non-trainable params: 0
  __________________________________________________________________________________________________
  
  # Fit model on input data
  model.fit(train_input, train['label'].values, epochs = 3, verbose = 1, batch_size = 16, 
            validation_split = 0.2)
  </code></pre>
  <p>And this is the error message:</p>
  <blockquote>
  <p>ValueError: Input 0 of layer dense_1 is incompatible with the layer: expected axis -1 of input shape to have value 1152 but received
  input with shape [16, 6168]</p>
  </blockquote>
  <p>I am unable to understand how the input shape to layer dense_1 (the output dense layer) can be 6168? As per the model summary, it should always be 1152.</p>