KatInterview

Neural Network Implementation of Centroid-Based Classification

Arrays & HashingAuto-graded · 20 test cases
EasyAuto-gradedArrays & Hashingneural-networksclusteringlinear-algebra

Last updated

Derives the weight matrix and bias vector of a single-layer network that performs soft assignment to cluster centers using squared distance.

Neural Network Implementation of Centroid-Based Classification

You are given a collection of cluster centers (centroids) in multi-dimensional space. Your goal is to construct a single-layer neural network that assigns an input point to these centroids based on squared Euclidean distance.

Specifically, implement a function that builds the parameters of a linear transformation followed by softmax activation. The network should output a probability distribution where points closer to a centroid receive higher probability, controlled by a temperature parameter.

Task

Given:

  • A list of centroids, where each centroid is a list of floats representing coordinates in d-dimensional space
  • An input point x as a list of floats
  • A temperature parameter (a positive float)

Compute and return a dictionary containing:

  • "weights": A weight matrix represented as a list of lists, where each row corresponds to one centroid
  • "bias": A bias vector as a list of floats, one entry per centroid
  • "probabilities": The softmax output as a list of floats summing to 1.0

The network parameters must satisfy: for each centroid index i, the logit (pre-softmax value) equals -||x - centroid_i||² / (2 * temperature) plus any constant that depends only on x (not on i).

Mathematical Background

The squared Euclidean distance between point x and centroid c_i expands as:

||x - c_i||² = ||x||² - 2·(x·c_i) + ||c_i||²

Since softmax probabilities are unchanged when the same constant is added to all logits, the ||x||² term can be ignored. The remaining terms should be computed by your linear layer.

Constraints

  • 1 ≤ number of centroids ≤ 100
  • 1 ≤ dimension d ≤ 100
  • All coordinate values are in the range [-1000, 1000]
  • 0.01 ≤ temperature ≤ 100
  • Use numerically stable softmax (subtract maximum logit before exponentiating)

Examples

Example 1

Input: compute_soft_centroid_assignment([[0, 0], [2, 2]], [1, 2], 0.1)

Output: {'weights': [[0, 0], [20, 20]], 'bias': [0, -40], 'probabilities': [2.0611536181902033e-9, 0.9999999979388463]}

Example 2

Input: compute_soft_centroid_assignment([[0, 0], [2, 2]], [1, 2], 1)

Output: {'weights': [[0, 0], [2, 2]], 'bias': [0, -4], 'probabilities': [0.11920292202211755, 0.8807970779778823]}

Example 3

Input: compute_soft_centroid_assignment([[1, -1], [4, 2], [-2, 3]], [0, 0], 0.5)

Output: {'weights': [[2, -2], [8, 4], [-4, 6]], 'bias': [-2, -20, -13], 'probabilities': [0.999983283348681, 1.5229725150451643e-8, 0.000016701421593737116]}

Hints

Hint 1

The dot product x·c_i appears when expanding the squared distance formula and is the only term linear in x

Hint 2

Softmax is shift-invariant: adding the same value to all logits doesn't change the output probabilities

Hint 3

The bias term must compensate for differences in centroid magnitudes to ensure distance-based assignment