Skip to content
Back to skills

Learned Distance Metric

ASecurity

Trainable nonlinear distance metric that transforms (v1-v2) and (v1-v2)^2 through a linear layer before computing squared norm

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
ai-agentspython

Works with

  • cli

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill learned-distance-metric --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Learned Distance Metric?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Learned Distance Metric
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-learned-distance-metric/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-learned-distance-metric)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cv-learned-distance-metric
description: Trainable nonlinear distance metric that transforms (v1-v2) and (v1-v2)^2 through a linear layer before computing squared norm
---

# Learned Distance Metric

## Overview

Euclidean and cosine distances treat all embedding dimensions equally. A learned distance metric passes the difference vector and its element-wise square through a linear transformation, then computes the squared norm. This approximates a Mahalanobis-like distance but with nonlinear capacity from the squared terms, learning which dimensions and interactions matter for similarity.

## Quick Start

```python
import torch
import torch.nn as nn

class LearnedMetric(nn.Module):
    def __init__(self, emb_dim=64):
        super().__init__()
        self.linear = nn.Linear(emb_dim * 2, emb_dim * 2, bias=False)

    def forward(self, v1, v2):
        d = v1 - v2
        d2 = d.pow(2)
        x = self.linear(torch.cat([d, d2], dim=-1))
        return x.pow(2).sum(dim=-1)

metric = LearnedMetric(emb_dim=128)
dist = metric(embedding_a, embedding_b)
```

## Workflow

1. Train embedding model with a fixed or learned metric
2. Initialize `LearnedMetric` matching embedding dimension
3. Train jointly with contrastive or triplet loss
4. At inference, use the learned metric for nearest-neighbor retrieval

## Key Decisions

- **No bias**: bias-free linear keeps distance symmetric and zero for identical inputs
- **Concat [d, d^2]**: captures both linear (Mahalanobis-like) and nonlinear distance components
- **Joint training**: train metric alongside the embedding model end-to-end
- **vs Mahalanobis**: Mahalanobis is linear-only; this adds nonlinear capacity with minimal parameters

## References

- [Similarity DenseNet121 [0.805LB]](https://www.kaggle.com/code/iafoss/similarity-densenet121-0-805lb-kernel-time-limit)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.