0

使用带有 的哈希表查找时tf.contrib.Dataset.map(),它会失败并出现以下错误:

TypeError: In op 'hash_table_Lookup', input types ([tf.string, tf.string, tf.int32]) are not compatible with expected types ([tf.string_ref, tf.string, tf.int32])

重现代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

initializer = tf.contrib.lookup.KeyValueTensorInitializer(
    ['one', 'two', 'three'], [1, 2, 3])
hash_table = tf.contrib.lookup.HashTable(initializer, -1)

tensor = tf.convert_to_tensor(['one', 'two', 'three'])
dataset = tf.contrib.data.Dataset.from_tensor_slices(tensor)
dataset = dataset.map(lambda k: hash_table.lookup(k))

它抱怨tf.string_reftf.string不兼容。

奇怪的是它需要 atf.string_ref而不是 a tf.string。有谁知道为什么会这样以及我能做些什么?

这些问题与table_reftf.string_ref 这里有关。

4

1 回答 1

1

这是 TensorFlow 1.3 中修复的错误。如果您使用的是 TensorFlow 1.2,则以下解决方法应该有效:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

# Use internal library implementation of `lookup_ops` in TensorFlow 1.2.
from tensorflow.python.ops import lookup_ops
initializer = lookup_ops.KeyValueTensorInitializer(
    ['one', 'two', 'three'], [1, 2, 3])
hash_table = lookup_ops.HashTable(initializer, -1)

tensor = tf.convert_to_tensor(['one', 'two', 'three'])
dataset = tf.contrib.data.Dataset.from_tensor_slices(tensor)
dataset = dataset.map(lambda k: hash_table.lookup(k))

直到 TensorFlow 1.2,该tf.contrib.lookup库使用“引用类型”来表示查找表,而在内部库中(用于tf.contrib.lookup从 1.3 开始实现)使用更现代和兼容的“资源类型”

于 2017-09-06T01:24:12.670 回答