Tensorflow 中的 while_loop 错误 [英] while_loop error in Tensorflow
问题描述
我尝试在 Tensorflow 中使用 while_loop,但是当我尝试从 while 循环中的 callable 返回目标 output 时,它给了我每次都会增加形状的错误.
I tried to use while_loop in Tensorflow, but when I try to return the target output from callable in while loop, it gives me an error because the shape is increased every time.
输出应包含基于数据值(输入数组)的(0 或 1)个值.如果数据值大于5返回1,否则返回0.返回值必须添加到输出
The output should be contains (0 or 1) values based on data value (input array). If data value is large than 5 return 1 else return 0. The returned value must be added into output
这是代码::
import numpy as np
import tensorflow as tf
data = np.random.randint(10, size=(30))
data = tf.constant(data, dtype= tf.float32)
global output
output= tf.constant([], dtype= tf.float32)
i = tf.constant(0)
c = lambda i: tf.less(i, 30)
def b(i):
i= tf.add(i,1)
cond= tf.cond(tf.greater(data[i-1], tf.constant(5.)), lambda: tf.constant(1.0), lambda: tf.constant([0.0]))
output =tf.expand_dims(cond, axis = i-1)
return i, output
r,out = tf.while_loop(c, b, [i])
print(out)
sess= tf.Session()
sess.run(out)
错误::
r, out = tf.while_loop(c, b, [i])
r, out = tf.while_loop(c, b, [i])
ValueError: 两个结构的元素数量不同.
ValueError: The two structures don't have the same number of elements.
第一个结构(1个元素):[tf.Tensor 'while/Identity:0' shape=()dtype=int32]
First structure (1 elements): [tf.Tensor 'while/Identity:0' shape=() dtype=int32]
第二个结构(2个元素):[tf.Tensor 'while/Add:0' shape=()dtype=int32, tf.Tensor 'while/ExpandDims:0' shape=unknowndtype=float32>]
Second structure (2 elements): [tf.Tensor 'while/Add:0' shape=() dtype=int32, tf.Tensor 'while/ExpandDims:0' shape=unknown dtype=float32>]
我使用 tensorflow-1.1.3 和 python-3.5
如何更改代码以提供目标结果?
How can I change my code to gives me the target result?
:
我根据@mrry 的答案编辑了代码,但我仍然遇到输出错误答案的问题输出是数字求和
I edit the code based on @mrry answer, but I still have an issue that the output is incorrect answer the output is numbers summation
a = tf.ones([10,4])
print(a)
a = tf.reduce_sum(a, axis = 1)
i =tf.constant(0)
c = lambda i, _:tf.less(i,10)
def Smooth(x):
return tf.add(x,2)
summ = tf.constant(0.)
def b(i,_):
global summ
summ = tf.add(summ, tf.cast(Smooth(a[i]), tf.float32))
i= tf.add(i,1)
return i, summ
r, smooth_l1 = tf.while_loop(c, b, [i, smooth_l1])
print(smooth_l1)
sess = tf.Session()
print(sess.run(smooth_l1))
输出是6.0(错误).
推荐答案
tf.while_loop()
函数要求以下四个列表的长度相同,每个元素的类型相同:
The tf.while_loop()
function requires that the following four lists have the same length, and the same type for each element:
cond
函数的参数列表(在本例中为c
).body
函数的参数列表(在本例中为b
).body
函数的返回值列表.- 表示循环变量的
loop_vars
列表.
- The list of arguments to the
cond
function (c
in this case). - The list of arguments to the
body
function (b
in this case). - The list of return values from the
body
function. - The list of
loop_vars
representing the loop variables.
因此,如果你的循环体有两个输出,你必须给b
和c
添加一个对应的参数,给loop_vars
添加一个对应的元素>:
Therefore, if your loop body has two outputs, you must add a corresponding argument to b
and c
, and a corresponding element to loop_vars
:
c = lambda i, _: tf.less(i, 30)
def b(i, _):
i = tf.add(i, 1)
cond = tf.cond(tf.greater(data[i-1], tf.constant(5.)),
lambda: tf.constant(1.0),
lambda: tf.constant([0.0]))
# NOTE: This line fails with a shape error, because the output of `cond` has
# a rank of either 0 or 1, but axis may be as large as 28.
output = tf.expand_dims(cond, axis=i-1)
return i, output
# NOTE: Use a shapeless `tf.placeholder_with_default()` because the shape
# of the output will vary from one iteration to the next.
r, out = tf.while_loop(c, b, [i, tf.placeholder_with_default(0., None)])
正如评论中所指出的,循环体(特别是对 tf.expand_dims()
的调用)似乎不正确,该程序无法按原样运行,但希望这足以让您入门.
As noted in the comments, the body of the loop (specifically the call to tf.expand_dims()
) seems to be incorrect and this program won't work as-is, but hopefully this is enough to get you started.
这篇关于Tensorflow 中的 while_loop 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!