FastPrepBinary Logistic Loss and Gradient

Binary Logistic Loss and Gradient

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

For this exercise, assume a batch of classification examples and the current parameters of a linear model. Implement its loss and analytic derivatives directly, without an automatic-differentiation or machine-learning library.

features is an n-by-d matrix; labels has one value per sample.

For sample i, let z[i] = bias + sum_j(features[i][j] * weights[j]) and p[i] = 1 / (1 + exp(-z[i])). The loss is mean binary cross-entropy: L = -sum_i(labels[i]*ln(p[i]) + (1-labels[i])*ln(1-p[i])) / n, using natural logarithms.

Return [L, dL/dweights[0], ..., dL/dweights[d-1], dL/dbias]. Differentiate with respect to the current weights and bias; do not update them. Compute the loss stably from logits, so a probability rounded to zero or one does not produce infinity.

No regularization, probability clipping or class weighting is used; labels are zero or one.

Function

binaryLossGradient(features: double[][], labels: int[], weights: double[], bias: double) → double[]

Examples

Example 1

features = [[1.0],[2.0]]labels = [0,1]weights = [0.0]bias = 0.0return = [0.6931471805599453,-0.25,0.0]

Both probabilities are 0.5. The mean loss is log(2), the weight derivative is -0.25 and the bias derivative is zero.

Example 2

features = [[0.0]]labels = [1]weights = [3.0]bias = 0.0return = [0.6931471805599453,0.0,-0.5]

Only the bias can change the zero-feature prediction. The result is log(2), zero weight derivative and -0.5 bias derivative.

Example 3

features = [[1.0]]labels = [0]weights = [0.0]bias = 1.0return = [1.3132616875182228,0.7310585786300049,0.7310585786300049]

The positive logit is 1. Loss is log(1+exp(1)); both parameter derivatives equal sigmoid(1).

Constraints

  • 1 <= n <= 1000 and 1 <= d <= 50.
  • All arrays have the compatible shapes described above; all input numbers are finite.
  • Feature values, weights and bias are in [-10,10].
  • Labels are 0 or 1.
  • Each returned number is compared with absolute or relative tolerance 10^-6.
  • Use analytic derivatives; finite-difference estimates are outside the exercise contract.

More Amazon problems

See Amazon hiring insights
public double[] binaryLossGradient(double[][] features, int[] labels, double[] weights, double bias) {
    return new double[0];
}
features[[1.0],[2.0]]
labels[0,1]
weights[0.0]
bias0.0
expected[0.6931471805599453,-0.25,0.0]
Checking account…