FastPrepTrain a Binary Recommender MLP
Problem · Math

Train a Binary Recommender MLP

Learn this problem
HardLinkedIn logoLinkedInFULLTIMEPHONE SCREEN

Problem statement

Implement deterministic training for a small binary recommender. The rows of members and items are already computed embedding vectors. Their zero-based row numbers serve as IDs. Each row of pairs is [memberId, itemId, label], where 1 means clicked and 0 means skipped. Every ID exists. Repeated pairs are allowed and count as separate training examples.

For each labeled pair, form a feature vector x by concatenating the member embedding first and the item embedding second. Do not average or normalize them. Let F be the combined feature width, H the hidden-layer width, and N the number of labeled pairs.

Model and initialization

The supplied initial parameters are hiddenWeights with shape H × F, hiddenBias and outputWeights of length H, and scalar outputBias. For one feature vector, compute:

  • a[h] = hiddenBias[h] + Σ hiddenWeights[h][d] × x[d].
  • r[h] = max(0, a[h]) (ReLU).
  • z = outputBias + Σ outputWeights[h] × r[h].
  • p = 1 / (1 + exp(-z)), the probability of a click.

Training contract

Run exactly steps full-batch gradient-descent updates. In each update, compute the mean binary cross-entropy over all N labeled pairs using the same current parameter snapshot. Compute its gradients for all weights and biases, then update every parameter simultaneously with parameter -= learningRate × gradient. Do not update parameters between examples, shuffle the input, add regularization, or use momentum. At an exactly zero ReLU preactivation, use derivative 0; it is also zero for negative preactivations and one for positive preactivations. State must be fresh for each call; do not mutate the supplied parameter arrays.

For stable loss evaluation, one example with logit z and label y has loss max(z, 0) - y × z + log(1 + exp(-abs(z))). Use double-precision calculations. The finite inputs and learning rates keep all model values within a safe range for the sigmoid formula.

Result

After the last update, run a fresh forward pass. Return a double[] of length N + 1: the first N entries are final click probabilities in the original labeled-pair order, and the last entry is their final mean binary cross-entropy. Do not return the pre-update probabilities or the average loss across training steps. Each returned number is accepted with absolute or relative error at most 10^-6.

Function

trainRecommender(members: double[][], items: double[][], pairs: int[][], hiddenWeights: double[][], hiddenBias: double[], outputWeights: double[], outputBias: double, steps: int, learningRate: double) → double[]

Examples

Example 1

members = [[1]]items = [[1]]pairs = [[0,0,1]]hiddenWeights = [[0.5,0.5]]hiddenBias = [0]outputWeights = [0]outputBias = 0steps = 1learningRate = 0.1return = [0.52497918747894,0.6443966600735709]

The feature vector is [1,1], the hidden activation is 1, and the initial probability is 0.5. The single positive example changes the output weight and bias from zero to 0.05 each. The hidden parameters do not change because the old output weight is zero. The final logit is 0.1, giving probability about 0.5249791875 and loss about 0.6443966601.

Example 2

members = [[1]]items = [[1]]pairs = [[0,0,0]]hiddenWeights = [[-0.5,-0.5]]hiddenBias = [0]outputWeights = [0]outputBias = 0steps = 1learningRate = 0.1return = [0.4875026035157896,0.6684596480132863]

The hidden preactivation is -1, so its activation and derivative are zero. For the negative label, only the output bias changes, from zero to -0.05. The final probability is about 0.4875026035, and the final negative-label loss is about 0.6684596480.

Constraints

  • Each embedding table has from 1 through 8 rows. Each table is rectangular, with its own width from 1 through 3; the two widths need not match.
  • 1 ≤ N ≤ 12, 1 ≤ H ≤ 4, and all parameter shapes match the combined width.
  • Every embedding value and initial parameter is finite and belongs to [-1, 1].
  • 1 ≤ steps ≤ 10 and 0.001 ≤ learningRate ≤ 0.1.
  • Each pair contains valid row IDs and a binary label. Duplicate and conflicting labels are allowed as separate examples.

More LinkedIn problems

drafts saved locally
public double[] trainRecommender(double[][] members, double[][] items, int[][] pairs, double[][] hiddenWeights, double[] hiddenBias, double[] outputWeights, double outputBias, int steps, double learningRate) {
    // Write your code here
}
members[[1]]
items[[1]]
pairs[[0,0,1]]
hiddenWeights[[0.5,0.5]]
hiddenBias[0]
outputWeights[0]
outputBias0
steps1
learningRate0.1
expected[0.52497918747894,0.6443966600735709]
checking account