9

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.

mdlee6
  • 101
  • 1
  • 1
  • 5
  • 1
    Try `keep_prob: 0.98` instead. – Hugh Bothwell Jun 24 '17 at 21:32
  • `[]` and `()` differ in whether the endpoint is inclusive. Thus, the syntax above indicates that `-1` is a valid value, but `1` is not. – Charles Duffy Jun 24 '17 at 21:36
  • Huh. We've got at least one [other instance of this question](https://stackoverflow.com/questions/44581910/tensorflow-i-get-something-wrong-in-accuracy-does-anybody-know-whats-going-on), but without an answer; one or the other will need to be answered before we can dupe 'em out. – Charles Duffy Jun 24 '17 at 21:38
  • 1
    I thought the error was saying that the variable `acc` which is the tensor `accuracy` being evaluated, has a dimension of 1, but it should be within the range [-1, 1). What does it mean to have [-1, 1) dimensions? – mdlee6 Jun 25 '17 at 00:07
  • Maybe this is the same problem Hope to be helpful [enter link description here](https://stackoverflow.com/questions/48377214/runtimeerror-dimension-out-of-range-expected-to-be-in-range-of-1-0-but-go) – pinetree Jul 25 '18 at 13:20
  • Maybe this is the same problem Hope to be helpful [same problem](https://stackoverflow.com/questions/48377214/runtimeerror-dimension-out-of-range-expected-to-be-in-range-of-1-0-but-go) – pinetree Jul 25 '18 at 13:23

5 Answers5

3

For the people coming for Tensorflow serving or Estimator loading, this error occurs because the values in the feature dictionary need to be in batches.

data = {
        "signature_name": "predict",
        "inputs": {k:[v] for k,v in inputs.items()}
    }
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).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • So does that mean that the dimensions of `acc` is 1, but it should be within the range [-1, 1)? What does it mean to have dimensions within that range? – mdlee6 Jun 25 '17 at 00:51
  • @mdlee6, equal to or greater than -1, less than 1. – Charles Duffy Jun 25 '17 at 00:51
  • yeah so the expected dimensions is supposed to be equal to or greater than -1, less than 1, but the dimension of `acc` came out to 1. What does it mean to have a dimension equal to or greater than -1, less than 1? – mdlee6 Jun 25 '17 at 00:54
  • Stumbled upon this. Am I the only one thinking having to use `-1` instead of the index of the last dimension, if that is known, is extremely counter-intuitive? – GPhilo Nov 20 '17 at 11:42
  • 1
    Yeah somebody needs to update this exception message. It is very confusing. – Maksym Bondarenko Jul 04 '18 at 22:45
2

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

Roshini
  • 703
  • 2
  • 8
  • 21
0

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));
JasonWayne
  • 1,724
  • 1
  • 19
  • 16
-1

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

pinetree
  • 61
  • 1
  • This does not give the answer to the question and your link is not accessible for anyone to see. The correct answer to this question can be found here: https://stackoverflow.com/questions/44581910/tensorflow-i-get-something-wrong-in-accuracy – Roshini Aug 17 '18 at 10:22