TensorFlow估算器的类数不会改变

时间:2018-01-05 13:05:27

标签: python tensorflow machine-learning deep-learning mnist

我尝试使用tensorflow估算器作为MNIST数据集。出于某种原因,它一直说我的n_classes设置为1,即使它是10!

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)


feature_columns = [tf.feature_column.numeric_column("x", shape=[784])]

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                        hidden_units=[500, 500, 500],
                                        n_classes=10,
                                        model_dir="/tmp/MT")
for i in range(100000):
    xdata, ydata = mnist.train.next_batch(500)
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x":xdata},
        y=ydata,
        num_epochs=None,
        shuffle=True)
    classifier.train(input_fn=train_input_fn, steps=2000)

# Define the test inputs
test_input_fn = tf.estimator.inputs.numpy_input_fn(
    x= {"x":mnist.test.images},
    y= mnist.test.labels,
    num_epochs=1,
    shuffle=False)

# Evaluate accuracy.
accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]

print("\nTest Accuracy: {0:f}\n".format(accuracy_score))

错误:

ValueError: Mismatched label shape. Classifier configured with n_classes=1.  Received 10. Suggested Fix: check your n_classes argument to the estimator and/or the shape of your label.

Process finished with exit code 1

1 个答案:

答案 0 :(得分:2)

这是一个很好的问题。 tf.estimator.DNNClassifier使用tf.losses.sparse_softmax_cross_entropy丢失,换句话说,它需要序数编码,而不是 one-hot (无法在doc中找到它) ,只有the source code):

  

labels必须是密集的Tensor,形状匹配logits,即     [D0, D1, ... DN, 1]。如果给出label_vocabularylabels必须是字符串     Tensor包含词汇表中的值。如果没有给出label_vocabulary,     labels必须是整数Tensor,其值指定类索引。

您应该使用one_hot=False读取数据,并将标签转换为int32以使其正常工作:

y=ydata.astype(np.int32)