tensorflow tf.edit_distance
函数如何工作?它如何比较存储在2d或3d密集矩阵的两个不同稀疏矩阵中的字符串。
在tensorflow网页上给出的示例https://www.tensorflow.org/api_docs/python/tf/edit_distance并不那么明显。请使用其他一些示例提供说明。
这个例子也不清楚。
#'hypothesis' is a tensor of shape [2, 1] with variable-length values:
#(0,0) = ["a"] and (1,0) = ["b"]
hypothesis = tf.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.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.5, 1.0]],
(0,0): no truth, (0,1): no hypothesis, (1,0): addition, (1,1): no hypothesis
输出的大小如何[2,2]?
规范化在这里做什么?
密集形式的假设看起来像这样
[[['a']],
[['b']]] # (2, 1, 1)
事实就是这样
[[[],['a']],
[['b', 'c'], ['a']]] # (2, 2, 2)
我们试图在假设和真值之间找到Levenshtein distance。那么,这是发生了什么:
在(0,0,0) - []中假设的['a']有多远 - 在该位置没有真相所以无法计算距离
at(0,0,1) - 因为在假设中没有任何东西我们返回1.与上面的情况不同,距离是1,因为理论上假设可以通过插入一个字符与真理相同(参见Levenshtein距离)计算)
at(1,0,0) - 事实上,['b','c']中的['b']与['b']有多远。这又是1,因为我们可以插入一个字符来使hyp与真理相同。但是,我们选择标准化输出距离。所以我们除以真值段的长度,即2.你得到0.5
在(1,0,1) - 来自['a']的[]中的[]有多远,因为在该位置没有任何东西,我们返回1
输出是(2,2)因为hyp和truth的等级是3.函数返回张量与rank(rank-1)
它通过想象我们在这里尝试做什么来帮助。假设中有2个序列,真相中有2个序列。因此,您的输出分数将使您获得每个序列中每个职位的分数。
这是一个例子,我们尝试将4个假设与真值相匹配。我认为你必须为你在评论中描述的用例的每个真实序列执行此操作 - 如果你发现更有效的东西,请告诉我:-)
import tensorflow as tf
hypothesis = tf.SparseTensor(
[[0, 0, 0],
[1, 0, 0],
[2, 0, 0],
[3, 0, 0]],
["a", "b", "c", "d"],
(4, 1, 1))
truth = tf.SparseTensor([[0, 0, 0], [0, 0, 1], [0, 1, 0]], ["b", "c", "a"], (1,2,2))
num_hyp = 4
truth = tf.sparse_concat(0, [truth] * num_hyp)
d = tf.edit_distance(hypothesis, truth)
with tf.Session() as sess:
print(sess.run(d))
输出:
[[1. 1. ]
[0.5 1. ]
[0.5 1. ]
[1. 1. ]]