4

When I have 2 classes I used binary_crossentropy as loss value like this to compile a model:

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

But right now I have 5 classes & I'm not using on hot encoded features. So I choose sparse_categorical_crossentropy as loss value. But what should be the accuracy metric as keras metric source code suggested there are multiple accuracy metrics available. I tried:

model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])

So is it correct or should I just use categorical_accuracy?

Poles
  • 3,585
  • 9
  • 43
  • 91

1 Answers1

2

sparse_categorical_accuracy is a correct metrics for sparse_categorical_entropy.

But why are you using sparse_categorical_entropy? What kind of classes do you have? sparse_categorical_entropy is being used for Integer outputs. But if you have a one-hot-encoded target, you should use categorical_crossentropy as loss function and accuracy or categorical_accuracy for metrics.


UPDATE:
Use the following code for your classification problem:

model.add(Dense(5, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
Reza Behzadpour
  • 638
  • 5
  • 16
  • I don't know how to use one-hot-encoded. Thats why I posted [this](https://stackoverflow.com/questions/53594864/how-to-use-one-hot-encoded-ouput-vector-with-dense-to-train-a-model-in-keras) – Poles Dec 04 '18 at 10:26
  • Use `keras.utils.to_categorical` function. – Reza Behzadpour Dec 04 '18 at 10:59
  • I already generated one-hot-encoded vector but I don't know how to use it creating the model. Can you show me any example for that? – Poles Dec 04 '18 at 11:10