我是新手,正在尝试学习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']
3. 定义输入函数以传递给估计器。
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__() 仅在启用急切执行时受支持
- 在实际的tf源代码中放置打印语句以了解为什么context.executing_eagerly()被设置为False。在context.py中,default_execution_mode由EAGER_MODE初始化,所以我对它为什么变成False感到困惑
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/python/data/ops/dataset_ops.py
这是我的第一篇StackOverflow提问,请见谅如果我没有遵守任何指导方针或规则。非常感谢您的帮助。谢谢。