slim.fully_connected:使用Tensor.shape指定`num_outputs`

slim.fully_connected: Using Tensor.shape to specify `num_outputs`

我想用 tf.slim.fully_connected 做这样的事:

conv_out = conv2d(...)
_, h, w, c = conv_out.shape
flat = tf.reshape(conv_out, [-1, h*w*c])
fc_out = fully_connected(flat, h*w*c)

但是,当我这样做时,出现错误:

ValueError: num_outputs should be int or long, got 49.

h*w*c 属于 tensorflow.python.framework.tensor_shape.Dimension.

类型

有没有办法在事先不知道 whc 并且不必启动会话来确定它们的情况下做到这一点?

h*w*c is of type tensorflow.python.framework.tensor_shape.Dimension.

正确,但 slim.fully_connected 检查 isinstance(num_outputs, six.integer_types)。它不期望 Dimension 实例。

这就是为什么您应该手动将 h*w*c 转换为 int:

fc_out = fully_connected(flat, int(h*w*c))