Tensorflow 错误:不受支持的可调用 [英] Tensorflow error : unsupported callable

查看:32
本文介绍了Tensorflow 错误:不受支持的可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照教程 https://www.tensorflow.org/tutorials/layers 和我想用它来使用我自己的数据集.

I follow the tutorial https://www.tensorflow.org/tutorials/layers and I want use it to use my own dataset.

def train_input_fn_custom(filenames_array, labels_array, batch_size):
    # Reads an image from a file, decodes it into a dense tensor, and resizes it to a fixed shape.
    def _parse_function(filename, label):
        image_string = tf.read_file(filename)
        image_decoded = tf.image.decode_png(image_string, channels=1)
        image_resized = tf.image.resize_images(image_decoded, [40, 40])
        return image_resized, label

    filenames = tf.constant(filenames_array)
    labels = tf.constant(labels_array)

    dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
    dataset = dataset.map(_parse_function)
    dataset = dataset.shuffle(1000).repeat().batch(batch_size)

    return dataset.make_one_shot_iterator().get_next()


def main(self):
    tf.logging.set_verbosity(tf.logging.INFO)

    # Get data
    filenames_train = ['blackcorner-data/1.png', 'blackcorner-data/2.png']
    labels_train = [0, 1]

    # Create the Estimator
    classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/test_convnet_model")

    # Set up logging for predictions
    tensors_to_log = {"probabilities": "softmax_tensor"}
    logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50)

    # Train the model
    cust_train_input_fn = train_input_fn_custom(
            filenames_array=filenames_train,
            labels_array=labels_train,
            batch_size=3)

    classifier.train(
            input_fn=cust_train_input_fn,
            steps=2000,
            hooks=[logging_hook])


if __name__ == "__main__":
    tf.app.run()

但是我有这个错误:

    Traceback (most recent call last):
      File "/usr/lib/python3.6/inspect.py", line 1119, in getfullargspec
        sigcls=Signature)
      File "/usr/lib/python3.6/inspect.py", line 2186, in _signature_from_callable
        raise TypeError('{!r} is not a callable object'.format(obj))
    TypeError: (<tf.Tensor 'IteratorGetNext:0' shape=(?, 40, 40, ?) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(?,) dtype=int32>) is not a callable object

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "cnn_mnist_for_stackoverflow.py", line 139, in <module>
        tf.app.run()
      File "/home/geo/Projet/ML/cnn_mnist/venv/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 126, in run
        _sys.exit(main(argv))
      File "cnn_mnist_for_stackoverflow.py", line 135, in main
        hooks=[logging_hook])
     ...
        raise TypeError('unsupported callable') from ex
    TypeError: unsupported callable

我不明白这个错误,我只知道它来自 train_input_fn_custom.tensorflow 版本为 1.6

I don't understand this error, I just know it came from the train_input_fn_custom. The tensorflow version is 1.6

如果有人有想法..谢谢!

if anyone has an idea.. Thanks !

推荐答案

classifier.train()input_fn 参数必须是可调用对象(没有参数),例如函数或 lambda.在您的代码中,您传递的是调用 train_input_fn_custom()结果,而不是调用 train_input_fn_custom().要解决此问题,请将 cust_train_input_fn 的定义替换如下:

The input_fn argument to classifier.train() must be a callable object (with no arguments), such as a function or a lambda. In your code, you are passing the results of calling train_input_fn_custom(), rather than a callable object that invokes train_input_fn_custom(). To fix this issue, replace the definition of cust_train_input_fn as follows:

# The `lambda:` creates a callable object with no arguments.
cust_train_input_fn = lambda: train_input_fn_custom(
    filenames_array=filenames_train, labels_array=labels_train, batch_size=3)

这篇关于Tensorflow 错误:不受支持的可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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