Compute the EditDistance metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute EditDistance, or asks how to score with EditDistance.
Scanned 9/11/2026
Install to Claude Code
npx -y skills add qhjqhj00/research-skills-pool --skill editdistance --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Editdistance?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/qhjqhj00-editdistance)More formats (shields.io, HTML) on the badges page.
---
name: editdistance
description: Compute the EditDistance metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute EditDistance, or asks how to score with EditDistance.
metadata:
skill_kind: metric
source_lib: torchmetrics
import_path: torchmetrics.text.EditDistance
source: library_introspection
---
# editdistance
> Metric `EditDistance` from `torchmetrics` (torchmetrics.text.EditDistance)
## When to invoke this skill
The user has predictions + ground truth and asks to evaluate with EditDistance, or
mentions `torchmetrics.text.EditDistance` directly, or wants the standard torchmetrics implementation.
## Reference signature
```python
from torchmetrics.text import EditDistance
# EditDistance(substitution_cost: int = 1, reduction: Optional[Literal['mean', 'sum', 'none']] = 'mean', **kwargs: Any) -> None
```
## Library docstring
```
Calculates the Levenshtein edit distance between two sequences.
The edit distance is the number of characters that need to be substituted, inserted, or deleted, to transform the
predicted text into the reference text. The lower the distance, the more accurate the model is considered to be.
Implementation is similar to `nltk.edit_distance <https://www.nltk.org/_modules/nltk/metrics/distance.html>`_.
As input to ``forward`` and ``update`` the metric accepts the following input:
- ``preds`` (:class:`~Sequence`): An iterable of hypothesis corpus
- ``target`` (:class:`~Sequence`): An iterable of iterables of reference corpus
As output of ``forward`` and ``compute`` the metric returns the following output:
- ``eed`` (:class:`~torch.Tensor`): A tensor with the extended edit distance score. If `reduction` is set to
``'none'`` or ``None``, this has shape ``(N, )``, where ``N`` is the batch size. Otherwise, this is a scalar.
Args:
substitution_cost: The cost of substituting one character for another.
reduction: a method to reduce metric score over samples.
- ``'mean'``: takes the mean over samples
- ``'sum'``: takes the sum over samples
- ``None`` or ``'none'``: return the score per sample
kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
Example::
Basic example with two strings. Going from “rain” -> “sain” -> “shin” -> “shine” takes 3 edits:
>>> from torchmetrics.text import EditDistance
>>> metric = EditDistance()
>>> metric(["rain"], ["shine"])
tensor(3.)
Example::
Basic example with two strings and substitution cost of 2. Going from “rain” -> “sain” -> “shin” -> “shine”
takes 3 edits, where two of them are substitutions:
>>> from torchmetrics.text import EditDistance
>>> metric = EditDistance(substitution_cost=2)
>>> metric(["rain"], ["shine"])
tensor(5.)
Example::
Multiple strings example:
>>> from torchmetrics.text import EditDistance
>>> metric = EditDistance(reduction=None)
>>> metric(["rain", "lnaguaeg"], ["shine", "language"])
tensor([3, 4], dtype=torch.int32)
>>> metric = EditDistance(reduction="mean")
>>> metr
```
## Quick recipe
```python
import torchmetrics.text as _m
score = _m.EditDistance(y_true, y_pred)
```
## Don'ts
- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is `(y_true, y_pred)` while torchmetrics is `(preds, target)`.
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!