简体   繁体   English

Tensorflow-ValueError:形状必须为0级,但输入范围为[],[10],[]的“范围”(操作数:“范围”)的“极限”必须为1级

[英]Tensorflow - ValueError: Shape must be rank 0 but is rank 1 for 'limit' for 'range' (op: 'Range') with input shapes: [], [10], []

I am learning how to build a simple neural network recently. 我最近正在学习如何建立一个简单的神经网络。
Following Mr Mo's tutorial, I write the code step by step: 按照莫先生的教程,我逐步编写代码:

from __future__ import print_function
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# number 1 to 10 data
mnist = 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 result

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):
    # 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 network
xs = tf.placeholder(tf.float32, [None, 784]) # 28x28
ys = 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 data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                              reduction_indices=[1]))       # loss
train_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]))

However, I get an Error: 但是,我得到一个错误:

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.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Traceback (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 runfile
execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(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 softmax
return _softmax(logits, gen_nn_ops._softmax, dim, name)

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1491, in _softmax
logits = _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_axis
math_ops.range(dim_index), [last_index],

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1163, in range
return 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 _range
delta=delta, name=name)

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
op_def=op_def)

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2338, in create_op
set_shapes_for_outputs(ret)

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1719, in set_shapes_for_outputs
shapes = shape_func(op)

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1669, in call_with_requiring
return 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_fn
debug_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_impl
raise ValueError(err.message)

ValueError: Shape must be rank 0 but is rank 1
     for 'limit' for 'range' (op: 'Range') with input shapes: [], [10], [].

I find some similar questions and their solutions. 我发现了一些类似的问题及其解决方案。 For example, "You declared the learning rate as a 1D Tesnor while it should be a scalar". 例如,“您将学习速率声明为1D Tesnor,而它应该是标量”。 Unfortunately, I don't know what it actually means or how to solve my problem. 不幸的是,我不知道这实际上意味着什么或如何解决我的问题。

Thank you so much in advance! 提前非常感谢您!

On this line: 在这行上:

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

It should be: 它应该是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:形状必须为 0 级,但对于“ReadFile”(操作:“ReadFile”)为 1 级,输入形状为:[1] - ValueError: Shape must be rank 0 but is rank 1 for 'ReadFile' (op: 'ReadFile') with input shapes: [1] ValueError:形状必须为 2 级,但对于 'in_top_k/InTopKV2'(操作:'InTopKV2')为 4 级,输入形状为:[?,28,28,10], [?], [] - ValueError: Shape must be rank 2 but is rank 4 for 'in_top_k/InTopKV2' (op: 'InTopKV2') with input shapes: [?,28,28,10], [?], [] ValueError: Shape must be rank 2 but is rank 1 for &#39;MatMul&#39; (op: &#39;MatMul&#39;) with input shape: [2], [2,3] - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [2], [2,3] ValueError:Shape必须为1级,但对于&#39;ROIAlign / Crop&#39;(op:&#39;CropAndResize&#39;)排名为0,输入形状为:[2,360,475,3],[1,4],[],[2] - ValueError: Shape must be rank 1 but is rank 0 for 'ROIAlign/Crop' (op: 'CropAndResize') with input shapes: [2,360,475,3], [1,4], [], [2] TensorFlow 推荐者 - ValueError:形状必须为 2 级,但为 3 级 - TensorFlow Recommenders - ValueError: Shape must be rank 2 but is rank 3 Tensorflow:ValueError:Shape必须是等级2,但是等级3 - Tensorflow : ValueError: Shape must be rank 2 but is rank 3 Tensorflow: ValueError: Shape must be rank 4 but is rank 5 - Tensorflow: ValueError: Shape must be rank 4 but is rank 5 使用LSTM RNN在tensorflow中进行分类,ValueError:Shape(1、10、5)必须具有等级2 - classification with LSTM RNN in tensorflow, ValueError: Shape (1, 10, 5) must have rank 2 Tensorflow错误:ValueError:形状必须等于等级,但是2和1从形状1与其他形状合并 - Tensorflow Error: ValueError: Shapes must be equal rank, but are 2 and 1 From merging shape 1 with other shapes Tensorflow:optimizer.minimize()-“ ValueError:形状必须为0级,但为1级 - Tensorflow: optimizer.minimize() - "ValueError: Shape must be rank 0 but is rank 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM