RuntimeError: __iter__() 仅在 tf.function 内部或在启用急切执行时受支持 [英] RuntimeError: __iter__() is only supported inside of tf.function or when eager execution is enabled

查看:109
本文介绍了RuntimeError: __iter__() 仅在 tf.function 内部或在启用急切执行时受支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 tensorflow 的新手并试图学习它.尝试在 Tensorflow 2.2.0 中运行估算器 LinearClassifier.

I am new to tensorflow and trying to learn it. Trying to run an estimator LinearClassifier in Tensorflow 2.2.0.

  1. 导入所有模块并读入 tfRecords

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 + "/" + name) for name in os.listdir(path) if name.startswith("part")]
print (filenames)

  1. 定义解析函数

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']

  1. 定义输入函数以传入估计器

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

  1. 初始化估算器

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
)

运行以下代码报错:

Traceback (most recent call last):
  File "linear_classification.py", line 42, in <module>
    input_fn = input_fn
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 349, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1182, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1208, in _train_model_default
    self._get_features_and_labels_from_input_fn(input_fn, ModeKeys.TRAIN))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1044, in _get_features_and_labels_from_input_fn
    self._call_input_fn(input_fn, mode))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1137, in _call_input_fn
    return input_fn(**kwargs)
  File "linear_classification.py", line 31, in input_fn
    iterator = iter(dataset)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 406, in __iter__
    raise RuntimeError("__iter__() is only supported inside of tf.function "
RuntimeError: __iter__() is only supported inside of tf.function or when eager execution is enabled.

我尝试过的事情:

  1. 强制立即执行(即使在 tf 2 中也是默认执行的).
  2. 尝试搜索现有的 StackOverflow:TensorFlow 2.0 数据集.__iter__() 仅在启用 Eager Execution 时支持
  3. 将打印语句放入实际的 tf 源代码中,以了解为什么 context.executing_eagerly() 设置为 False.context.py 中的 default_execution_mode 是由 EAGER_MODE 初始化的,所以我很困惑为什么它变成了 False
  1. forcing eager execution (even tho in tf 2 it is done by default).
  2. Trying to search existing StackOverflow: TensorFlow 2.0 dataset.__iter__() is only supported when eager execution is enabled
  3. Put print statements in actual tf source code to understand why context.executing_eagerly() is setting to False. The default_execution_mode in context.py is initialized by EAGER_MODE, so I am confused to why it becomes False

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py

这是我的第一个 StackOverflow 问题,如果我没有遵循任何指导方针或规则,请原谅.任何帮助深表感谢.谢谢.

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.

推荐答案

所以我想出了问题所在.由于错误状态 RuntimeError: __iter__() 仅在 tf.function 内部或在启用了急切执行时才受支持.我将 @tf.function 放在我的 input_fn() 之上.所以现在我的 input_fn() 看起来像:

So I figured out the issue is. As the error states RuntimeError: __iter__() is only supported inside of tf.function or when eager execution is enabled. I put the @tf.function above my input_fn(). So now my input_fn() looks like:

@tf.function
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

我能够通过阅读 TensorFlow 文档来跟踪问题:https://www.tensorflow.org/guide/effective_tf2

I was able to track the issue by reading the TensorFlow documentation: https://www.tensorflow.org/guide/effective_tf2

这篇关于RuntimeError: __iter__() 仅在 tf.function 内部或在启用急切执行时受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