计算编辑距离(feed_dict错误)

时间:2016-06-24 19:31:06

标签: tensorflow sparse-matrix levenshtein-distance

我在Tensorflow中编写了一些代码来计算一个字符串和一组字符串之间的编辑距离。我无法弄清楚错误。

import tensorflow as tf
sess = tf.Session()

# Create input data
test_string = ['foo']
ref_strings = ['food', 'bar']

def create_sparse_vec(word_list):
    num_words = len(word_list)
    indices = [[xi, 0, yi] for xi,x in enumerate(word_list) for yi,y in enumerate(x)]
    chars = list(''.join(word_list))
    return(tf.SparseTensor(indices, chars, [num_words,1,1]))


test_string_sparse = create_sparse_vec(test_string*len(ref_strings))
ref_string_sparse = create_sparse_vec(ref_strings)

sess.run(tf.edit_distance(test_string_sparse, ref_string_sparse, normalize=True))

此代码有效,运行时会产生输出:

array([[ 0.25],
       [ 1.  ]], dtype=float32)

但是当我尝试通过稀疏占位符输入稀疏张量时,我得到一个错误。

test_input = tf.sparse_placeholder(dtype=tf.string)
ref_input = tf.sparse_placeholder(dtype=tf.string)

edit_distances = tf.edit_distance(test_input, ref_input, normalize=True)

feed_dict = {test_input: test_string_sparse,
             ref_input: ref_string_sparse}

sess.run(edit_distances, feed_dict=feed_dict)

以下是错误追溯:

Traceback (most recent call last):

  File "<ipython-input-29-4e06de0b7af3>", line 1, in <module>
    sess.run(edit_distances, feed_dict=feed_dict)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 372, in run
run_metadata_ptr)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 597, in _run
    for subfeed, subfeed_val in _feed_fn(feed, feed_val):

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 558, in _feed_fn
    return feed_fn(feed, feed_val)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 268, in <lambda>
    [feed.indices, feed.values, feed.shape], feed_val)),

TypeError: zip argument #2 must support iteration

知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:3)

TL; DR:对于currency.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") 的返回类型,请使用tf.SparseTensorValue代替tf.SparseTensor

此处的问题来自create_sparse_vec()的返回类型,tf.SparseTensor,并且在调用sess.run()时未被理解为Feed

当您提供(密集)create_sparse_vec()时,期望值类型是NumPy数组(或可以转换为数组的某些对象)。当您提供tf.Tensor时,预期值类型为tf.SparseTensorValue,类似于tf.SparseTensor,但其tf.SparseTensorindices和{{1}属性是NumPy数组(或者可以转换为数组的某些对象,如示例中的列表。

以下代码应该有效:

values