New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Echooff3 opened this Issue Jan 3, 2018 · 7 comments

Comments

Projects
None yet
6 participants
@Echooff3

Echooff3 commented Jan 3, 2018

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

@facaiy

This comment has been minimized.

Member

facaiy commented Jan 3, 2018

How about removing tf.one_hot for label?

labels: Tensor of shape [batch_size, 1] or [batch_size] labels of dtype int32 or int64 in the range [0, n_classes).

@Echooff3

This comment has been minimized.

Echooff3 commented Jan 3, 2018

If I remove the one_hot for the label I get the following error:

ValueError: Dimensions must be equal, but are 4 and 1 for 'dnn/head/sparse_softmax_cross_entropy_loss/xentropy/xentropy' (op: 'SparseSoftmaxCrossEntropyWithLogits') with input shapes: [4,4], [1].

For background: I have a bunch of 1 second audio data clips of people talking (47 distinct voices). It's in a binary format where the first 2 bytes are the id of the speaker and the next 16,000 bytes are the raw audio data. Perhaps I am using this estimator incorrectly. My assumption was to create one hot labels based on the id of the row and use that as the label. If this is not the intended way for this estimator to be used I will close this issue and revisit my approach.

@Echooff3

This comment has been minimized.

Echooff3 commented Jan 3, 2018

I revisited my input function. Works as expected.

import tensorflow as tf
import numpy as np
tf.logging.set_verbosity(tf.logging.INFO)
trainX = np.array([1,0,2,3])
labelX = np.array([1,0,1,0])
num_classes = 2
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():
    def gen1(a,b):
        return {'f1':a},b
    dataset = tf.data.Dataset.from_tensor_slices((trainX, labelX)).map(gen1)
    dataset = dataset.repeat(8)
    dataset = dataset.batch(32)
    iterator = dataset.make_one_shot_iterator()
    data, labels = iterator.get_next()
    return data, labels

def input_fn_pred(idx):
    def gen1(a,b):
        return {'f1':[idx]},[idx]
    dataset = tf.data.Dataset.from_tensor_slices((trainX, trainX)).map(gen1)
    dataset = dataset.batch(1)
    iterator = dataset.make_one_shot_iterator()
    data, labels = iterator.get_next()
    return data, labels

classifier.train(input_fn=lambda: input_fn())

predict_results = classifier.predict(
    input_fn=lambda: input_fn_pred(2))
for prediction in predict_results:
   print prediction["class_ids"][0] 

@Echooff3 Echooff3 closed this Jan 3, 2018

@abdel

This comment has been minimized.

abdel commented Feb 14, 2018

I actually ran into this problem recently. My labels shape for the dataset is (?, 10) by default and passed n_classes=10 in a LinearClassifier, receiving a similar error.

Digging a bit deeper - the error message is a bit misleading, as I noticed that expected_labels_dimension is hard-coded to be 1 in the create_loss function here.

@aaronsherwood

This comment has been minimized.

aaronsherwood commented Mar 18, 2018

I am also receiving this same error. No matter what I put for n_classes it says:

Classifier configured with n_classes=1. Received 10.

@jdwinkler

This comment has been minimized.

jdwinkler commented Apr 3, 2018

Same error on my end-has this been fixed in an update?

@bjayakumar

This comment has been minimized.

bjayakumar commented Nov 6, 2018

I have the same error. Any fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment