【问题标题】:why do I have to reshape `inputs` from `tf.train.batch()` to use with `slim.fully_connected()?为什么我必须从 `tf.train.batch()` 重塑 `inputs` 才能与 `slim.fully_connected() 一起使用?
【发布时间】:2018-08-10 06:14:24
【问题描述】:

为什么slim.fully_connected() 会出现此错误?

ValueError: Input 0 of layer fc1 is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [32]

我的输入是来自tf.train.batch()Tensor("batch:0", shape=(32,), dtype=float32)

  inputs, labels = tf.train.batch(
        [input, label],
        batch_size=batch_size,
        num_threads=1,
        capacity=2 * batch_size)

如果我将输入重塑为(32,1),它可以正常工作。

inputs, targets = load_batch(train_dataset)
print("inputs:", inputs, "targets:", targets)
# inputs: Tensor("batch:0", shape=(32,), dtype=float32) targets: Tensor("batch:1", shape=(32,), dtype=float32)

inputs = tf.reshape(inputs, [-1,1])
targets = tf.reshape(targets, [-1,1])

slim walkthrough 中的示例似乎在 load_batch() 之后无需显式重塑即可工作

【问题讨论】:

    标签: tensorflow tf-slim


    【解决方案1】:

    tf.train.batch 需要类似数组的输入,因为标量很少见(实际上是这样)。所以,你必须重塑你的输入。我认为下一个代码 sn-p 将清除一切。

    >>> import numpy as np
    >>> a = np.array([1,2,3,4])
    >>> a.shape
    (4,)
    >>> a = np.reshape(a,[4,1])
    >>> a
    array([[1],
           [2],
           [3],
           [4]])
    >>>  
    

    【讨论】:

    • 这就是我的想法,但我的 np.array 看起来像这样 shape x= (5, 1) [[4.6446195] [9.981602 ] [7.1564007] [8.539308 ] [2.0904353]] 并且我正在使用 tf.train.Feature(float_list=tf.train.FloatList(value= inputs[m].tolist() )) 所以我不敢相信它是一个标量数组而不是一个数组数组。
    • 我以前从未使用过tf.train.FloatList,但显然它在保存数据之前正在运行tf.squeeze。 API 文档是空的,必须通过查看代码来验证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    相关资源
    最近更新 更多