SYNTOLOGY HomeExplorerAtlasCodeMethodologyAboutDevelopersFeedPricing
Paper · 1802.07595 · 2018

Smooth Loss Functions for Deep Top-k Classification

arXiv · PDF · Open in the Atlas

Code that ran

7 of 8 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, 0 violated it and 7 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/1802.07595.json · badge: /api/ran/1802.07595.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
oval-group/smooth-topk canonical (classifier label)
classifier basis: census
7 ran · 1 no recorded run
FunctionStatusWhere it lives
data_to_var Ran
ran on a synthesized input
oval-group/smooth-topk/experiments/epoch.py
code served (permissive licence) · get_code("2af97084bcdc48cc")
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 tuple. The image it ran in was not recorded. The code mentions cuda, and the harness aliases cuda to the CPU for every sample; whether this run needed that is not recorded. If your copy returns the same type 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

torch.manual_seed(0)

# Create small tensors for data and target
data = torch.randn(2, 4)  # batch_size=2, features=4
target = torch.randint(0, 2, (2,))  # batch_size=2, class labels

# Define ARGS dict with all parameters
ARGS = {
    "data": data,
    "target": target,
    "cuda": False,
    "volatile": False,
}
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 tuple — a tuple output has no array shape of its own; none is stated
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
delta Ran
ran on a synthesized input
oval-group/smooth-topk/topk/utils.py
code served (permissive licence) · get_code("2e2b18b4902bdec8")
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; no output shape is stated. It ran in syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1) at 2026-09-27T00:36:13Z. 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 torch
import torch.autograd as ag

torch.manual_seed(0)

# Create small tensors for testing
# y: predictions/logits, shape (batch_size,)
y = torch.tensor([0, 1], dtype=torch.long)

# labels: ground truth labels, shape (num_classes,)
labels = torch.tensor([0, 1, 2], dtype=torch.long)

# alpha: optional scaling factor
alpha = 0.5

ARGS = {
    "y": y,
    "labels": labels,
    "alpha": alpha,
}
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 — no output shape is stated: no later run recorded an output shape for this sample (observed_output and metamorphic_properties carry none; shape_contract and output_contract record ndim only)
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 syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1); 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
detect_large Ran
ran on a synthesized input
oval-group/smooth-topk/topk/utils.py
code served (permissive licence) · get_code("f114ace76abbcb50")
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 tuple, ndim 2, dtype family float; no output shape is stated. It ran in syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1) at 2026-09-27T00:36:13Z. 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 torch
import math

torch.manual_seed(0)

# x: tensor that .topk() is called on with (k+1, 1)
# This means x should be 2D with shape (batch, features)
# topk returns top k+1 values along dimension 1
# We need at least k+1 columns for topk to work
x = torch.randn(2, 5)  # batch=2, features=5

# k: used in topk(k+1, 1) and indexing top[:, k-1] and top[:, k]
# k must be at least 1 (since we index k-1) and less than number of features
k = 2

# tau: scalar multiplied with k in the comparison
tau = 0.5

# thresh: used in math.log(thresh), should be positive
thresh = 10.0

ARGS = {
    "x": x,
    "k": k,
    "tau": tau,
    "thresh": thresh
}
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 tuple, ndim 2, dtype family float — a tuple 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 syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1); 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
divide_and_conquer Ran
ran on a synthesized input
oval-group/smooth-topk/topk/polynomial/divide_conquer.py
code served (permissive licence) · get_code("e701a00b175d7aad")
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; no output shape is stated. It ran in syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1) at 2026-09-27T00:36:13Z. 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 torch
import torch.nn as nn

torch.manual_seed(0)

# The function uses mul as a callable that takes two lists of tensors
# and returns a list of tensors. We need to create a simple multiplication function.
def mul(left_list, right_list):
    """
    Multiply corresponding elements from two lists of tensors.
    Returns a list of resulting tensors.
    """
    result = []
    for left, right in zip(left_list, right_list):
        # Perform element-wise multiplication and keep 2D structure
        result.append((left * right).unsqueeze(0) if left.dim() == 1 else left * right)
    return result

# Create x: a list of 2d tensors of size (n_classes, n_roots)
# n_classes = 2, n_roots = 4 (using distinct sizes)
x = [
    torch.randn(4, 3),  # First tensor: 4 rows (n_roots), 3 columns
    torch.randn(4, 3)   # Second tensor: 4 rows (n_roots), 3 columns
]

# k: number of coefficients to obtain
k = 2

ARGS = {
    "x": x,
    "k": k,
    "mul": mul
}
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 — no output shape is stated: no later run recorded an output shape for this sample (observed_output and metamorphic_properties carry none; shape_contract and output_contract record ndim only)
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 syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1); 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
log1mexp Ran
ran on a synthesized input
oval-group/smooth-topk/topk/logarithm.py
code served (permissive licence) · get_code("b02fd556128a43d3")
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; no output shape is stated. It ran in syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1) at 2026-09-27T00:36:13Z. 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 torch

torch.manual_seed(0)

# Create a small tensor U with values <= 0
# The function expects U to contain negative values
U = torch.randn(2, 4)  # Shape (2, 4) - batch of 2, dim 4
U = -torch.abs(U)  # Ensure all values are <= 0

