tf.lookup 说白了就是tf 的字典
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tf.lookup 说白了就是tf 的字典相关的知识,希望对你有一定的参考价值。
tf 1.x
import tensorflow as tf
keys_tensor = tf.constant([1, 2])
vals_tensor = tf.constant([3, 4])
input_tensor = tf.constant([1, 5])
table = tf.lookup.StaticHashTable(
tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1)
out = table.lookup(input_tensor)
with tf.Session() as sess:
sess.run(tf.tables_initializer())
print(sess.run(out))
tf 2.x
import tensorflow as tf
tf.enable_eager_execution()
keys_tensor = tf.constant([1, 2])
vals_tensor = tf.constant([3, 4])
input_tensor = tf.constant([[1, 5],[2,1]])
table = tf.lookup.StaticHashTable(
tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1)
print(table.lookup(input_tensor))
以上是关于tf.lookup 说白了就是tf 的字典的主要内容,如果未能解决你的问题,请参考以下文章