TensorFlow estimator number of classes does not change

痴心易碎 提交于 2019-12-07 17:38:47

问题


I tried using tensorflow estimator for the MNIST dataset. For some reason it keep saying my n_classes is set to 1 even though it is at 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))

Error:

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:


That's a good question. tf.estimator.DNNClassifier is using tf.losses.sparse_softmax_cross_entropy loss, in other words it expects ordinal encoding, instead of one-hot (can't find it in the doc, only the source code):

labels must be a dense Tensor with shape matching logits, namely [D0, D1, ... DN, 1]. If label_vocabulary given, labels must be a string Tensor with values from the vocabulary. If label_vocabulary is not given, labels must be an integer Tensor with values specifying the class index.

You should read the data with one_hot=False and also cast the labels to int32 to make it work:

y=ydata.astype(np.int32)


来源:https://stackoverflow.com/questions/48114258/tensorflow-estimator-number-of-classes-does-not-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!