SYNTOLOGY HomeExplorerAtlasCodeMethodologyAboutDevelopersFeedPricing
Paper · 2204.07596 · ICML · 2022

Perfectly Balanced: Improving Transfer and Robustness of Supervised Contrastive Learning

Christopher Ré, Zhao Song, Michael Zhang, Kayvon Fatahalian, Mayee Chen, Avanika Narayan, Daniel Fu

arXiv · PDF · Open in the Atlas

Code that ran

6 of 7 samples (functions and classes) lifted from this paper's linked repositories executed on a synthesized input in Syntology's jail (0 of them constructed an object rather than computing a result; 0 honoured the contract we drafted, 1 violated it and 5 have no contract verdict; 0 failures of our instrument; 1 no recorded run), measured 2026-11-22; repository links from the Papers with Code archive attributed under CC BY-SA, no affiliation; bodies inline only under a permissive licence as GitHub detects it. Not a reproduction of the paper's results.

"Ran" means the function executed on a synthesized input and returned a value. It is not a reproduction of the paper's results. This record, dated, as JSON: /api/ran/2204.07596.json · badge: /api/ran/2204.07596.svg

Each repository's role is a label: a classifier's output recorded on the link, or, where no role is recorded, what the Papers with Code archive marked. The line under it names its basis. Neither says who wrote the code.

RepositoryRoleRan
HazyResearch/thanos-code canonical (classifier label)
classifier basis not recorded on the link
6 ran · 1 no recorded run
FunctionStatusWhere it lives
ce_loss Ran
ran on a synthesized input
HazyResearch/thanos-code/unagi/tasks/loss_modules.py
code served (permissive licence) · get_code("4251f968142da139")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Tensor, ndim 0, dtype family float, shape () as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import torch.nn as nn

torch.manual_seed(0)

# Create a simple loss function
loss_fn = nn.CrossEntropyLoss()

# Create loss_fns dict mapping task names to loss functions
loss_fns = {
    'classification': loss_fn
}

# Create aug_type
aug_type = 'rotation'

# Create n_views
n_views = 2

# Create module_names
module_names = ['encoder', 'decoder']

# Create task_name
task_name = 'classification'

# Create Y (target labels)
Y = torch.randint(0, 2, (2,))  # batch size 2, 2 classes

# Create intermediate_output_dict
# This dict needs to contain:
# - Keys for each module in module_names, each with a list containing output tensors
# - A key for '{aug_type}_augmentation' with a list containing Y
intermediate_output_dict = {
    'encoder': [torch.randn(2, 4)],  # batch size 2, output dim 4
    'decoder': [torch.randn(2, 4)],  # batch size 2, output dim 4
    'rotation_augmentation': [torch.randint(0, 2, (2,))]  # augmented Y
}

