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

时间:2017-10-12 16:57:09

标签: python tensorflow

尝试运行以下代码时出错:

onPress={() => this.setState({ fontLoaded: true })}

实际错误是:

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})

代码在TensorFlow中使用逻辑回归运行简单的二进制分类。

我一直在网上做一些研究,但无法找到满意的解决方案。

谢谢,

3 个答案:

答案 0 :(得分:2)

问题不在于准确性。错误清楚地表明问题出在argmax中。  请检查您的激活'并且' y'如果他们中的任何人是1-D,那么删除argmax的第二个操作数,它可能会解决你的问题。

答案 1 :(得分:1)

Github上有一个完整的运行示例。具体来说,我能够运行以下代码:

$ 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

这表明您可能拥有较旧版本的Tensorflow。我会尝试安装1.3.0并查看是否能解决您的问题。

答案 2 :(得分:1)

在您的代码中对此进行修改

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

对此

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