Tensorflow Error: "Label IDs must < n_classes", but my Label IDs appear to meet this requirement already



I'm attempting to create a Python 3 program to classify sentences into categories using Tensorflow. However, I'm getting a very lengthy series of errors when I try to run my code. The following error appears to be the foundation of my issue:

InvalidArgumentError: assertion failed: [Label IDs must < n_classes] [Condition x < y did not hold element-wise:x (linear/head/ToFloat:0) = ] [[4][4]1...] [y (linear/head/assert_range/Const:0) = ] 2

I am using Scikit-Learn's LabelEncoder() method to create label IDs, which should meet this requirement; their documentation page says, "Encode labels with value between 0 and n_classes-1."

The code I am trying to run is:

import tensorflow as tf
import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split

data_df = pd.read_csv('data.csv') #data.csv has 2 columns: "Category", and "Description"

features = data_df.drop('Category', axis=1) #drop Category column
lab_enc = preprocessing.LabelEncoder()
labels = lab_enc.fit_transform(data_df['Category']) #Encode labels with value between 0 and n_classes-1
labels = pd.Series(labels) #pandas_input_func needs the labels in Series format    

features_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size=0.3, random_state=101)

description = tf.feature_column.categorical_column_with_hash_bucket('Description', hash_bucket_size=1000)
feat_cols = [description]

input_func = tf.estimator.inputs.pandas_input_fn(x=features_train, y=labels_train, batch_size=100, num_epochs=None, shuffle=True)

model = tf.estimator.LinearClassifier(feature_columns=feat_cols)
model.train(input_fn=input_func, steps=1000)

The data.csv file I'm using is a small set of gibberish test data: Testing data

I'm at a bit of a loss as to how to proceed. I've only found one post referencing a similar issue here, but that user's issue appeared to be of a different nature to mine, if I'm understanding correctly.

Any insights are greatly appreciated!

1 Answer: 

Try this:

# Explicitly specify the number of classes, e.g. 10
model = tf.estimator.LinearClassifier(feature_columns=feat_cols, n_classes=10)

The default value n_classes=2, which internally means that tensorflow uses sigmoid cross entropy loss. Setting the number of classes will make it a softmax cross entropy.