Tensorflow:tf.contrib.data 中 dataset.map() 的不兼容类型

Tensorflow: Incompatible types for dataset.map() in tf.contrib.data

tf.contrib.Dataset.map() 中使用散列 table 查找时失败并出现以下错误:

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 不兼容。

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

问题与 table_ref 相关 tf.string_ref here

这是一个已在 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 to represent the lookup tables, whereas in the internal library (used to implement tf.contrib.lookup from 1.3 onwards) the more modern and compatible