taga
taga

Reputation: 3885

How to do Multiclass classification with Keras?

I want to make simple classifier with Keras that will classify my data. Features are numeric data and results are string/categorical data. I'm predicting 15 different categories/classes. This is how my code looks:

model = Sequential()
model.add(Dense(16, input_dim = x_train.shape[1], activation = 'relu')) # input layer requires input_dim param
model.add(Dense(16, activation = 'relu'))
model.add(Dense(16, activation = 'relu'))
model.add(Dense(1, activation='relu'))

model.compile(loss="binary_crossentropy", optimizer= "adam", metrics=['accuracy'])

#es = EarlyStopping(monitor='loss', min_delta=0.005, patience=1, verbose=1, mode='auto')
model.fit(x_train, y_train, epochs = 100, shuffle = True, batch_size=128, verbose=2)

scores = model.evaluate(x_test, y_test)
print(model.metrics_names[0], model.metrics_names[1])

the problem is that I'm always getting this error:

ValueError: could not convert string to float 'category1'

What am I doing wrong?

When I replace my classes names "category1", "category2" etc with integer numbers, my code works but it always give me accuracy of 0. I have tried to change number of nodes and layers and activation functions but the result is always 0. It seams like the model thinks that I'm doing regression not classification.

What is the correct way to do classification with Keras lib If my categorical values are not just 1 or 0?

Upvotes: 8

Views: 22910

Answers (2)

Milton Arango G
Milton Arango G

Reputation: 805

You need to convert your string categories to integers, there is a method for that:

y_train = tf.keras.utils.to_categorical(y_train, num_classes=num_classes)

Also, the last layer for multi-class classification should be something like:

model.add(Dense(NUM_CLASSES, activation='softmax'))

And finally, for multi-class classification, the correct loss would be categorial cross-entropy.

model.compile(loss="categorical_crossentropy", optimizer= "adam", metrics=['accuracy'])

This is a nice example available from tensorflow: Classification Example

Upvotes: 13

emil
emil

Reputation: 331

If you have 15 classes, represented by labels 0 to 14, you can set up your final dense layer with 15 neurons and activation sigmoid Dense(15, ...).

Additionaly, if you do not one-hot encode your data, set sparse_categorical_crossentropy as loss and sparse_categorical_accuracy as metric.

Upvotes: 1

Related Questions