使用字符串张量的Tensorflow字典查找 [英] Tensorflow Dictionary lookup with String tensor
问题描述
有什么方法可以在Tensorflow中基于字符串张量执行字典查找吗?
Is there any way to perform a dictionary lookup based on a String tensor in Tensorflow?
在普通的Python中,我会做类似的事情
In plain Python, I'd do something like
value = dictionary[key]
.现在,当我将key
作为String张量时,我想在Tensorflow运行时做同样的事情.像
. Now I'd like to do the same thing at Tensorflow runtime, when I have my key
as a String tensor. Something like
value_tensor = tf.dict_lookup(string_tensor)
会很好.
推荐答案
您可能会发现tensorflow.contrib.lookup
有用:
https://github.com/tensorflow/tensorflow/blob /master/tensorflow/contrib/lookup/lookup_ops.py
You might find tensorflow.contrib.lookup
helpful:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lookup/lookup_ops.py
https://www.tensorflow.org/api_docs/python/tf/contrib/lookup/HashTable
特别是,您可以执行以下操作:
In particular, you can do:
table = tf.contrib.lookup.HashTable(
tf.contrib.lookup.KeyValueTensorInitializer(keys, values), -1
)
out = table.lookup(input_tensor)
table.init.run()
print out.eval()
这篇关于使用字符串张量的Tensorflow字典查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!