相关疑难解决方法(0)

TypeError:使用带有估计器input_fn的数据集不支持可调用

我正在尝试转换Iris教程(https://www.tensorflow.org/get_started/estimator)以从.png文件而不是.csv中读取训练数据.它可以使用,numpy_input_fn但不是我从它做的时候Dataset.我认为input_fn()是返回了错误的类型,但并不真正理解它应该是什么以及如何实现它.错误是:

  File "iris_minimal.py", line 27, in <module>
    model_fn().train(input_fn(), steps=1)
    ...
    raise TypeError('unsupported callable') from ex
TypeError: unsupported callable
Run Code Online (Sandbox Code Playgroud)

TensorFlow版本是1.3.完整代码:

import tensorflow as tf
from tensorflow.contrib.data import Dataset, Iterator

NUM_CLASSES = 3

def model_fn():
    feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]
    return tf.estimator.DNNClassifier([10, 20, 10], feature_columns, "tmp/iris_model", NUM_CLASSES)

def input_parser(img_path, label):
    one_hot = tf.one_hot(label, NUM_CLASSES)
    file_contents = tf.read_file(img_path)
    image_decoded = tf.image.decode_png(file_contents, channels=1)
    image_decoded = tf.image.resize_images(image_decoded, [2, 2])
    image_decoded = tf.reshape(image_decoded, [4])
    return image_decoded, one_hot      

def input_fn(): …
Run Code Online (Sandbox Code Playgroud)

python python-3.x deep-learning tensorflow tensorflow-datasets

4
推荐指数
1
解决办法
3557
查看次数