Your question is Custom Loss Without Deep Libraries. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Optum Insight model evaluation pipelines may need a lightweight loss calculation when a deep learning framework is unavailable. Implement a custom weighted binary cross-entropy loss directly in Python, using raw logits and returning the gradient for every example.
Write weighted_logistic_loss(logits, labels, weights), where:
logits is a list of real-valued model outputs.labels is a list containing only 0 or 1.weights is a list of nonnegative example weights.weights is positive.For each example, compute the numerically stable binary cross-entropy from the logit z:
max(z, 0) - z * y + log(1 + exp(-abs(z)))
Return a dictionary with:
loss: the weighted mean loss, divided by the sum of all weights.gradients: a list where each value is weight / sum(weights) * (sigmoid(logit) - label).Do not use NumPy, PyTorch, TensorFlow, or other external libraries. The implementation must remain stable for very large positive or negative logits.
def weighted_logistic_loss(logits, labels, weights):