问题描述
我需要将多个不同的位置参数传递给 tf.while_loop 函数,这意味着 cond 的参数与 body 的参数不同。
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tfc = tf.Variable([[1.,2.],[3.,4.],[5,6],[7,8],[8,9]]) # Use variable
i = tf.constant(0)
n = tf.constant(10)
c = lambda i: tf.less(i,5)
def body(i,m,n):
m[i] = [i,n]
l = tf.add(n,1)
r = tf.add(i,1)
return m,l,r
m,r = tf.while_loop(c,body,loop_vars=[i,tfc,n])
with tf.Session() as sess:
init=tf.global_variables_initializer()
sess.run(init)
result = sess.run(m)
print(result[0])
根据 TensorFlow 文档,我可以为 cond 和 body 传递尽可能多的参数,但在示例中仅显示了一个参数 here。
这是我在传递多个参数时遇到的错误:
TypeError: <lambda>() takes 1 positional argument but 3 were given
任何有关此问题的帮助。
提前致谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)