skills/cv/learned-distance-metric/SKILL.md
Trainable nonlinear distance metric that transforms (v1-v2) and (v1-v2)^2 through a linear layer before computing squared norm
npx skillsauth add wenmin-wu/ds-skills cv-learned-distance-metricInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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.
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)
LearnedMetric matching embedding dimensiondata-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF