计算序列之间的 Levenshtein 距离。
用法
tf.edit_distance(
hypothesis, truth, normalize=True, name='edit_distance'
)参数
-
hypothesis包含假设序列的SparseTensor。 -
truth包含真值序列的SparseTensor。 -
normalize一个bool。如果True,将 Levenshtein 距离归一化为truth.的长度 -
name操作的名称(可选)。
返回
-
一个密集的
Tensor秩为R - 1,其中 R 是SparseTensor输入hypothesis和truth的秩。
抛出
-
TypeError如果hypothesis或truth不是SparseTensor。
此操作采用可变长度序列(hypothesis 和 truth),每个序列都作为 SparseTensor 提供,并计算 Levenshtein 距离。您可以通过将normalize 设置为true,按truth 的长度标准化编辑距离。
例如:
给定以下输入,
hypothesis是形状为[2, 1, 1]的tf.SparseTensortruth是形状为[2, 2, 2]的tf.SparseTensor
hypothesis = tf.SparseTensor(
[[0, 0, 0],
[1, 0, 0]],
["a", "b"],
(2, 1, 1))
truth = tf.SparseTensor(
[[0, 1, 0],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0]],
["a", "b", "c", "a"],
(2, 2, 2))
tf.edit_distance(hypothesis, truth, normalize=True)
<tf.Tensor:shape=(2, 2), dtype=float32, numpy=
array([[inf, 1. ],
[0.5, 1. ]], dtype=float32)>
该操作返回一个形状为 [2, 2] 的密集张量,其编辑距离由 truth 长度标准化。
注意:可以计算具有可变长度值的两个稀疏张量之间的编辑距离。但是,在启用即刻执行时尝试创建它们将导致 ValueError 。
对于以下输入,
# '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 distances
normalized by 'truth' lengths.
# output => array([0., 0.5], dtype=float32)
相关用法
- Python tf.experimental.dlpack.from_dlpack用法及代码示例
- Python tf.errors.InvalidArgumentError用法及代码示例
- Python tf.experimental.numpy.iinfo用法及代码示例
- Python tf.estimator.TrainSpec用法及代码示例
- Python tf.experimental.Optional.has_value用法及代码示例
- Python tf.estimator.LogisticRegressionHead用法及代码示例
- Python tf.experimental.dispatch_for_unary_elementwise_apis用法及代码示例
- Python tf.experimental.dispatch_for_api用法及代码示例
- Python tf.estimator.MultiHead用法及代码示例
- Python tf.experimental.unregister_dispatch_for用法及代码示例
- Python tf.estimator.PoissonRegressionHead用法及代码示例
- Python tf.estimator.WarmStartSettings用法及代码示例
- Python tf.experimental.tensorrt.Converter用法及代码示例
- Python tf.estimator.experimental.stop_if_lower_hook用法及代码示例
- Python tf.estimator.RunConfig用法及代码示例
- Python tf.experimental.ExtensionType用法及代码示例
- Python tf.estimator.MultiLabelHead用法及代码示例
- Python tf.experimental.Optional.get_value用法及代码示例
- Python tf.estimator.experimental.stop_if_no_increase_hook用法及代码示例
- Python tf.estimator.BaselineEstimator用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.edit_distance。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
