[英]What does this error InvalidArgumentError (see above for traceback): Expected dimension in the range [-1, 1), but got 1 mean?
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. 代码在TensorFlow中使用逻辑回归运行简单的二进制分类。
I've been doing some research on the net but can't really find a satisfactory solution. 我一直在网上做一些研究但是找不到满意的解决方案。
Thanks, 谢谢,
The problem is not in accuracy. 问题不在于准确性。 Error clearly shows that problem is in argmax.
错误清楚地表明问题出在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.
请检查“激活”和“y”的维度,如果其中任何一个是1-D,则删除argmax的第二个操作数,它可能会解决您的问题。
There is a full, running example on Github . 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. 这表明您可能拥有较旧版本的Tensorflow。 I would try installing
1.3.0
and seeing if that solves your problem. 我会尝试安装
1.3.0
,看看是否能解决您的问题。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.