Python 张量流中while_环中反向传播的奇异行为

Python 张量流中while_环中反向传播的奇异行为,python,tensorflow,while-loop,backpropagation,Python,Tensorflow,While Loop,Backpropagation,我试图看看Tensorflow是否可以在没有预定义数量的循环的情况下通过while_循环进行反向传播。有件奇怪的事引起了我的注意: import tensorflow as tf from numpy import * tf.reset_default_graph() X = tf.Variable(5.0, dtype = float32) Y = tf.Variable(4.0, dtype = float32) def cond(iterN, Y): return iterN

我试图看看Tensorflow是否可以在没有预定义数量的循环的情况下通过while_循环进行反向传播。有件奇怪的事引起了我的注意:

import tensorflow as tf
from numpy import *

tf.reset_default_graph()

X = tf.Variable(5.0, dtype = float32)
Y = tf.Variable(4.0, dtype = float32)

def cond(iterN, Y):
    return iterN<6

def body(iterN, X):
    return iterN+1, tf.add(X,X)
    # return iterN+1, tf.multiply(X,2)

finalOutX = tf.while_loop(cond, body, [0,X], parallel_iterations=1, back_prop=True, swap_memory=True)

gra = tf.gradients(finalOutX, X)

init = tf.global_variables_initializer()

with tf.Session() as sess:

    init.run()
    X_val, graVal, = sess.run([finalOutX, gra])    

print(X_val)
print(graVal)
但是,当我将
tf.add(X,X)
替换为
tf.multiply(X,2)
时,可以执行代码并返回正确答案
gra=64


当我使用
tf.add(X,X)
时,有人能帮我理解错误的来源吗?提前谢谢

看起来像一个奇怪的TF错误。您可以提交一个问题。对于那些关心的人:将
tf.add(X,X)
更改为
tf.add(tf.multiply(X,1.0),tf.multiply(X,1.0))
是一种解决方法
InvalidArgumentError: Retval[0] does not have value