Tensorflow:tf.contrib.data中dataset.map()的不兼容类型
将哈希表查找与Tensorflow:tf.contrib.data中dataset.map()的不兼容类型,tensorflow,tensorflow-datasets,Tensorflow,Tensorflow Datasets,将哈希表查找与tf.contrib.Dataset.map()一起使用时,会失败,并出现以下错误: TypeError:在op'hash\u table\u Lookup'中,输入类型([tf.string,tf.string,tf.int32])与预期类型([tf.string\u ref,tf.string,tf.int32])不兼容。 要复制的代码: from __future__ import absolute_import from __future__ import division
tf.contrib.Dataset.map()
一起使用时,会失败,并出现以下错误:
TypeError:在op'hash\u table\u Lookup'中,输入类型([tf.string,tf.string,tf.int32])与预期类型([tf.string\u 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\u ref
和tf.string
不兼容
奇怪的是,它期望的是tf.string\u ref
而不是tf.string
。有人知道为什么会这样吗?我能做些什么
这些问题与
table_ref
为tf.string_ref
有关,这是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
库用于表示查找表,而在内部库(从1.3开始用于实现tf.contrib.lookup
中)中则使用了更现代、更兼容的库