Linear, Sigmoid and L2 Forward and Backward Passes
Updated
Problem statement
You are given a tiny model: a linear layer, then a sigmoid activation, then an L2 loss. Compute its forward pass (the predictions and the loss) and its backward pass (the derivatives of the loss).
The features matrix has B samples, and each sample has D features. weights has D values. bias is one number. targets has one value per sample.
Forward graph
- The linear output, also called the logit, 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).
Find the derivative of this loss with respect to every weight, the bias, and every feature value. Use exact derivative formulas and ordinary arithmetic. Do not use an automatic-differentiation or machine-learning library. Use the given weights and bias as they are. Do not take an optimizer step, and do not add regularization.
Result layout
Return a matrix with B + 4 rows. The 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.
Do not round the values. Each value is accepted when its error is at most 10^-6 * max(1, abs(expected)). A logit can be a large positive or negative number. Your code must handle it without an exponential overflowing.
Scope
Explain and implement the linear layer, sigmoid activation and L2 loss as one computation graph, with both passes. Give pseudocode and explain your reasoning.
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, so the bias derivative is zero. Multiplying them by the features 1 and -1 and adding gives the weight derivative -0.125. The weight is zero, so the feature derivatives are zero too.
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 about ln(2). That gives the prediction 2/3, the loss 2/9, and the logit derivative 4/27. The weight derivatives are [4/27,8/27]. The 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.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Rotate ImageTokopedia · PHONE SCREENTokopedia interview questions
- Rotate ImageSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Scaled Dot-Product AttentionAmazon · PHONE SCREEN
- Multiply Two MatricesAdobe · PHONE SCREENAdobe interview questions
- Adjacent Order-Statistic Gap DistributionsScale AI · PHONE SCREENScale AI interview questions
- Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
- Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
- Build a Gaussian Blur KernelLuma AI · OALuma AI interview questions
- Rotate a Square Grid ClockwiseC3.ai · ONSITE INTERVIEWC3.ai interview questions