小编Gri*_*tel的帖子

RuntimeError: __iter__() 仅在 tf.function 内部或在启用急切执行时受支持

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

  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)
Run Code Online (Sandbox Code Playgroud)
  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']
Run Code Online (Sandbox Code Playgroud)
  1. 定义传入估计器的输入函数
def input_fn():
    dataset = tf.data.TFRecordDataset(filenames = filenames) …
Run Code Online (Sandbox Code Playgroud)

tensorflow tensorflow-datasets tensorflow2.0

3
推荐指数
1
解决办法
5323
查看次数