Linear Head Prediction

~15 mincode completion

Implement linear_head_predict(Z, W) that returns the predictions and linear_head_mse(Z, W, y_true) that returns the MSE loss. Implement them as two separate functions.

Examples

Input
linear_head_predict([[1, 0], [0, 1], [1, 1]], [[2], [3]])
Output
[[2], [3], [5]]

linear_head_mse: perfect predictions → 0 loss

Input
linear_head_predict([[1, 0], [0, 1]], [[1], [1]], y_true=[[1], [1]])
Output
0

linear_head_mse: known error

Input
linear_head_predict([[1, 0], [0, 1]], [[2], [3]], y_true=[[1], [1]])
Output
2.5

Hints

Hint 1

Use a matrix product rather than nested loops, and check which operand transposes.

Hint 2

Do not forget to matrix multiply. That step is easy to skip.

Requirements

  • Z: Embedding matrix from frozen backbone, shape (m, d)

  • : Head weight vector, shape (d, 1)

  • Return Predictions of shape (m, 1).

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 min

8 employers weight this skill

4 frontier labs, 3 AI product companies, 1 enterprise vendor. Top match scores 81.

Python
import numpy as np

def linear_head_predict(Z: np.ndarray, W: np.ndarray) -> np.ndarray:
    """
    Compute predictions from frozen embeddings and a linear head.

    Args:
        Z: Embedding matrix from frozen backbone, shape (m, d)
        W: Head weight vector, shape (d, 1)

    Returns:
        Predictions of shape (m, 1).
    """
    # YOUR CODE HERE
    pass

def linear_head_mse(Z: np.ndarray, W: np.ndarray, y_true: np.ndarray) -> float:
    """
    Compute MSE loss for the linear head.

    Args:
        Z:      Embedding matrix, shape (m, d)
        W:      Head weights, shape (d, 1)
        y_true: Ground truth, shape (m, 1)

    Returns:
        Scalar MSE loss.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.