This operation takes variable-length sequences (hypothesis and truth),
each provided as a SparseTensor, and computes the Levenshtein distance.
You can normalize the edit distance by length of truth by setting
normalize to true.
The operation returns a dense Tensor of shape [2, 2] with
edit distances normalized by truth lengths.
For the following inputs,
# 'hypothesis' is a tensor of shape `[2, 1]` with variable-length values:# (0,0) = ["a"]# (1,0) = ["b"]hypothesis=tf.sparse.SparseTensor([[0,0,0],[1,0,0]],["a","b"],(2,1,1))# 'truth' is a tensor of shape `[2, 2]` with variable-length values:# (0,0) = []# (0,1) = ["a"]# (1,0) = ["b", "c"]# (1,1) = ["a"]truth=tf.sparse.SparseTensor([[0,1,0],[1,0,0],[1,0,1],[1,1,0]],["a","b","c","a"],(2,2,2))normalize=True# The output would be a dense Tensor of shape `(2,)`, with edit distancesnormalizedby'truth'lengths.# output => array([0., 0.5], dtype=float32)
Args
hypothesis
A SparseTensor containing hypothesis sequences.
truth
A SparseTensor containing truth sequences.
normalize
A bool. If True, normalizes the Levenshtein distance by
length of truth.
name
A name for the operation (optional).
Returns
A dense Tensor with rank R - 1, where R is the rank of the
SparseTensor inputs hypothesis and truth.
Raises
TypeError
If either hypothesis or truth are not a SparseTensor.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.edit_distance\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L3490-L3592) |\n\nComputes the Levenshtein distance between sequences.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.edit_distance`](https://www.tensorflow.org/api_docs/python/tf/edit_distance)\n\n\u003cbr /\u003e\n\n tf.edit_distance(\n hypothesis, truth, normalize=True, name='edit_distance'\n )\n\nThis operation takes variable-length sequences (`hypothesis` and `truth`),\neach provided as a `SparseTensor`, and computes the Levenshtein distance.\nYou can normalize the edit distance by length of `truth` by setting\n`normalize` to true.\n\n#### For example:\n\nGiven the following input,\n\n- `hypothesis` is a [`tf.SparseTensor`](../tf/sparse/SparseTensor) of shape `[2, 1, 1]`\n- `truth` is a [`tf.SparseTensor`](../tf/sparse/SparseTensor) of shape `[2, 2, 2]`\n\n hypothesis = tf.SparseTensor(\n [[0, 0, 0],\n [1, 0, 0]],\n [\"a\", \"b\"],\n (2, 1, 1))\n truth = tf.SparseTensor(\n [[0, 1, 0],\n [1, 0, 0],\n [1, 0, 1],\n [1, 1, 0]],\n [\"a\", \"b\", \"c\", \"a\"],\n (2, 2, 2))\n tf.edit_distance(hypothesis, truth, normalize=True)\n \u003ctf.Tensor: shape=(2, 2), dtype=float32, numpy=\n array([[inf, 1. ],\n [0.5, 1. ]], dtype=float32)\u003e\n\nThe operation returns a dense Tensor of shape `[2, 2]` with\nedit distances normalized by `truth` lengths.\n| **Note:** It is possible to calculate edit distance between two sparse tensors with variable-length values. However, attempting to create them while eager execution is enabled will result in a `ValueError`.\n\nFor the following inputs, \n\n # 'hypothesis' is a tensor of shape `[2, 1]` with variable-length values:\n # (0,0) = [\"a\"]\n # (1,0) = [\"b\"]\n hypothesis = tf.sparse.SparseTensor(\n [[0, 0, 0],\n [1, 0, 0]],\n [\"a\", \"b\"],\n (2, 1, 1))\n\n # 'truth' is a tensor of shape `[2, 2]` with variable-length values:\n # (0,0) = []\n # (0,1) = [\"a\"]\n # (1,0) = [\"b\", \"c\"]\n # (1,1) = [\"a\"]\n truth = tf.sparse.SparseTensor(\n [[0, 1, 0],\n [1, 0, 0],\n [1, 0, 1],\n [1, 1, 0]],\n [\"a\", \"b\", \"c\", \"a\"],\n (2, 2, 2))\n\n normalize = True\n\n # The output would be a dense Tensor of shape `(2,)`, with edit distances\n normalized by 'truth' lengths.\n # output =\u003e array([0., 0.5], dtype=float32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|--------------------------------------------------------------------------------|\n| `hypothesis` | A `SparseTensor` containing hypothesis sequences. |\n| `truth` | A `SparseTensor` containing truth sequences. |\n| `normalize` | A `bool`. If `True`, normalizes the Levenshtein distance by length of `truth.` |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A dense `Tensor` with rank `R - 1`, where R is the rank of the `SparseTensor` inputs `hypothesis` and `truth`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|-------------------------------------------------------------|\n| `TypeError` | If either `hypothesis` or `truth` are not a `SparseTensor`. |\n\n\u003cbr /\u003e"]]