title: RuntimeError: __iter__() is only supported inside of tf.function or when eager execution is enabled
body: <p>I am new to tensorflow and trying to learn it. Trying to run an estimator LinearClassifier in Tensorflow 2.2.0.</p>
  <ol>
  <li>Imported all the modules and read in tfRecords</li>
  </ol>
  <pre><code>import tensorflow as tf
  print(tf.version.VERSION)
  import os
  
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  print (tf.executing_eagerly())
  tf.executing_eagerly()
  tf.compat.v1.enable_eager_execution()
  
  path = 'train.tfrecord'
  filenames = [(path + &quot;/&quot; + name) for name in os.listdir(path) if name.startswith(&quot;part&quot;)]
  print (filenames)
  </code></pre>
  <ol start="2">
  <li>Define the parse function</li>
  </ol>
  <pre><code>def _parse_function(example_proto):
      features = {
          'Age': tf.io.FixedLenFeature([], tf.string),
          'EstimatedSalary': tf.io.FixedLenFeature([], tf.string),
          'Purchased': tf.io.FixedLenFeature([], tf.string)
      }
      tf_records = tf.io.parse_single_example(example_proto, features)
      features_dict = {
          'Age': tf_records['Age'],
          'EstimatedSalary': tf_records['EstimatedSalary']
      }
      return features_dict, tf_records['Purchased']
  </code></pre>
  <ol start="3">
  <li>Define the input function to pass in the estimator</li>
  </ol>
  <pre><code>def input_fn():
      dataset = tf.data.TFRecordDataset(filenames = filenames)
      
      dataset = dataset.map(_parse_function)
      iterator = iter(dataset)
      next_element = iterator.get_next()
      return next_element
  </code></pre>
  <ol start="4">
  <li>Initializing estimator</li>
  </ol>
  <pre><code>feature_columns = [
      tf.feature_column.numeric_column('Age'),
      tf.feature_column.numeric_column('EstimatedSalary')
  ]
  
  estimator = tf.estimator.LinearClassifier(feature_columns = feature_columns)
  estimator.train(
      input_fn = input_fn
  )
  </code></pre>
  <p>Running the following code gives an error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;linear_classification.py&quot;, line 42, in &lt;module&gt;
      input_fn = input_fn
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py&quot;, line 349, in train
      loss = self._train_model(input_fn, hooks, saving_listeners)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py&quot;, line 1182, in _train_model
      return self._train_model_default(input_fn, hooks, saving_listeners)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py&quot;, line 1208, in _train_model_default
      self._get_features_and_labels_from_input_fn(input_fn, ModeKeys.TRAIN))
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py&quot;, line 1044, in _get_features_and_labels_from_input_fn
      self._call_input_fn(input_fn, mode))
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py&quot;, line 1137, in _call_input_fn
      return input_fn(**kwargs)
    File &quot;linear_classification.py&quot;, line 31, in input_fn
      iterator = iter(dataset)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py&quot;, line 406, in __iter__
      raise RuntimeError(&quot;__iter__() is only supported inside of tf.function &quot;
  RuntimeError: __iter__() is only supported inside of tf.function or when eager execution is enabled.
  </code></pre>
  <p>Things I tried:</p>
  <ol>
  <li>forcing eager execution (even tho in tf 2 it is done by default).</li>
  <li>Trying to search existing StackOverflow: <a href="https://stackoverflow.com/questions/55576133/tensorflow-2-0-dataset-iter-is-only-supported-when-eager-execution-is-enab/56736763#56736763">TensorFlow 2.0 dataset.__iter__() is only supported when eager execution is enabled</a></li>
  <li>Put print statements in actual tf source code to understand why <strong>context.executing_eagerly()</strong> is setting to False. The <strong>default_execution_mode</strong> in context.py is initialized by EAGER_MODE, so I am confused to why it becomes False</li>
  </ol>
  <pre><code>/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py
  </code></pre>
  <p>This is my first StackOverflow question, so please excuse if I have not followed any guidelines or rules. Any help is much appreciated. Thank you.</p>