Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
marftabucks opened this issue Jul 3, 2022 · 1 comment

Comments

@marftabucks
Copy link

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?

@gmgeorg
Copy link
Collaborator

gmgeorg commented Jan 21, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants