Linear, Sigmoid and L2 Forward and Backward Passes
Problem statement
Implement the forward and backward passes of a computation graph with a linear layer, sigmoid activation, and L2 loss. The supplied features matrix has B samples and D features per sample; weights has D values, bias is one scalar, and targets has one value per sample.
Forward graph
- The linear output is
z[i] = bias + sum_j(features[i][j] * weights[j]). - The prediction is
prediction[i] = 1 / (1 + exp(-z[i])). - The loss is half the mean squared error:
L = sum_i((prediction[i] - targets[i])^2) / (2 * B).
Differentiate this scalar loss with respect to every weight, the bias, and every feature value. Use analytic derivatives and ordinary arithmetic, without an automatic-differentiation or machine-learning library. Evaluate the supplied fixed parameters; do not take an optimizer step or add regularization.
Result layout
Return a matrix with B + 4 rows. Its rows may have different lengths:
- Row
0: theBpredictions in sample order. - Row
1: the single valueL. - Row
2: theDvaluesdL/dweights[j]in feature order. - Row
3: the single valuedL/dbias. - Row
4 + i: theDvaluesdL/dfeatures[i][j]for samplei, in feature order.
Return unrounded floating-point values. Each value is accepted when its error is at most 10^-6 * max(1, abs(expected)). Handle large positive and negative logits without overflowing an exponential.
What the interview report shared
The report explicitly asks candidates to explain and implement a linear layer, sigmoid activation, L2 loss computation graph, and both passes. It asks for pseudocode and an explanation of the reasoning; it does not supply tensor shapes, loss normalization, or the returned values.
Function
linearSigmoidL2(features: double[][], weights: double[], bias: double, targets: double[]) → double[][]Examples
Example 1
features = [[0,0]]weights = [2,-3]bias = 0targets = [1]return = [[0.5],[0.125],[0.0,0.0],[-0.125],[-0.25,0.375]]The logit is 0, so the prediction is 0.5 and the loss is 0.125. The logit derivative is -0.125. Zero features give zero weight derivatives, while the feature derivatives are -0.125 * [2,-3] = [-0.25,0.375].
Example 2
features = [[1],[-1]]weights = [0]bias = 0targets = [1,0]return = [[0.5,0.5],[0.125],[-0.125],[0.0],[0.0],[0.0]]Both predictions are 0.5. The two logit derivatives are -0.0625 and 0.0625. Their bias contributions cancel; multiplying by features 1 and -1 gives weight derivative -0.125. A zero weight gives zero feature derivatives.
Example 3
features = [[1,2]]weights = [0.6931471805599453,0]bias = 0targets = [0]return = [[0.6666666666666666],[0.2222222222222222],[0.14814814814814814,0.2962962962962963],[0.14814814814814814],[0.10268847119406598,0.0]]The logit is approximately ln(2), giving prediction 2/3, loss 2/9, and logit derivative 4/27. Weight derivatives are [4/27,8/27]; feature derivatives are [(4/27)*ln(2),0].
Constraints
1 <= B <= 32and1 <= D <= 16.featuresis rectangular withBrows andDcolumns;weights.length = Dandtargets.length = B.- All input numbers are finite. Feature values, weights and bias lie in
[-10,10]. - Every target lies in
[0,1]; targets need not be integers.