【问题标题】:Invalid argument error when using tf.reshape()使用 tf.reshape() 时出现无效参数错误
【发布时间】:2018-07-05 06:18:25
【问题描述】:

我正在使用一些关于卷积神经网络的 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 的张量来自哪里。

【问题讨论】:

  • 你没有告诉我们你是怎么打电话给convolutional_neural_network(x)

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


【解决方案1】:

您没有向我们展示您是如何准确执行卷积和池化的,但很可能是 conv2d 中的 strides=[1, 1, 1, 1]maxpool2d 中的 strides=[1, 2, 2, 1],以及它们两者中的 padding='SAME'

在这种情况下,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,这样代码对输入图像大小以及卷积和池化设置的鲁棒性更强。

【讨论】:

    猜你喜欢
    • 2016-08-18
    • 1970-01-01
    • 2012-08-28
    • 2020-06-30
    • 1970-01-01
    • 2013-10-08
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多