InvalidArgumentError: Expected dimension in the range [-1, 1) but got 1

柔情痞子 提交于 2019-12-19 17:27:10

问题


I'm not sure what this error means. This error occurs when I try to calculate acc:

acc = accuracy.eval(feed_dict = {x: batch_images, y: batch_labels, keep_prob: 1.0})

I've tried looking up solutions, but I couldn't find any online. Any ideas on what's causing my error?

Here's a link to my full code.


回答1:


I had a similar error but the problem for me was that I was trying to use argmax on a 1 dimensional vector. So the shape of my label was (50,) and I was trying to do a tf.argmax(y,1) on that when evaluating. The solution reference is Tensorflow: I get something wrong in accuracy




回答2:


The source code generating this error reads as follows:

OP_REQUIRES(context, axis >= 0 && axis < input_dims,
            errors::InvalidArgument("Expected dimension in the range [",
                                    -input_dims, ", ", input_dims,
                                    "), but got ", dim));

Note that axis is required to be less than input_dims, not less-than-or-equal.

This conforms with the syntax [-1,1) in the message: [ indicates an inclusive value (such that -1 is valid), whereas ) indicates an exclusive value (putting 1 itself outside the range).




回答3:


For code like

tf.equal(tf.argmax(y, 1), tf.argmax(labels, 1))

which is often used when calculating accuracy, you can change to

tf.equal(tf.argmax(y, -1), tf.argmax(labels, -1))

according to the source code:

// tensorflow/compiler/tf2xla/kernels/index_ops_cpu.cc:58
OP_REQUIRES(ctx, axis >= 0 && axis < input_dims,
            errors::InvalidArgument("Expected dimension in the range [",
                                    -input_dims, ", ", input_dims,
                                    "), but got ", dim));



回答4:


I solved this problem. Check the expression of batch_labels

# if use one hot code use
# y_true_cls = tf.argmax(y_true, dimension=1)

# if not one hot code use
y_true_cls = y_true

Hope to be helpful



来源:https://stackoverflow.com/questions/44741134/invalidargumenterror-expected-dimension-in-the-range-1-1-but-got-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!