tensorflow.python.framework.errors.InvalidArgumentError:重塑的输入是一个带xxx值的张量,但请求的形状需要倍数

时间:2016-12-04 03:59:12

标签: tensorflow

我正在通过TensorFlow实现此目标(https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py)。我的代码如下。

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
    strides=[1, 2, 2, 1], padding='SAME')

if __name__ == '__main__':
    mnist = input_data.read_data_sets('data', one_hot=True)
    x = tf.placeholder("float", shape=[None, 784])
    y_ = tf.placeholder("float", shape=[None, 10])
    sess = tf.InteractiveSession()

    x_image = tf.reshape(x, [-1,28,28,1])

    W_conv1 = weight_variable([3, 3, 1, 32])
    b_conv1 = bias_variable([32])
    W_conv2 = weight_variable([3, 3, 32, 32])
    b_conv2 = bias_variable([32])
    W_fc1 = weight_variable([12*12*32, 128])
    b_fc1 = bias_variable([128])
    W_fc2 = weight_variable([128, 10])
    b_fc2 = bias_variable([10])

    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)
    h_pool = max_pool_2x2(h_conv2)
    keep_prob1 = tf.placeholder("float")
    h_drop1 = tf.nn.dropout(h_pool, keep_prob1)
    h_flat = tf.reshape(h_drop1, [-1, 12*12*32])
    h_fc1 = tf.nn.relu(tf.matmul(h_flat, W_fc1) + b_fc1)
    keep_prob2 = tf.placeholder("float")
    h_drop2 = tf.nn.dropout(h_fc1, keep_prob2)

    y_conv = tf.nn.softmax(tf.matmul(h_drop2, W_fc2) + b_fc2)

    cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    sess.run(tf.initialize_all_variables())
    for i in range(20000):
        batch = mnist.train.next_batch(50)
        if i%100 == 0:
            train_accuracy = accuracy.eval(feed_dict={
                x: batch[0], y_: batch[1], keep_prob1: 1.0, keep_prob2: 1.0})
            print("step %d, training accuracy %g"%(i, train_accuracy))
        train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob1: 0.25, keep_prob2: 0.5})

    print("test accuracy %g"%accuracy.eval(feed_dict={
        x: mnist.test.images, y_: mnist.test.labels, keep_prob1: 1.0, keep_prob2: 1.0}))

当我跑步时,在展平图层上发生以下错误。我试图匹配输入形状,但错误信息表示"一个313600值的张量"我不知道它来自哪里。

Traceback (most recent call last):
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 972, in _do_call
    return fn(*args)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 954, in _run_fn
    status, run_metadata)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 313600 values, but the requested shape requires a multiple of 4608
     [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](dropout/mul, Reshape_1/shape)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mnist_tensorflow.py", line 60, in <module>
    x: batch[0], y_: batch[1], keep_prob1: 1.0, keep_prob2: 1.0})
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 559, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3761, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 313600 values, but the requested shape requires a multiple of 4608
     [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](dropout/mul, Reshape_1/shape)]]

Caused by op 'Reshape_1', defined at:
  File "mnist_tensorflow.py", line 44, in <module>
    h_flat = tf.reshape(h_drop1, [-1, 12*12*32])
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1977, in reshape
    name=name)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
    op_def=op_def)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 313600 values, but the requested shape requires a multiple of 4608
     [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](dropout/mul, Reshape_1/shape)]]

我已经检查了类似的问题(Error: Tensorflow CNN dimension),我确认我定义了目标图像以进行整形/展平。如果您有任何想法,请告诉我。

1 个答案:

答案 0 :(得分:0)

错误非常简单。我发现我应该设置'有效',而不是&#39;相同&#39;在conv2中,这样我可以在平整操作之前制作12,12,32形状。谢谢乌龟帮助我。