张量流中的二元搜索和插值

张量流中的二元搜索和插值

本文介绍了张量流中的二元搜索和插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 tensorflow 中插入一个一维张量(我实际上想要相当于 np.interp).由于我找不到类似的 tensorflow op,我不得不自己执行插值.

第一步是在 x 值的排序列表中搜索 y 值中的相应索引,即执行二分搜索.我尝试为此使用 while 循环,但出现了一个神秘的运行时错误.这是一些代码:

xaxis = tf.placeholder(tf.float32, shape=100, name='xaxis')查询 = tf.placeholder(tf.float32, name='query')使用 tf.name_scope("binsearch"):up = tf.Variable(0, dtype=tf.int32, name='up')mid = tf.Variable(0, dtype=tf.int32, name='mid')down = tf.Variable(0, dtype=tf.int32, name='down')done = tf.Variable(-1, dtype=tf.int32, name='done')def cond(上,下,中,完成):返回 tf.logical_and(done1)def body(上、下、中、完成):val = tf.gather(xaxis, mid)done = tf.cond(val>query,tf.cond(tf.gather(xaxis, mid-1)<query, lambda:mid-1, lambda: -1),tf.cond(tf.gather(xaxis, mid+1)>query, lambda:mid, lambda: -1) )up = tf.cond(val>query, lambda: mid, lambda: up )down = tf.cond(val<query, lambda: mid, lambda: down )使用 tf.control_dependencies([done, up, down]):return up, down, (up+down)//2, done上、下、中、完成 = tf.while_loop(cond, body, (xaxis.shape[0]-1, 0, (xaxis.shape[0]-1)//2, -1))

结果是

AttributeError: 'int' 对象没有属性 'name'

我在 Windows 7 和 tensorflow 1.1 上使用 Python 3.6,支持 GPU.知道出了什么问题吗?谢谢.

这是完整的堆栈跟踪:

AttributeError Traceback(最近一次调用最后一次)<ipython-input-185-693d3873919c>在 <module>()19 return up, down, (up+down)//2, done20--->21 up, down, mid, done = tf.while_loop(cond, body, (xaxis.shape[0]-1, 0, (xaxis.shape[0]-1)//2, -1))c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name)2621上下文=WhileContext(parallel_iterations,back_prop,swap_memory,名称)2622 ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT,上下文)->2623 结果= context.BuildLoop(cond,body,loop_vars,shape_invariants)2624 返回结果2625c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants)2454 self.Enter()2455 original_body_result,exit_vars = self._BuildLoop(->第 2456 章2457终于:2458 self.Exit()c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)2404结构=original_loop_vars,2405 flat_sequence=vars_for_body_with_tensor_arrays->2406 body_result = body(*packed_vars_for_body)2407 如果不是 nest.is_sequence(body_result):2408 body_result = [body_result]<ipython-input-185-693d3873919c>在身体(上,下,中间,完成)11 val = tf.gather(xaxis, mid)12 完成 = tf.cond(val>query,--->13 tf.cond(tf.gather(xaxis, mid-1)<query, lambda:mid-1, lambda: -1),14 tf.cond(tf.gather(xaxis, mid+1)>query, lambda:mid, lambda: -1) )15 up = tf.cond(val>query, lambda: mid, lambda: up )c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in cond(pred, fn1, fn2, name)1746 context_f = CondContext(pred,pivot_2,branch=0)第1747章->1748 _,res_f = context_f.BuildCondBranch(fn2)第1749章第1750章c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py 在 BuildCondBranch(self, fn)1666 real_v = sparse_tensor.SparseTensor(指数,值,dense_shape)1667 其他:->1668 real_v = self._ProcessOutputTensor(v)第1669话1670 返回 original_r,结果c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in _ProcessOutputTensor(self, val)1624 """处理条件分支的输出张量."""1625 real_val = val->1626 如果 val.name 不在 self._values 中:1627 # 处理 lambda 的特殊情况:x第1628章AttributeError: 'int' 对象没有属性 'name'
解决方案

我不知道你的错误来源,但我可以告诉你 tf.while_loop 很可能是减缓.您可以实现没有循环的线性插值,如下所示:

将 numpy 导入为 np将张量流导入为 tfxaxis = tf.placeholder(tf.float32, shape=100, name='xaxis')yaxis = tf.placeholder(tf.float32, shape=100, name='yaxis')查询 = tf.placeholder(tf.float32, name='query')# 在开始和结束添加额外元素进行外推xaxis_pad = tf.concat([[tf.minimum(query - 1, xaxis[0])], xaxis, [tf.maximum(query + 1, xaxis[-1])]],axis=0)yaxis_pad = tf.concat([yaxis[:1], yaxis, yaxis[-1:]],axis=0)# 查找包含查询的区间的索引cmp = tf.cast(query >= xaxis_pad, dtype=tf.int32)差异 = cmp[1:] - cmp[:-1]idx = tf.argmin(diff)# 插值alpha = (query - xaxis_pad[idx])/(xaxis_pad[idx + 1] - xaxis_pad[idx])res = alpha * yaxis_pad[idx + 1] + (1 - alpha) * yaxis_pad[idx]# 用 f(x) = 2 * x 测试q = 5.4x = np.arange(100)y = 2 * x使用 tf.Session() 作为 sess:q_interp = sess.run(res, feed_dict={xaxis: x, yaxis: y, query: q})打印(q_interp)>>>10.8

填充部分只是为了避免在传递超出范围的值时出现问题,否则只是比较和查找值开始大于query的位置.>

I'm trying to interpolate a 1D tensor in tensorflow (I effectively want the equivalent of np.interp). Since I couldn't find a similar tensorflow op, I had to perform the interpolation myself.

