title: Tensorflow 2.0: how to transform from MapDataset (after reading from TFRecord) to some structure that can be input to model.fit
body: <p>I've stored my training and validation data on two separate TFRecord files, in which I store 4 values: signal A (float32 shape (150,)), signal B (float32 shape (150,)), label (scalar int64), id (string). My parsing function for reading is:</p>
  
  <pre class="lang-py prettyprint-override"><code>def _parse_data_function(sample_proto):
  
      raw_signal_description = {
          'label': tf.io.FixedLenFeature([], tf.int64),
          'id': tf.io.FixedLenFeature([], tf.string),
      }
  
      for key, item in SIGNALS.items():
          raw_signal_description[key] = tf.io.FixedLenFeature(item, tf.float32)
  
      # Parse the input tf.Example proto using the dictionary above.
      return tf.io.parse_single_example(sample_proto, raw_signal_description)
  </code></pre>
  
  <p>where <code>SIGNALS</code> is a dictionary mapping <em>signal name</em>-><em>signal shape</em>. Then, I read the raw datasets:</p>
  
  <pre class="lang-py prettyprint-override"><code>training_raw = tf.data.TFRecordDataset(&lt;path to training&gt;), compression_type='GZIP')
  val_raw = tf.data.TFRecordDataset(&lt;path to validation&gt;), compression_type='GZIP')
  </code></pre>
  
  <p>and use map to parse the values:</p>
  
  <pre class="lang-py prettyprint-override"><code>training_data = training_raw.map(_parse_data_function)
  val_data = val_raw.map(_parse_data_function)
  </code></pre>
  
  <p>Displaying the header of <code>training_data</code> or <code>val_data</code>, I get:</p>
  
  <p><code>&lt;MapDataset shapes: {Signal A: (150,), Signal B: (150,), id: (), label: ()}, types: {Signal A: tf.float32, Signal B: tf.float32, id: tf.string, label: tf.int64}&gt;</code></p>
  
  <p>which is pretty much as expected. I also checked some of the values for consistency and they seemed to be correct.</p>
  
  <p>Now, to my issue: <strong>how do I get from the MapDataset, with the dictionary like structure, to something that can be given as input to the model?</strong></p>
  
  <p>The input to my model is the pair (Signal A, label), though in the future I will use Signal B as well. </p>
  
  <p>The simplest way to me seemed to create an generator over the elements that I want. Something like:</p>
  
  <pre class="lang-py prettyprint-override"><code>def data_generator(mapdataset):
      for sample in mapdataset:
          yield (sample['Signal A'], sample['label'])
  </code></pre>
  
  <p>However, with this approach I lose some convenience of Datasets, such as batching, and it is also not clear how to use the same approach for the <code>validation_data</code> paramenter of <code>model.fit</code>. Ideally, I would only convert between the map representation and the Dataset representation where it iterates over pairs of Signal A tensors and labels.</p>
  
  <p>EDIT: My end product should be something with a header akin to:
  <code>&lt;TensorSliceDataset shapes: ((150,), ()), types: (tf.float32, tf.int64)&gt;</code>
  But not necessarily <code>TensorSliceDataset</code></p>