使用tf.reshape()时参数错误无效

时间:2018-01-25 21:42:54

标签: python-3.x tensorflow machine-learning artificial-intelligence conv-neural-network

我在卷积神经网络上使用了一些MNIST教程来开发我自己的教程,可以将15x15分类为两个类中的一个。

在定义卷积网络时,我遇到了无效的参数错误,但我无法弄清楚我哪里出错了。以下是我用来定义转发网的代码:

def convolutional_neural_network(x):
weights = {'W_conv1':tf.Variable(tf.random_normal([5,5,1,32])),
           'W_conv2':tf.Variable(tf.random_normal([5,5,32,64])),
           'W_fc':tf.Variable(tf.random_normal([3*3*64,1024])),
           'out':tf.Variable(tf.random_normal([1024, n_classes]))}

biases = {'b_conv1':tf.Variable(tf.random_normal([32])),
           'b_conv2':tf.Variable(tf.random_normal([64])),
           'b_fc':tf.Variable(tf.random_normal([1024])),
           'out':tf.Variable(tf.random_normal([n_classes]))}

x = tf.reshape(x, shape=[-1, 15, 15, 1])

conv1 = tf.nn.relu(conv2d(x, weights['W_conv1']) + biases['b_conv1'])
conv1 = maxpool2d(conv1)

conv2 = tf.nn.relu(conv2d(conv1, weights['W_conv2']) + 
biases['b_conv2'])
conv2 = maxpool2d(conv2)

fc = tf.reshape(conv2,[-1, 3*3*64])
fc = tf.nn.relu(tf.matmul(fc, weights['W_fc'])+biases['b_fc'])
fc = tf.nn.dropout(fc, keep_rate)

output = tf.matmul(fc, weights['out'])+biases['out']

return output

它抛出的错误看起来像这样:

Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1323, in _do_call
    return fn(*args)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1302, in _run_fn
    status, run_metadata)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 4096 values, but the requested shape requires a multiple of 576
     [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](MaxPool_1, Reshape_1/shape)]]

我可以看到tf.reshape()请求张量大小为576的位置,但是我不明白4096的张量来自何处。

1 个答案:

答案 0 :(得分:0)

您没有向我们展示您是如何执行卷积和汇总的,但很可能strides=[1, 1, 1, 1] conv2dstrides=[1, 2, 2, 1] maxpool2d和{{} padding='SAME'两个都是1}}。

在这种情况下,conv1张量的形状为(?, 8, 8, 32),因此conv2张量为(?, 4, 4, 64),而不是(?, 3, 3, 64)。当您传递批量大小等于4时,tensorflow会尝试将(4, 4, 4, 64)重新整形为[-1, 3*3*64]并失败 - 这就是4096来自的位置。

解决方案是使用conv2.shape[1] * conv2.shape[2] * conv2.shape[3]而不是硬编码3*3*64,以便代码对输入图像大小以及卷积和池化设置更加健壮。