4

I'm getting an error when trying to run the following code:

correct = tf.equal(tf.argmax(activation,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print ('Accuracy: ', sess.run(accuracy, feed_dict = {x: test_x, yL test_y})

The actual error is:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected dimension in the range [-1, 1), but got 1
     [[Node: ArgMax_1 = ArgMax[T=DT_FLOAT, Tidx=DT_INT32, output_type=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_Placeholder_1_0_1, ArgMax_1/dimension)]]

The code is running a simple binary classification using logistic regression in TensorFlow.

I've been doing some research on the net but can't really find a satisfactory solution.

Thanks,

mshsayem
  • 17,557
  • 11
  • 61
  • 69
redmage123
  • 413
  • 8
  • 15
  • 1
    `[` means inclusive, `(` means exclusive. Thus, the condition that must be satisified is `-1 <= x < 1`, and you provided `x = 1`. – Charles Duffy Oct 12 '17 at 17:00
  • We also have this same question at https://stackoverflow.com/questions/44741134/invalidargumenterror-expected-dimension-in-the-range-1-1-but-got-1, and another instance at https://stackoverflow.com/questions/44581910/tensorflow-i-get-something-wrong-in-accuracy-does-anybody-know-whats-going-on. – Charles Duffy Oct 12 '17 at 17:01
  • 1
    Okay, but now I'm confused. Every example I've seen using tf.argmax has the second value set to one. This line of code comes directly out of the offical TensorFlow tutorial on logistic regression. What exactly do I need to change to make this work? – redmage123 Oct 12 '17 at 17:27
  • Sorry -- I grok the relevant math syntax, so I can describe what the error message means (which *is* a literal way to interpret the question title, after all), but I don't know Tensorflow to tell you *why* it's telling you a value is supposed to be in that range. – Charles Duffy Oct 12 '17 at 17:31
  • ...that said, if I had to make a guess from reading the docs -- see the restriction `0 <= axis < rank(input)` in `argmax`, so `axis==1` is only valid if the relevant input is not a simple vector; otherwise, it should be 0. – Charles Duffy Oct 12 '17 at 17:40

3 Answers3

2

The problem is not in accuracy. Error clearly shows that problem is in argmax. please check your dimension of 'activation' and 'y' if anyone of them is of 1-D then remove the second operand of argmax and it will probably resolve your issue.

abhi
  • 397
  • 3
  • 14
1

There is a full, running example on Github. Specifically, I was able to get the following code to run:

$ cat tf.py

from __future__ import print_function

import tensorflow as tf
assert tf.__version__ == '1.3.0'

# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

# Parameters
learning_rate = 0.01
training_epochs = 0
batch_size = 100
display_step = 1

# tf Graph Input
x = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 classes

# Set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# Construct model
pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax

# Minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))

# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()

# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))

# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
sess = tf.InteractiveSession()

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
sess.run(init)
print ('Accuracy: ', sess.run(accuracy, feed_dict = {x: mnist.test.images,
                                                     y: mnist.test.labels}))

$ python tf.py
Extracting /tmp/data/train-images-idx3-ubyte.gz
Extracting /tmp/data/train-labels-idx1-ubyte.gz
Extracting /tmp/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/data/t10k-labels-idx1-ubyte.gz
Accuracy:  0.098

This suggests that you might have an older version of Tensorflow. I would try installing 1.3.0 and seeing if that solves your problem.

finbarr
  • 749
  • 4
  • 11
  • I have version 1.3.0. My code is a lot simpler than the MNIST example. I'm simply taking two columns, height and gender and trying to predict whether the person is male or female based on the height. I'm using the standard tf.nn.sigmoid() function for the activation function and calculating cross entropy like so: cross_entropy = tf.reduce_mean(-(y*tf.log(activation) + (1 - y) * tf.log(1-activation))) – redmage123 Oct 12 '17 at 18:04
  • Can you add your data? – finbarr Oct 12 '17 at 21:00
1

In your code modify this

# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))

to this

# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y))
Faeza
  • 101
  • 5