【tensorFlow】tf.reshape()报错信息 - TypeError: Expected binary or unicode string, got -1

在使用tensorflow训练CNN时,通常会通过将输入设置成一个placeholder,如下所示:

image_dims = [224, 224, 3]
inputs = tf.placeholder(tf.float32, [None] + image_dims, name='input_images')

其中None所在位置,表示训练网络时,一个迭代需要用到多少个样本,也是就batch_size,这一维可以不用明确指定,主要是方便后续测试采用不同的数目如[1, 224, 224, 3](一个样本)。

但此时如果在网络定义中使用了tf.reshape函数的话(全卷积层中用到),就很容易报错,如下代码

resh0 = tf.reshape(h0, [h0.shape[0], -1])

此时报错TypeError: Expected binary or unicode string, got -1

需要将上述代码改为

resh0 = tf.reshape(h0, [-1, h0.get_shape().as_list()[1] * h0.get_shape().as_list()[2] *
                                    h0.get_shape().as_list()[3]])

其中需要使用.as_list()将获取到的shape转换成list才行。


Reference
stackoverflow:Tensorflow reshape on convolution output gives TypeError

猜你喜欢

转载自blog.csdn.net/yideqianfenzhiyi/article/details/79464725