Tensorflow 错误:不支持可调用

2023-12-21

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

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。 张量流版本是1.6

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


The input_fn论证classifier.train()必须是可调用对象(不带参数),例如函数或lambda。在您的代码中,您正在传递results的呼唤train_input_fn_custom(),而不是一个可调用对象invokes train_input_fn_custom()。要解决此问题,请将定义替换为cust_train_input_fn如下:

# 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Tensorflow 错误:不支持可调用 的相关文章

  • Python 异常 - args 属性如何自动设置?

    假设我定义了以下异常 gt gt gt class MyError Exception def init self arg1 pass 然后我实例化该类以创建异常对象 gt gt gt e MyError abc gt gt gt e ar
  • sphinx 中的分组方法文档字符串

    是否可以使用 sphinx 的 autodoc 功能将多个方法文档字符串分组 以便将它们列在一起 class Test object def a self A method of group foo def b self A method
  • HoughLinesP后如何合并线?

    My task is to find coordinates of lines startX startY endX endY and rectangles 4 lines Here is input file 我使用下一个代码 img c
  • 查找 python 数据框中每行的最高值

    我想找到每行中的最高值并返回 python 中该值的列标题 例如 我想找到每行的前两个 df A B C D 5 9 8 2 4 1 2 3 我希望我的输出看起来像这样 df B C A D 您可以使用字典理解来生成largest n数据帧
  • 导入错误:没有名为“wordcloud”的模块

    我正在努力将 wordcloud 安装到我的环境中 这是我正在运行的代码 import os import matplotlib pyplot as plt from wordcloud import WordCloud 我收到以下错误 I
  • __getitem__、__setitem__ 如何处理切片?

    我正在运行 Python 2 7 10 我需要拦截列表中的更改 我所说的 更改 是指在浅层意义上修改列表的任何内容 如果列表由相同顺序的相同对象组成 则列表不会更改 无论这些对象的状态如何 否则 它会更改 我不需要找出来how列表已经改变
  • 使用Python进行图像识别[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我有一个想法 就是我想识别图像中的字母 可能是 bmp或 jpg 例如 这是一个包含字母 S 的 bmp 图像 我想做的是使用Pyth
  • 将分布拟合到直方图

    I want to know the distribution of my data points so first I plotted the histogram of my data My histogram looks like th
  • 覆盖现有的 django-admin 命令

    除了编写自定义 django admin 命令之外 这是有详细记录的 https docs djangoproject com en 1 9 howto custom management commands 我希望能够覆盖现有命令 例如ma
  • 为什么我在将数据上传到数据库时不断看到“正在重置断开的连接”?

    我正在通过 REST API 将数亿个项目从 Heroku 上的云服务器上传到 AWS EC2 中的数据库 我正在使用 Python 并且经常在日志中看到以下 INFO 日志消息 requests packages urllib3 conn
  • 获取 HTML 代码的结构

    我正在使用 BeautifulSoup4 我很好奇是否有一个函数可以返回 HTML 代码的结构 有序标签 这是一个例子 h1 Simple example h1 p This is a simple example of html page
  • 如何将 pip 指向 Mercurial 分支?

    我正在尝试通过 pip 将我的应用程序安装到 virtualenv 进行测试 安装时效果很好default or tip像这样 pip install e hg https email protected cdn cgi l email p
  • Bottle 是否可以处理没有并发的请求?

    起初 我认为 Bottle 会并发处理请求 所以我编写了如下测试代码 import json from bottle import Bottle run request response get post import time app B
  • PyPI 上的轮子平台约束有什么限制吗?

    是否有任何地方 PEP 或其他地方 声明关于 Linux 轮子上传范围的限制 PyPI http pypi io 应该有 具体来说 上传是否被认为是可接受的做法linux x86 64轮子到 PyPI 而不是manylinux1 x86 6
  • Python:使用for循环更改变量后缀

    我知道这个问题被问了很多 但到目前为止我无法使用 理解答案 我想改变for循环中变量的后缀 我尝试了 stackoverflow 搜索提供的所有答案 但很难理解提问者经常提出的具体代码 因此 为了清楚起见 我使用一个简单的示例 这并不意味着
  • Docker Build 找不到 pip

    尝试关注一些 1 https aws amazon com blogs aws run docker apps locally using the elastic beanstalk eb cli 2 http docs aws amazo
  • 与 GNU Make 等 Python 相关的并行任务并发

    我正在寻找一种方法或者可能是一种哲学方法来如何在 python 中执行类似 GNU Make 的操作 目前 我们使用 makefile 来执行处理 因为 makefile 非常擅长通过更改单个选项 j x 进行并行运行 此外 gnu mak
  • 为什么 tesseract 无法从这个简单的图像中读取文本?

    我在 pytesseract 上阅读了大量的帖子 但我无法让它从一个简单的图像中读取文本 它返回一个空字符串 这是图像 我尝试过缩放它 灰度化它 调整对比度 阈值 模糊 以及其他帖子中所说的一切 但我的问题是我不知道 OCR 想要更好地工作
  • 在Python中打开网站框架或图像

    所以我对 python 相当熟练 并且经常使用 urllib2 和 Cookies 来实现网站自动化 我刚刚偶然发现了 webbrowser 模块 它可以在默认浏览器中打开一个网址 我想知道是否可以从该 url 中仅选择一个对象并打开它 具
  • 使用Python的线程模块调用ctypes函数比使用多处理更快?

    我一生都无法找出这个问题的答案 我编写了一个可以执行数百次繁重计算的脚本 我有一个绝妙的主意 将这些计算任务编写为 C 然后使用 Python 的 ctypes 与它们交互 我心想 我什至可以使用并行性进一步优化它 我最初的方法是使用线程

随机推荐