Skip to content

[bug] tf.estimator.DNNClassifier setting n_classes has no effect #15800

@Echooff3

Description

@Echooff3

Describe the problem

I was following the examples for a tensorflow estimator. I am setting n_classes but the label check in (_check_labels tensorflow/python/estimator/canned/head.py", line 222) keeps kicking back the following error:

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

Source code / logs

import tensorflow as tf
import numpy as np

trainX = np.array([1,0,2,3])
num_classes = 4
feature_names = ['f1']
feature_columns = [tf.feature_column.numeric_column(k) for k in feature_names]
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns, 
                                            n_classes=num_classes, #setting number of classes here
                                            hidden_units=[10])

def input_fn():
    my_int_variable = tf.get_variable("my_int_variable", [1], dtype=tf.int32, initializer=tf.zeros_initializer)
    label = tf.one_hot(my_int_variable, num_classes) #using same number of classes
    return {'f1':trainX},label

classifier.train(input_fn=lambda: input_fn())
Traceback (most recent call last):
  File "test.py", line 17, in <module>
    classifier.train(input_fn=lambda: input_fn())
  File "/home/user/tensorflow/tf/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 302, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/home/user/tensorflow/tf/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 711, in _train_model
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "/home/user/tensorflow/tf/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 694, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "/home/user/tensorflow/tf/lib/python2.7/site-packages/tensorflow/python/estimator/canned/dnn.py", line 334, in _model_fn
    config=config)
  File "/home/user/tensorflow/tf/lib/python2.7/site-packages/tensorflow/python/estimator/canned/dnn.py", line 203, in _dnn_model_fn
    logits=logits)
  File "/home/user/tensorflow/tf/lib/python2.7/site-packages/tensorflow/python/estimator/canned/head.py", line 493, in create_estimator_spec
    features=features, mode=mode, logits=logits, labels=labels)
  File "/home/user/tensorflow/tf/lib/python2.7/site-packages/tensorflow/python/estimator/canned/head.py", line 433, in create_loss
    label_ids = self._label_ids(_check_labels(_maybe_expand_dim(labels), 1))
  File "/home/user/tensorflow/tf/lib/python2.7/site-packages/tensorflow/python/estimator/canned/head.py", line 222, in _check_labels
    (expected_labels_dimension, dim1))
ValueError: Mismatched label shape. Classifier configured with n_classes=1.  Received 4. Suggested Fix: check your n_classes argument to the estimator and/or the shape of your label.

System information

Mac OSX 10.12.6
Python 2.7
Tensorflow ('v1.4.0-19-ga52c8d9b01', '1.4.1')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions