Ordinal regression, also known as ordinal classification, is a type of regression analysis used in machine learning where the target variable has a natural order, but the intervals between the categories are not necessarily known or equal. Common examples include rating scales (e.g., "poor", "fair", "good", "excellent") or stages of a disease (e.g., "stage 1", "stage 2", "stage 3").
In ordinal regression, it is important to choose an activation function that respects the order of the categories. One common approach is to use the 'softmax' function for predicting the probabilities of the classes and then decode the softmax outputs to respect the ordinal nature of the problem. However, this does not inherently take the order into account during the training process.
Recently, more appropriate activation functions have been devised for ordinal regression. One such function is the 'cumulative link' (also known as 'proportional odds') model. In Keras, you can implement a custom activation function that models the cumulative probabilities of the ordinal classes.
Here is an example of how you might define a custom activation function for ordinal regression in Keras:
python
from keras.layers import Activation
from keras import backend as K
def ordinal_softmax(x):
"""An ordinal regression activation function.
Assuming x is of shape (batch_size, num_classes - 1)
"""
x = K.concatenate([x, K.zeros_like(x[:, :1])], axis=1)
x = K.cumsum(x, axis=1)
return K.softmax(x)
# To use this custom activation function, you would include it in your model like this:
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
# ... (other layers) ...
# Assuming you have 5 ordinal classes, you output 4 logits to represent the thresholds between classes
model.add(Dense(4))
model.add(Activation(ordinal_softmax))
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Continue with model fitting, evaluation, etc.
In this custom function, ordinal_softmax, the idea is to compute cumulative logits for each threshold between the ordinal classes and then apply the softmax function across these cumulative logits. This way, the model learns the boundaries between the ordinal classes.
When compiling the model, you can still use a loss function such as 'sparse_categorical_crossentropy' if your data is in the form of integer class indices. However, if you want to better handle the ordinal nature during training, you might need a custom loss function that penalizes the misprediction according to the distance between the true and predicted categories.
Remember that when you are dealing with ordinal regression, it's crucial to ensure that all aspects of your model and training process respect the ordinal nature of your problem. This includes the architecture, the loss function, and the way you interpret the output of the model.
User
what activation function should i use when my keras model makes an ordinal regression?
I use a single value with
0 for worse
0.25 for bad
0.50 for acceptable
0.75 for good
1.0 for perfect
sigmoid is not best choice in my mind and relu overshoots
Assistant
In your situation, where the outputs are ordinal and scaled between 0 and 1, you may want to consider using an activation function that constrains the outputs to this range. Commonly, the sigmoid activation function is used for binary classification problems because it maps values into the range (0, 1), which can also be suitable for ordinal regression when the targets are scaled similarly.
However, you've mentioned that a sigmoid may not be the best choice in your opinion. This might be due to sigmoid potentially suffering from saturation and slow learning when the output is near 0 or 1. Given that you have a well-defined scale from 0 to 1 with increments of 0.25, you can still consider using the sigmoid function but be careful with the initialization and scaling of your outputs.
Alternatively, you might want to create a custom scaled activation function that specifically constrains outputs to the range you're interested in, and is less prone to saturation. A simple custom activation function that clips the output can be considered:
python
from keras.layers import Activation
from keras import backend as K
def clipped_relu(x):
return K.minimum(K.maximum(x, 0), 1)
# To use this custom activation function, you would include it in your model like this:
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
# ... (other layers) ...
model.add(Dense(1))
model.add(Activation(clipped_relu))
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
# Continue with model fitting, evaluation, etc.
Here, clipped_relu is a custom function that behaves like a ReLU but clips its maximum value to 1. This is very similar to the ReLU6 activation function used in MobileNet architectures, which clips the ReLU to 6, except here we clip it to 1.
For your loss function, since the problem is ordinal and the output is continuous within a range, you might want to use a regression loss like mean squared error (MSE) or mean absolute error (MAE), rather than a classification loss. This way, the model is penalized based on how far off the prediction is from the actual value.
Remember to scale your output labels to the same scale as your activation function during training. With MSE or MAE as your loss, your model will learn to predict the continuous value that represents your ordinal classes, and you can then round or map these predictions to your ordinal scale (0, 0.25, 0.50, 0.75, 1.0) after the model makes its predictions.