【问题标题】:Tensorflow: TypeError with numpy_input_fnTensorflow:带有 numpy_input_fn 的 TypeError
【发布时间】:2018-02-18 17:18:58
【问题描述】:

我正在编写一个卷积神经网络来在 TensorFlow 中对图像进行分类,但是有一个问题:

当我尝试将我的 NumPy 平面图像数组(RGB 值从 0 到 255 的 3 个通道)输入到 tf.estimator.inputs.numpy_input_fn 时,我收到以下错误:

  TypeError: Failed to convert object of type <class 'dict'> to Tensor. 
  Contents: {'x': <tf.Tensor 'random_shuffle_queue_DequeueMany:1' shape=(8, 
  196608) dtype=uint8>}. Consider casting elements to a supported type.

我的 numpy_imput_fn 看起来像这样:

train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={'x': train_x},
    y=train_y,
    batch_size=8,
    num_epochs=None,
    shuffle=True)

在函数的文档中说 x 应该是 NumPy 数组的字典:

x: numpy 数组对象的字典。

【问题讨论】:

    标签: numpy tensorflow typeerror


    【解决方案1】:

    没关系,对于那些有同样问题的人,我已经解决了。在我的模型函数中,我有:

    input_layer = tf.reshape(features, [-1, 256, 256, 1])
    

    这引发了类型错误。要修复它,您必须访问功能字典中的“x”键:

    input_layer = tf.reshape(features['x'], [-1, 256, 256, 1]) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-09
      • 2020-08-13
      • 1970-01-01
      • 2023-04-05
      • 2020-11-11
      • 2014-05-08
      • 2021-03-10
      • 1970-01-01
      相关资源
      最近更新 更多