Categorical classification in Keras Python

I am doing multi-class classification of 5 classes. I am using Tensorflow with Keras. My code is like this:

# load dataset
dataframe = pandas.read_csv("Data5Class.csv", header=None)
dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:47].astype(float)
Y = dataset[:,47]
print("Load Data.....")

encoder= to_categorical(Y)

def create_larger():  
    model = Sequential()
    print("Create Dense Ip & HL 1 Model ......")
    model.add(Dense(47, input_dim=47, kernel_initializer='normal',     activation='relu'))
    print("Add Dense HL 2 Model ......")
    model.add(Dense(40, kernel_initializer='normal', activation='relu'))
    print("Add Dense output Model ......")
    model.add(Dense(5, kernel_initializer='normal', activation='sigmoid'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

estimators = []
estimators.append(('rnn', KerasClassifier(build_fn=create_larger, epochs=60,  batch_size=10, verbose=0)))
pipeline = Pipeline(estimators)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(pipeline, X, encoder, cv=kfold)
print("Accuracy: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))

The CSV file I have taken as an input contains the data with labels. The labels are like this 0, 1, 2, 3, 4 which represent 5 different classes.

  1. Then, as the labels are already in integer form, do I need to use the LabelEncoder() function in my code?
  2. Also, I have used to_categorical(Y) function. Should I use it or I should just pass the Y variable containing these labels to the classifier for training?

  3. I got the error like this: Supported target types are: ('binary', 'multiclass'). Got 'multilabel-indicator' instead. This error occurred when I used encoder variable in the code results = cross_val_score(pipeline, X, encoder, cv=kfold) where encoder variable represents the to_categorical(Y) data. How to solve this error?

1 answer

  • answered 2018-04-17 04:13 Vivek Kumar

    As mentioned on the Keras documentation here:

    Note: when using the categorical_crossentropy loss, your targets should be in categorical format (e.g. if you have 10 classes, the target for each sample should be a 10-dimensional vector that is all-zeros except for a 1 at the index corresponding to the class of the sample). In order to convert integer targets into categorical targets, you can use the Keras utility to_categorical:

    from keras.utils.np_utils import to_categorical
    categorical_labels = to_categorical(int_labels, num_classes=None)
    

    So this means that you need to use the to_categorical() method on your y before training. But no need to use LabelEncoder if y is already in integer type.