title: Combine feature and labels to correctly produce tf dataset for model.fit (Using tf.data.Dataset.from_tensor_slices)
body: <p>I have created a model that takes an input of shape (None, 512). Below is the summary of my model</p>
  <p><a href="https://i.stack.imgur.com/TrkLW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TrkLW.png" alt="enter image description here" /></a></p>
  <h3>shape of training feature</h3>
  <pre><code>train_ids.shape
  (10, 512)
  </code></pre>
  <h3>shape of the training response variable</h3>
  <pre><code>indus_cat_train.shape
  (10, 49)
  </code></pre>
  <p>My model runs perfectly if I use</p>
  <pre><code>history = model.fit(
      train_ids, indus_cat_train, epochs=2, validation_data=(
          valid_ids, indus_cat_valid))
  </code></pre>
  <h3>However my actual dataset is very large and feeding the completed dataset all at once is consuming so much RAM and shut down all the process.</h3>
  <p>I want to feed all data in batches or one by one. In order to complete this task, I tried out tf.data.Dataset.from_tensor_slices function</p>
  <pre><code># training data
  tf_train_data = tf.data.Dataset.from_tensor_slices((train_ids, indus_cat_train))
  
  # validation data
  tf_valid_data = tf.data.Dataset.from_tensor_slices((valid_ids, indus_cat_valid))
  </code></pre>
  <p>The above code is running fine and upon inspection, it is giving the desired shape</p>
  <pre><code>for elem in t:
      print(elem[0].shape) # for features
      print(elem[1].shape) # for response
  </code></pre>
  print output
  <pre><code>    (512,) # for features
      (49,)  # for response variable
  # terminating all other output to save space
  </code></pre>
  <p>However on-calling model.fit on tf_train_dataset the model gives me an error</p>
  <pre><code>bert_history = model.fit(
      tf_train_data, epochs=2, validation_data=tf_valid_data)
  </code></pre>
  <p><em><strong>WARNING:tensorflow:Model was constructed with shape (None, 512) for input Tensor(&quot;input_ids_1:0&quot;, shape=(None, 512), dtype=int32), but it was called on an input with incompatible shape (512, 1).</strong></em></p>
  Sharing model code for further understanding as asked by Prateek
  <pre><code># training data
  
  tf_train_data = tf.data.Dataset.from_tensor_slices((train_ids, indus_cat_train))
  
  # validation data
  
  tf_valid_data = tf.data.Dataset.from_tensor_slices((valid_ids, indus_cat_valid))
  
  
  
  
  
  # model downloaded from bert
  
  bert_model_name = &quot;uncased_L-12_H-768_A-12&quot;
  
  bert_ckpt_dir = &quot;bert_model&quot;
  
  bert_ckpt_file = os.path.join(bert_ckpt_dir, &quot;bert_model.ckpt&quot;)
  
  bert_config_file = os.path.join(bert_ckpt_dir, &quot;bert_config.json&quot;)
  
  
  
  # creating tokenizer
  
  tokenizer = FullTokenizer(vocab_file=os.path.join(bert_ckpt_dir, &quot;vocab.txt&quot;))
  
  
  
  # create function for model
  
  def create_model(max_seq_len, bert_ckpt_file, n_classes):
  
      with tf.io.gfile.GFile(bert_config_file, &quot;r&quot;) as reader:
  
          # get bert configurations
  
          bert_configurations = StockBertConfig.from_json_string(reader.read())
  
          bert_params = map_stock_config_to_params(bert_configurations)
  
          bert_params_adapter_size = None
  
          bert = BertModelLayer.from_params(bert_params, name=&quot;bert&quot;)
  
  
  
      input_ids = keras.layers.Input(shape=(max_seq_len,), dtype=&quot;int32&quot;,
  
                                     name=&quot;input_ids&quot;)
  
  
  
      bert_output = bert(input_ids)
  
      print(&quot;bert shape&quot;, bert_output.shape)
  
  
  
      cls_out = keras.layers.Lambda(lambda seq: seq[:, 0, :])(bert_output)
  
      cls_out = keras.layers.Dropout(0.5)(cls_out)
  
      logits = keras.layers.Dense(units=765, activation=&quot;tanh&quot;)(cls_out)
  
      logits = keras.layers.Dropout(0.5)(logits)
  
      logits = keras.layers.Dense(
  
          units=n_classes, activation=&quot;softmax&quot;)(logits)
  
  
  
      model = keras.Model(inputs=input_ids, outputs=logits)
  
      model.build(input_shape=(None, max_seq_len))
  
      load_stock_weights(bert, bert_ckpt_file)
  
      return model
  
  
  
  
  
  n_cats = 49 #number of output categories
  
  model = create_model(max_seq_len=512, bert_ckpt_file=bert_ckpt_file,
  
                       n_classes=n_cats)
  
  
  
  model.summary()
  
  optimizer = tf.keras.optimizers.Adam(    learning_rate=learning_rate, epsilon=1e-08)
  
  
  
  loss = tf.keras.losses.CategoricalCrossentropy()metric = tf.keras.metrics.CategoricalCrossentropy(    name='categorical_crossentropy')model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
  
  
  
  bert_history = model.fit( tf_train_data, epochs=2, validation_data=tf_valid_data)
  </code></pre>