The first step is to search in a sorted list of x-values for the corresponding index in the y-values i.e perform a binary search. I tried using a while-loop for this but I get a cryptic runtime error. Here's some code:

xaxis = tf.placeholder(tf.float32, shape=100, name='xaxis')
query = tf.placeholder(tf.float32, name='query')

with tf.name_scope("binsearch"):
    up   = tf.Variable(0, dtype=tf.int32, name='up')
    mid  = tf.Variable(0, dtype=tf.int32, name='mid')
    down = tf.Variable(0, dtype=tf.int32, name='down')
    done = tf.Variable(-1, dtype=tf.int32, name='done')

    def cond(up, down, mid, done):
        return tf.logical_and(done<0,up-down>1)

    def body(up, down, mid, done):
        val  = tf.gather(xaxis, mid)
        done = tf.cond(val>query,
                       tf.cond(tf.gather(xaxis, mid-1)<query, lambda:mid-1, lambda: -1),
                       tf.cond(tf.gather(xaxis, mid+1)>query, lambda:mid, lambda: -1) )
        up = tf.cond(val>query, lambda: mid, lambda: up )
        down = tf.cond(val<query, lambda: mid, lambda: down )

        with tf.control_dependencies([done, up, down]):
            return up, down, (up+down)//2, done

    up, down, mid, done = tf.while_loop(cond, body, (xaxis.shape[0]-1, 0, (xaxis.shape[0]-1)//2, -1))

This results in

AttributeError: 'int' object has no attribute 'name'

I'm using Python 3.6 on windows 7 and tensorflow 1.1 with gpu support. Any idea what's wrong?Thanks.

Here's the full stack trace:

AttributeError                            Traceback (most recent call last)
<ipython-input-185-693d3873919c> in <module>()
     19             return up, down, (up+down)//2, done
     20
---> 21     up, down, mid, done = tf.while_loop(cond, body, (xaxis.shape[0]-1, 0, (xaxis.shape[0]-1)//2, -1))

c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name)
   2621     context = WhileContext(parallel_iterations, back_prop, swap_memory, name)
   2622     ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, context)
-> 2623     result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
   2624     return result
   2625

c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants)
   2454       self.Enter()
   2455       original_body_result, exit_vars = self._BuildLoop(
-> 2456           pred, body, original_loop_vars, loop_vars, shape_invariants)
   2457     finally:
   2458       self.Exit()

c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)
   2404         structure=original_loop_vars,
   2405         flat_sequence=vars_for_body_with_tensor_arrays)
-> 2406     body_result = body(*packed_vars_for_body)
   2407     if not nest.is_sequence(body_result):
   2408       body_result = [body_result]

<ipython-input-185-693d3873919c> in body(up, down, mid, done)
     11         val  = tf.gather(xaxis, mid)
     12         done = tf.cond(val>query,
---> 13                        tf.cond(tf.gather(xaxis, mid-1)<query, lambda:mid-1, lambda: -1),
     14                        tf.cond(tf.gather(xaxis, mid+1)>query, lambda:mid, lambda: -1) )
     15         up = tf.cond(val>query, lambda: mid, lambda: up )

c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in cond(pred, fn1, fn2, name)
   1746     context_f = CondContext(pred, pivot_2, branch=0)
   1747     context_f.Enter()
-> 1748     _, res_f = context_f.BuildCondBranch(fn2)
   1749     context_f.ExitResult(res_f)
   1750     context_f.Exit()

c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in BuildCondBranch(self, fn)
   1666               real_v = sparse_tensor.SparseTensor(indices, values, dense_shape)
   1667           else:
-> 1668             real_v = self._ProcessOutputTensor(v)
   1669         result.append(real_v)
   1670     return original_r, result

c:\program files\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in _ProcessOutputTensor(self, val)
   1624     """Process an output tensor of a conditional branch."""
   1625     real_val = val
-> 1626     if val.name not in self._values:
   1627       # Handle the special case of lambda: x
   1628       self._values.add(val.name)

AttributeError: 'int' object has no attribute 'name'
解决方案

I don't know the source of your error, but I can tell you that tf.while_loop is very likely to be very slow. You can implement linear interpolation without loops like this:

import numpy as np
import tensorflow as tf

xaxis = tf.placeholder(tf.float32, shape=100, name='xaxis')
yaxis = tf.placeholder(tf.float32, shape=100, name='yaxis')
query = tf.placeholder(tf.float32, name='query')

# Add additional elements at the beginning and end for extrapolation
xaxis_pad = tf.concat([[tf.minimum(query - 1, xaxis[0])], xaxis, [tf.maximum(query + 1, xaxis[-1])]], axis=0)
yaxis_pad = tf.concat([yaxis[:1], yaxis, yaxis[-1:]], axis=0)

# Find the index of the interval containing query
cmp = tf.cast(query >= xaxis_pad, dtype=tf.int32)
diff = cmp[1:] - cmp[:-1]
idx = tf.argmin(diff)

# Interpolate
alpha = (query - xaxis_pad[idx]) / (xaxis_pad[idx + 1] - xaxis_pad[idx])
res = alpha * yaxis_pad[idx + 1] + (1 - alpha) * yaxis_pad[idx]

# Test with f(x) = 2 * x
q = 5.4
x = np.arange(100)
y = 2 * x
with tf.Session() as sess:
    q_interp = sess.run(res, feed_dict={xaxis: x, yaxis: y, query: q})
print(q_interp)
>>> 10.8

The padding part is just to avoid trouble if you pass values out of the range, but otherwise it is just a matter of comparing and finding where the values start to be bigger than query.

这篇关于张量流中的二元搜索和插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:24