eps = 0.001

ARGS = {
    "U": U,
    "eps": eps,
}
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 — no output shape is stated: no later run recorded an output shape for this sample (observed_output and metamorphic_properties carry none; shape_contract and output_contract record ndim only)
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 syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1); 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
log_sum_exp Ran
ran on a synthesized input
oval-group/smooth-topk/topk/polynomial/sp.py
code served (permissive licence) · get_code("17dff13c779067fd")
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 1, dtype family float, shape (2,) 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

torch.manual_seed(0)

# Create a 2D tensor with batch size 2 and feature dimension 4
# The function expects a 2D tensor and computes log(sum(exp(x), 1))
x = torch.randn(2, 4)

ARGS = {
    "x": x
}
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 1, dtype family float, shape (2,) — shape (2,) 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
split Ran
ran on a synthesized input
oval-group/smooth-topk/topk/utils.py
code served (permissive licence) · get_code("de94bb6f1e49e85a")
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 tuple. It ran in syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1) at 2026-09-27T00:36:13Z. If your copy returns the same type 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.autograd as ag

torch.manual_seed(0)

# Analyze the function:
# - x: indexed with mask (boolean indexing), has .size(0) method, so it's a tensor
# - y: indexed with [:, None], used in gather(1, y[:, None]), so it's a tensor of indices
# - labels: converted to ag.Variable, compared with torch.ne, indexed with [None, :]
#   so it's a tensor of class labels

# The function does:
# 1. labels becomes a Variable (no grad)
# 2. mask = torch.ne(labels[None, :], y[:, None])  - broadcasts to (1, batch_size) vs (batch_size, 1)
# 3. x[mask].view(x.size(0), -1) - boolean indexing on x
# 4. x.gather(1, y[:, None]) - gather on dimension 1 of x

# So x should be 2D: (batch_size, num_classes) or similar
# y should be 1D: (batch_size,) with class indices
# labels should be 1D: (num_classes,) or (batch_size,) depending on usage

# Looking at mask = torch.ne(labels[None, :], y[:, None]):
# labels[None, :] has shape (1, labels_size)
# y[:, None] has shape (batch_size, 1)
# mask has shape (batch_size, labels_size)

# Then x[mask] is indexed by this mask, so x should have shape (batch_size, labels_size)
# x.gather(1, y[:, None]) gathers from dimension 1

batch_size = 2
num_classes = 4

# Create tensors
x = torch.randn(batch_size, num_classes)  # shape (2, 4)
y = torch.tensor([0, 2], dtype=torch.long)  # class indices for each sample
labels = torch.arange(num_classes, dtype=torch.long)  # (0, 1, 2, 3)

ARGS = {
    "x": x,
    "y": y,
    "labels": labels
}
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 tuple — a tuple output has no array shape of its own; none is stated
Environment
image syntology-harvest-sandbox:v5-deps (sha256:6a292aa1aed1); 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
log Not yet run
unverified (harvested, no recorded run)
oval-group/smooth-topk/topk/logarithm.py
code served (permissive licence) · get_code("88f07bb8493c307d")
How it ran

no invocation recorded.

Also present in other papers' code

Of 8 samples lifted from this paper's linked repositories, 8 carry a function identity and 0 do not. Of the 8 distinct functions with an identity, 3 are also present in other papers whose code we hold (0 with at least one paper dated earlier, 0 the same month, 3 later) and 5 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
delta 1 other paper
1 later
2206.07290 (2022-06)
Differentiable Top-k Classification Learning
log 1 other paper
1 later
2206.07290 (2022-06)
Differentiable Top-k Classification Learning
log1mexp 1 other paper
1 later
2206.07290 (2022-06)
Differentiable Top-k Classification Learning

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

The top-k error is a common measure of performance in machine learning and computer vision. In practice, top-k classification is typically performed with deep neural networks trained with the cross-entropy loss. Theoretical results indeed suggest that cross-entropy is an optimal learning objective for such a task in the limit of infinite data. In the context of limited and noisy data however, the use of a loss function that is specifically designed for top-k classification can bring significant improvements. Our empirical evidence suggests that the loss function must be smooth and have non-sparse gradients in order to work well with deep neural networks. Consequently, we introduce a family of smoothed loss functions that are suited to top-k optimization via deep learning. The widely used cross-entropy is a special case of our family. Evaluating our smooth loss functions is computationally challenging: a naïve algorithm would require $\mathcal{O}(\binom{n}{k})$ operations, where n is the number of classes. Thanks to a connection to polynomial algebra and a divide-and-conquer approach, we provide an algorithm with a time complexity of $\mathcal{O}(k n)$. Furthermore, we present a novel approximation to obtain fast and stable algorithms on GPUs with single floating point precision. We compare the performance of the cross-entropy loss and our margin-based losses in various regimes of noise and data size, for the predominant use case of k=5. Our investigation reveals that our loss is more robust to noise and overfitting than cross-entropy.

For agents

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

get_harvested_code_for_paper("1802.07595")
get_code_for_paper("1802.07595")
have("1802.07595")

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

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

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

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

Connect an agent — have() is free.