简体   繁体   English

这个错误InvalidArgumentError是什么(见上面的回溯):预期的维度在[-1,1]范围内,但得到1均值?

[英]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.

相关问题 InvalidArgumentError:预期维度在 [-1, 1) 范围内,但得到 1 - InvalidArgumentError: Expected dimension in the range [-1, 1) but got 1 Tensorflow InvalidArgumentError(请参阅上面的回溯):最小张量等级:2但得到:1 - Tensorflow InvalidArgumentError (see above for traceback): Minimum tensor rank: 2 but got: 1 InvalidArgumentError(参见上面的回溯):indices [1] = 10不在[0,10]中 - InvalidArgumentError (see above for traceback): indices[1] = 10 is not in [0, 10) InvalidArgumentError:无法挤压昏暗 [2],预期维度为 1,得到 3 - InvalidArgumentError: Can not squeeze dim[2], expected a dimension of 1, got 3 Conv3d:InvalidArgumentError(请参阅上面的回溯):重塑的输入是张量 - Conv3d: InvalidArgumentError (see above for traceback): Input to reshape is a tensor Python 错误“预计最多输入 1 个参数,得到 3 个”是什么意思? - What does the Python error 'imput expected at most 1 arguments, got 3' mean? 在 Pytorch 中出现错误:IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) - Getting an Error in Pytorch: IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) InvalidArgumentError:无法挤压 dim[1],预期维度为 1,得到 16 - InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 16 空回溯是什么意思? - What does a null traceback mean? IndexError:维度超出范围(预期在 [-1, 0] 范围内,但得到 1) - IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM