gpt4 book ai didi

tensorflow - RuntimeError : __iter__() is only supported inside of tf. 函数或启用急切执行时

转载 作者:行者123 更新时间:2023-12-04 00:55:02 28 4
gpt4 key购买 nike

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

  • 导入所有模块并读取 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)
  • 定义解析函数
  • 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']
  • 定义要传入估计器的输入函数
  • 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
  • 初始化估计器
  • 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.
    我尝试过的事情:
  • 强制执行急切(即使在 tf 2 中也是默认完成的)。
  • 尝试搜索现有 StackOverflow:TensorFlow 2.0 dataset.__iter__() is only supported when eager execution is enabled
  • 将打印语句放在实际的 tf 源代码中,以了解原因 context.executing_eagerly() 设置为 False。 default_execution_mode 在 context.py 中由 EAGER_MODE 初始化,所以我很困惑为什么它变成 False
  • /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py
    这是我的第一个 StackOverflow 问题,如果我没有遵循任何准则或规则,请原谅。任何帮助深表感谢。谢谢你。

    最佳答案

    所以我发现问题是。正如错误状态 RuntimeError: __iter__() is only supported inside of tf.function or when eager execution is enabled .我把 @tf.function高于我的input_fn() .所以现在我的input_fn()好像:

    @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

    关于tensorflow - RuntimeError : __iter__() is only supported inside of tf. 函数或启用急切执行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63182524/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com