BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Paper implementations23 of 28 in topic

850. Barlow Twins redundancy reduction loss

MediumPaper implementations

Why it matters Barlow Twins is a non-contrastive SSL method with no negatives and no momentum encoder. Its loss is short enough to write in an interview and tests whether you know biased versus unbiased std.

Paper: "Barlow Twins: Self-Supervised Learning via Redundancy Reduction", Zbontar, Jing, Misra, LeCun and Deny, 2021, arXiv 2103.03230.

Two augmented views of a batch go through the same network, giving z_a and z_b of shape (N, D). Following the paper's pseudocode:
- standardise each feature over the batch: (z - mean over axis 0) / std over axis 0, where std is the unbiased sample std (ddof=1, the PyTorch default)
- c = z_a_norm.T @ z_b_norm / N, a (D, D) cross-correlation matrix
- loss = sum_i (1 - c_ii)^2 + lam * sum over i != j of c_ij^2

Write barlow_twins_loss(z_a, z_b, lam=0.005).

Inputs: z_a and z_b are lists or ndarrays of shape (N, D) with N >= 2. No feature has zero variance in the tests.

Return a Python float. No rounding.

Functiondef barlow_twins_loss(z_a, z_b, lam=0.005)

Example 1

Input: barlow_twins_loss([[1.0, 2.0], [3.0, 1.0], [5.0, 6.0]], [[1.0, 2.0], [3.0, 1.0], [5.0, 6.0]])

Output: 0.2248

Example 2

Input: z_a = [[1.0, 2.0], [3.0, 1.0], [5.0, 6.0]], z_b = [[6.0, 5.0], [1.0, 3.0], [2.0, 1.0]]

Output: 4.526015

Example 3

Input: z_a = [[0.0, 1.0, 2.0], [1.0, 0.0, 1.0], [2.0, 2.0, 0.0], [3.0, 1.0, 1.0]], z_b = [[0.5, 1.0, 2.0], [1.0, 0.5, 1.5], [2.5, 2.0, 0.0], [3.0, 1.5, 0.5]], lam = 0.01

Output: 0.278325

Constraints

  • z_b has shape (N, D)
  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Standardise each column with mean(0) and std(0, ddof=1).

Hint 2

c = a.T @ b / N.

Hint 3

Diagonal term: sum((1 - diag)^2). Off-diagonal: sum of all squares minus the diagonal squares, times lam.

Common mistakes
  • Using ddof=0 for the std.
  • Including the diagonal in the off-diagonal penalty.
  • Correlating samples (a @ b.T) instead of features.
Understand the idea

The loss wants the cross-correlation between the two views to be the identity: each feature agrees with itself across views (diagonal 1) and features do not repeat each other (off-diagonal 0). Because the paper divides by N but standardises with the unbiased std, even identical views give a diagonal of (N - 1) / N, not 1. With N = 3 and identical views the diagonal is 2/3, which is why the example loss is not zero.

Shapes and data flow
z_a, z_b: (N, D). c: (D, D). Output: scalar.
Topics
Paper implementations
Similar questions
  • Knowledge distillation loss with temperature (Hinton et al., 2015)Medium
  • ALiBi linear attention biases (Press et al.) instead of position embeddingsMedium
  • Mix a batch with mixupMedium
  • Compute a multi-token prediction loss over several headsMedium
All Paper implementations problems
Report
850 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def barlow_twins_loss(z_a, z_b, lam=0.005):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS