← All models

text-classification (regression) · text · WASM · ~58 MB

STS-B similarity — how alike are two sentences, on a 0–5 scale

This is a cross-encoder regressor. Instead of turning each sentence into a vector and comparing them, it reads both sentences together and outputs a single graded number: how semantically similar they are, from 0 (unrelated) to 5 (essentially the same thing) — the human scale from the STS-B benchmark. Type two sentences and watch the gauge, then look inside at the raw regression output it produces. Nothing leaves your device.

At a glance

Modelcross-encoder/stsb-TinyBERT-L4
Task / pipelinetext-classification with num_labels = 1 — used as a regression head
Input → outputa (sentence-A, sentence-B) pair encoded together → one similarity score
Scale0–5 (STS-B human scale): sigmoid(logit) gives 0–1, ×5 gives the 0–5 score
Architecture4-layer TinyBERT cross-encoder fine-tuned on the STS-B regression data (STSb Spearman ≈ 85.5)
Quantizationfp32 ONNX (this small model is only ~58 MB unquantised)
BackendWebAssembly (runs anywhere; no WebGPU needed)
Download~58 MB, cached after first load
LicenseApache-2.0
Web APIsWebAssembly, Web Workers — all Baseline widely available

Run it

Enter two sentences. The model scores how similar they mean, 0–5. Try the samples to see the full range — identical, paraphrase, related, and a negation trap.

See inside — one number, joint-encoded

A bi-encoder would embed each sentence on its own and take a cosine. A cross-encoder instead runs both sentences through the transformer together — attention flows across the pair — and the regression head emits a single value. That's why it catches subtleties (negation, near-paraphrases) that independent embeddings miss. Here's the exact chain from the model's raw output to the 0–5 gauge above:

Score a pair above to see the raw regression output.

The one raw logit is the honest model output; the sigmoid + ×5 are the fixed transform STS-B training implies. Nothing is thresholded into a yes/no — this is a graded regressor, not a classifier.

Why it matters

Most "are these the same?" tooling collapses meaning into a binary or a relevance rank. STS gives you a calibrated degree of similarity, so you can set a real threshold: dedupe support tickets above 4.2, flag possible paraphrases between 3 and 4, ignore anything below 1. Because it's a cross-encoder it reads the pair jointly, so "I love this" vs "I hate this" scores low even though the two sentences share most of their words — a bag-of-words or even a bi-encoder often gets that wrong. And at 58 MB it runs in a tab, so you can score private text on-device.

Use cases

Basics

Score any two sentences 0–5 and read the graded similarity off the gauge.

Practical

Dedup a list: pick a real cutoff and flag near-duplicate sentences.

Wild

Negation traps and subtle near-paraphrases — where word-overlap gets it wrong.

Multi-model

A bi-encoder shortlists candidate pairs; this cross-encoder scores the top ones precisely.

How the API works

A cross-encoder scores sentence pairs; we read the raw regression logit off the model directly:

import { AutoTokenizer, AutoModelForSequenceClassification } from "@huggingface/transformers";

const model_id = "cross-encoder/stsb-TinyBERT-L4";
const tokenizer = await AutoTokenizer.from_pretrained(model_id);
const model = await AutoModelForSequenceClassification.from_pretrained(model_id, { dtype: "fp32" });

// Encode the two sentences AS A PAIR — [CLS] A [SEP] B [SEP]:
const inputs = tokenizer(["A man is playing a guitar."], {
  text_pair: ["A person is playing a musical instrument."],
  padding: true, truncation: true,
});
const { logits } = await model(inputs);   // one regression logit
const score5 = (1 / (1 + Math.exp(-logits.data[0]))) * 5; // → 0–5 similarity

The page loads the model through the shared loader as a text-classification pipeline, then reuses pipe.model + pipe.tokenizer to read the raw logit (the pipeline would relabel it). All inference runs in a Web Worker so scoring never janks the page.

References