为什么我必须从`tf.train.batch()`重塑`inputs`以与`slim.fully_connected()一起使用?

时间:2018-03-02 03:06:14

标签: tensorflow tf-slim

为什么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]

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

tf.train.batch()
  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()

之后明确重塑

1 个答案:

答案 0 :(得分:0)

tf.train.batch期望数组像输入,因为标量很少(实际上说)。所以,你必须重塑你的输入。我认为下一个代码片段可以解决问题。

>>> 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]])
>>>