coral-ordinal icon indicating copy to clipboard operation
coral-ordinal copied to clipboard

How to have an output of the CoralOrdinal layer to be a single integer of predicted class?

Open marftabucks opened this issue 2 years ago • 1 comments

Hello.

I found out that the ordinal softmax function yields an output of list which contains NUM_CLASSES - 1. In other words, it's a list. I have been wondering how to have the layer to give an output of the predicted class in an integer. I found some activation functions in the activations module: 1) coral_cumprobs and 2) cumprobs_to_label.

Correct me if I'm wrong. If my goal is to have a single integer of predicted class in the output. I could use both functions mentioned above in order. Hence, I tried to created a custom activation function, something like this

def coral_labels(x: tf.Tensor):
  threshold = 0.5

  result = tf.math.sigmoid([x])
  predict_levels = tf.cast(result > threshold, dtype=tf.int32)
  predicted_labels = tf.reduce_sum(predict_levels, axis=1)
  return predicted_labels

However, the model summary still tells that the output layer is a list of (1, NUM_CLASSES - 1)

image

Could you help me on this?

marftabucks avatar Jul 03 '22 11:07 marftabucks

@marftabucks I think it's useful to review prediction vs classification. For example, have a look at https://stats.stackexchange.com/questions/464636/proper-scoring-rule-when-there-is-a-decision-to-make-e-g-spam-vs-ham-email/538442?noredirect=1#comment1107477_538442 or https://www.fharrell.com/post/classification/

Particular to your question here, ordinal regression (not classification!) gives you estimates of the Pr(label > i). It is up to the decision maker -- you or your stakeholder -- to take those probabilities and turn them into a classification of the decision (label) you want to pick. There is no single best way to do that; it depends on the cost of making errors.

Now you could write a custom layer that turns ordinal probabilities into integers (e.g., ordinal probs -> softmax probs -> tf.argmax()), add it to the model you have, and then adapt the loss function / metric that works off labels (e.g., precision). However, as explained in the posts above, this is usually not recommended, as you combine prediction & classification in a single process, which makes it much harder to use in practice. It also makes it impossible to differentiate model predictive performance from the decision making process.

See in particular the https://github.com/ck37/coral-ordinal/blob/master/coral_ordinal/activations.py , with all the helper functions that turn CORN/CORAL probabilities into softmax and labels.

gmgeorg avatar Jan 21 '23 19:01 gmgeorg