Tensorflow 表查找 int->float [英] Tensorflow table lookup int->float

查看:24
本文介绍了Tensorflow 表查找 int->float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个包含整数(代表类)的未知维度 [?, ?] 的 2D 张量,我想获得一个相同形状的新张量,但用从查找表中获取的浮点数代替值(代表类)权重).

Given a 2D Tensor of unknown dimensions [?, ?] containing integers (representing classes), I would like to obtain a new Tensor of the same shape, but with the values replaced by floats taken from a lookup table (representing class weights).

例如:

inputs = [ [1,3,3], [2,4,2] ]
lookup table: {1: 0.2, 2: 0.25, 3: 0.1, 4: 0.45}
output: [ [0.2, 0.1, 0.1], [0.25, 0.45, 0.25] ]

我尝试用 tf.map_fn 链接两个 lambda 函数,遍历每一行,然后遍历每个元素:

I have tried to chain two lambda functions with tf.map_fn, iterating over every row, then over every element:

elem_iter = lambda y: unknown_lookup_function(y)
row_iter = lambda x: elem_iter(x)
weights = tf.map_fn(row_iter, inputs, dtype=tf.float32)

但找不到定义查找函数的正确方法.关于如何实现这种行为的任何建议?是否有我可以使用的本机操作代替 map_fn ?

but could not find a proper way of defining the lookup function. Any advice on how to implement this behaviour ? Is there a native op that I could use instead of map_fn ?

推荐答案

我想你想使用 tf.gather:

I think you want to use tf.gather:

这个想法是将查找表存储为数组.在 i 的索引处,存储输入 i 的查找值.如果您的密钥不是整数而是字符串,则需要使用 index_table_from_file.

The idea is that you store the lookup table as an array. At the index of i, you store the lookup value for input i. If your key is not integer but string, you would need to use index_table_from_file.

# Note I pad a dummpy element at index-0.
lookup_table = tf.constant([0, 0.2, 0.25, 0.1, 0.45])

inputs = tf.constant([ [1,3,3], [2,4,2] ])
output = tf.gather(lookup_table, inputs)
with tf.Session() as sess:
  print sess.run(output)

> 
  [[ 0.2         0.1         0.1       ]
   [ 0.25        0.44999999  0.25      ]]

这篇关于Tensorflow 表查找 int->float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