tf.edit_distance
| Просмотреть исходный код на GitHub |
Вычисляет расстояние Левенштейна между последовательностями.
tf.edit_distance(
hypothesis, truth, normalize=True, name='edit_distance'
)
Эта операция принимает последовательности переменной длины (hypothesis и truth), каждая из которых предоставляется как SparseTensor, и вычисляет расстояние Левенштейна. Вы можете нормализовать расстояние Левенштейна относительно длины truth путём установки normalize в значение true.
Например, при следующих входных данных:
# '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
Эта операция вернёт следующее:
# 'output' is a tensor of shape `[2, 2]` with edit distances normalized
# by 'truth' lengths.
output ==> [[inf, 1.0], # (0,0): no truth, (0,1): no hypothesis
[0.5, 1.0]] # (1,0): addition, (1,1): no hypothesis
| Аргументы | |
|---|---|
hypothesis | A SparseTensor содержащий гипотетические последовательности. |
truth | A SparseTensor содержащий истинные последовательности. |
normalize | A bool. Если True, нормализует расстояние Левенштейна относительно длины truth. |
name | Имя операции (необязательно). |
| Возвращаемое значение | |
|---|---|
A dense Tensor с рангом R - 1, где R — ранг SparseTensor входных hypothesis и truth. |
| Исключения | |
|---|---|
TypeError | Если либо hypothesis либо truth не являются SparseTensor. |
© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/edit_distance