ARGS = {
    'loss_fns': loss_fns,
    'aug_type': aug_type,
    'n_views': n_views,
    'module_names': module_names,
    'task_name': task_name,
    'intermediate_output_dict': intermediate_output_dict,
    'Y': Y
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Tensor, ndim 0, dtype family float, shape () — shape () as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
is_list Ran
ran, violated the contract we drafted
HazyResearch/thanos-code/unagi/datasets/base_dataset.py
code served (permissive licence) · get_code("b0a00d47dac2d2f4")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned bool, ndim 0, dtype family float, shape () as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import numpy as np
from typing import Any, Dict, List, Sequence, Tuple, Union

torch.manual_seed(0)

# Test case 1: a list (should return True)
test_list = [1, 2, 3, 4]

# Test case 2: a string (should return False, even though it's a Sequence)
test_string = "hello"

# Test case 3: a tuple (is a Sequence, not a string, should return True)
test_tuple = (1, 2, 3)

# Test case 4: a numpy array (is a Sequence, not a string, should return True)
test_numpy = np.array([1, 2, 3])

# Test case 5: an integer (not a Sequence, should return False)
test_int = 42

# Test case 6: a torch tensor (is a Sequence, not a string, should return True)
test_tensor = torch.tensor([1, 2, 3, 4])

# Map parameter 'x' to various test values
ARGS = {
    "x": test_list
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
numpy, torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type bool, ndim 0, dtype family float, shape () — shape () as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
mask_loss Ran
ran on a synthesized input
HazyResearch/thanos-code/unagi/tasks/loss_modules.py
code served (permissive licence) · get_code("7cc0adbd026ba6e9")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Tensor, ndim 0, dtype family float, shape () as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import torch.nn as nn

torch.manual_seed(0)

# Create a simple loss function that takes two arguments
class MaskedLoss(nn.Module):
    def forward(self, pred, target):
        return torch.mean((pred - target) ** 2)

# Create loss_fns dict with masked_loss
loss_fns = {
    'masked_loss': MaskedLoss()
}

# Simple parameters
aug_type = 'rotation'
n_views = 2
module_names = ['encoder', 'decoder']
task_name = 'classification'

# Create intermediate_output_dict with the expected structure
# pre_encoder[0] should be a dict of field_name -> field_emb tensors
# decoder[0] should be a dict of field_name -> decoder_output tensors
field_names = ['field_a', 'field_b']
pre_encoding_embs = {}
decoder_outputs = {}

for field_name in field_names:
    pre_encoding_embs[field_name] = torch.randn(2, 4)  # batch=2, dim=4
    decoder_outputs[field_name] = torch.randn(2, 4)    # batch=2, dim=4

intermediate_output_dict = {
    'pre_encoder': [pre_encoding_embs],
    'decoder': [decoder_outputs]
}

# Y can be any value since it's not used in the function
Y = torch.randn(2, 3)

ARGS = {
    'loss_fns': loss_fns,
    'aug_type': aug_type,
    'n_views': n_views,
    'module_names': module_names,
    'task_name': task_name,
    'intermediate_output_dict': intermediate_output_dict,
    'Y': Y
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Tensor, ndim 0, dtype family float, shape () — shape () as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
multiclass_classification Ran
ran on a synthesized input
HazyResearch/thanos-code/unagi/tasks/output_layer_modules.py
code served (permissive licence) · get_code("d654687ffa322293")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Tensor, ndim 2, dtype family float, shape (2, 4) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import torch.nn as nn
from torch.nn import functional as F

torch.manual_seed(0)

# The function accesses immediate_output_dict[module_name][len(...) - 1]
# This means immediate_output_dict[module_name] must be indexable (list-like)
# and contain at least one element that is a tensor suitable for F.softmax with dim=1

# Create a tensor that will be the output
# Shape: (batch_size=2, num_classes=4) for multiclass classification
output_tensor = torch.randn(2, 4)

# immediate_output_dict[module_name] should be a list/sequence where the last element
# is our output tensor
module_name = "classifier"
immediate_output_dict = {
    module_name: [output_tensor]
}

ARGS = {
    "module_name": module_name,
    "immediate_output_dict": immediate_output_dict
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Tensor, ndim 2, dtype family float, shape (2, 4) — shape (2, 4) as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
multilabel_classification Ran
ran on a synthesized input
HazyResearch/thanos-code/unagi/tasks/output_layer_modules.py
code served (permissive licence) · get_code("13c311b0692c7407")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Tensor, ndim 2, dtype family float, shape (2, 4) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import torch.nn as nn
from torch.nn import functional as F

torch.manual_seed(0)

# The function accesses immediate_output_dict[module_name][len(...) - 1]
# This means immediate_output_dict[module_name] must be indexable (list-like)
# and we need to get the last element, then pass it to F.sigmoid()

# Create a small tensor output
output_tensor = torch.randn(2, 4)  # batch_size=2, dim=4

# Create the immediate_output_dict with a list of tensors
# The function gets the last element of this list
immediate_output_dict = {
    "classifier": [
        torch.randn(2, 4),  # some intermediate output
        output_tensor       # this will be accessed as the last element
    ]
}

module_name = "classifier"

ARGS = {
    "module_name": module_name,
    "immediate_output_dict": immediate_output_dict
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Tensor, ndim 2, dtype family float, shape (2, 4) — shape (2, 4) as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
sparse2coarse Ran
ran on a synthesized input
HazyResearch/thanos-code/unagi/datasets/tiny_imagenet/utils.py
code served (permissive licence) · get_code("0d0f48549ba480fa")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned list, ndim 1, dtype family int; no output shape is stated. The image it ran in was not recorded. If your copy returns the same type and ndim on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import numpy as np

# The function takes targets (array-like of indices) and an optional scramble parameter
# It uses the targets as indices into sparse_coarse_array, so targets should be integers
# in the range [0, 199] since sparse_coarse_array has 200 elements

# Create sample target indices - these will be used as indices into the sparse_coarse_array
targets = np.array([0, 5, 10, 15, 20, 50, 100, 150, 199, 42])

ARGS = {
    "targets": targets,
    "scramble": False
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
numpy — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type list, ndim 1, dtype family int — a list output has no array shape of its own; none is stated
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
sce_loss Not yet run
unverified (harvested, no recorded run)
HazyResearch/thanos-code/unagi/tasks/loss_modules.py
code served (permissive licence) · get_code("9c5cbb4a3040b938")
How it ran

no invocation recorded.

Also present in other papers' code

Of 7 samples lifted from this paper's linked repositories, 7 carry a function identity and 0 do not. Of the 7 distinct functions with an identity, 1 are also present in other papers whose code we hold (0 with at least one paper dated earlier, 0 the same month, 1 later) and 6 are only here among the 52,977 papers whose code Syntology holds (as of 2026-09-26).

Only here means: among the 52,977 papers whose code Syntology holds (51,887 with lifted samples, 1,090 through their assemblies only), as of 2026-09-26, no other paper's lifted samples or current assembly bricks contain an identical function body after normalisation.

FunctionAlso present inEarliest-datedOthers named
is_list 1 other paper
1 later
2311.18505 (2023-11)
String Sound Synthesizer on GPU-accelerated Finite Difference Scheme

Presence, not direction: an identical function body in two papers' repositories does not say who wrote it, which came from which, or that either paper's method rests on it. Matching is exact after normalisation, so a renamed or edited function is not matched. Dates are when papers appeared, not when code was written. Only .py files the harvester lifted are covered; notebooks are not read. A new-style arXiv id YYMM.NNNN dates a paper to the month 20YY-MM; otherwise Paper.year dates it to the year; otherwise it is undated. Dates are when the paper appeared, not when the code was written. Read 2026-11-22.

Repositories linked to this paper

Some links come from the archived Papers with Code dataset (CC BY-SA 4.0): attribution and licence.

Abstract

An ideal learned representation should display transferability and robustness. Supervised contrastive learning (SupCon) is a promising method for training accurate models, but produces representations that do not capture these properties due to class collapse-when all points in a class map to the same representation. Recent work suggests that "spreading out" these representations improves them, but the precise mechanism is poorly understood. We argue that creating spread alone is insufficient for better representations, since spread is invariant to permutations within classes. Instead, both the correct degree of spread and a mechanism for breaking this invariance are necessary. We first prove that adding a weighted class-conditional InfoNCE loss to SupCon controls the degree of spread. Next, we study three mechanisms to break permutation invariance: using a constrained encoder, adding a class-conditional autoencoder, and using data augmentation. We show that the latter two encourage clustering of latent subclasses under more realistic conditions than the former. Using these insights, we show that adding a properly-weighted class-conditional InfoNCE loss and a class-conditional autoencoder to SupCon achieves 11.1 points of lift on coarse-to-fine transfer across 5 standard datasets and 4.7 points on worst-group robustness on 3 datasets, setting state-of-the-art on CelebA by 11.5 points.

For agents

The same record, over MCP at https://syntology.ai/mcp:

get_harvested_code_for_paper("2204.07596")
get_code_for_paper("2204.07596")
have("2204.07596")

The run record, dated, one paper per request, free:

curl https://syntology.ai/api/ran/2204.07596.json

A badge for a README (the split and the date, never a ratio):

[![Syntology run record](https://syntology.ai/api/ran/2204.07596.svg)](https://syntology.ai/paper/2204.07596)

Connect an agent — have() is free.