Tensorflow – ValueError: ‘limit’ 的形状必须是秩 0,但却是秩 1,对于 ‘range’ 操作(op: ‘Range’),输入形状为:[], [10], []

我最近在学习如何构建一个简单的卷积神经网络。
按照 @ 的教程,我一步一步编写了代码:

from __future__ import print_functionimport tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data# number 1 to 10 datamnist = input_data.read_data_sets('MNIST_data', one_hot=True)def compute_accuracy(v_xs, v_ys):    global prediction    y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1})    correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob:1})    return resultdef 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):    # stride [1, x_movement, y_movement, 1]    # Must have strides[0] = strides[3] = 1    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')def max_pool_2x2(x):    # stride [1, x_movement, y_movement, 1]    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1],padding='SAME')# define placeholder for inputs to networkxs = tf.placeholder(tf.float32, [None, 784]) # 28x28ys = tf.placeholder(tf.float32, [None, 10])keep_prob = tf.placeholder(tf.float32)x_image = tf.reshape(xs, [-1,28,28,1])## conv1 layer ##W_conv1 = weight_variable([5,5,1,32])b_conv1 = bias_variable([32])h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)h_pool1 = max_pool_2x2(h_conv1)## conv2 layer ##W_conv2=weight_variable([5,5,32,64])b_conv2=bias_variable([64])h_conv2=tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)h_pool2=max_pool_2x2(h_conv2)## func1 layer ##W_fc1=weight_variable([7*7*64,1024]) b_fc1=bias_variable([1024])#[n_samples,7,7,64]->>[n_samples,7*7*64]h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64])h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)## func2 layer ##W_fc2=weight_variable([1024,10]) b_fc2=bias_variable([10])prediction=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2),b_fc2)# the error between prediction and real datacross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),                                              reduction_indices=[1]))       # losstrain_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)sess = tf.Session()init = tf.global_variables_initializer()sess.run(init)for i in range(1000):    batch_xs, batch_ys = mnist.train.next_batch(100)    sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5})    if i % 50 == 0:        print(compute_accuracy(            mnist.test.images[:1000], mnist.test.labels[:1000]))

然而,我遇到了一个错误:

runfile('C:/Users/220/tutorials-master/tensorflowTUT/tf18_CNN2/full_code.py', wdir='C:/Users/220/tutorials-master/tensorflowTUT/tf18_CNN2')Extracting MNIST_data\train-images-idx3-ubyte.gzExtracting MNIST_data\train-labels-idx1-ubyte.gzExtracting MNIST_data\t10k-images-idx3-ubyte.gzExtracting MNIST_data\t10k-labels-idx1-ubyte.gzTraceback (most recent call last):  File "<ipython-input-1-b66fc51270cf>", line 1, in <module>    runfile('C:/Users/220/tutorials-master/tensorflowTUT/tf18_CNN2/full_code.py', wdir='C:/Users/220/tutorials-master/tensorflowTUT/tf18_CNN2')  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfileexecfile(filename, namespace)  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfileexec(compile(f.read(), filename, 'exec'), namespace)  File "C:/Users/220/tutorials-master/tensorflowTUT/tf18_CNN2/full_code.py", line 66, in <module>prediction=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2),b_fc2)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1531, in softmaxreturn _softmax(logits, gen_nn_ops._softmax, dim, name)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1491, in _softmaxlogits = _swap_axis(logits, dim, math_ops.subtract(input_rank, 1))  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1463, in _swap_axismath_ops.range(dim_index), [last_index],  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1163, in rangereturn gen_math_ops._range(start, limit, delta, name=name)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 1740, in _rangedelta=delta, name=name)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_opop_def=op_def)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2338, in create_opset_shapes_for_outputs(ret)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1719, in set_shapes_for_outputsshapes = shape_func(op)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1669, in call_with_requiringreturn call_cpp_shape_fn(op, require_shape_fn=True)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fndebug_python_shape_fn, require_shape_fn)  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 676, in _call_cpp_shape_fn_implraise ValueError(err.message)ValueError: Shape must be rank 0 but is rank 1     for 'limit' for 'range' (op: 'Range') with input shapes: [], [10], [].

我找到了几个类似的问题及其解决方案。例如,“你声明学习率为一个一维张量,而它应该是一个标量”。不幸的是,我不知道这实际上意味着什么,也不知道如何解决我的问题。

非常感谢您的帮助!


回答:

在这一行:

prediction=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2), b_fc2)

应该改为:

prediction=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注