Tensorflow Retval[0]没有值:tf.cond(条件,net1,net2)
我试图使用Tensorflow Retval[0]没有值:tf.cond(条件,net1,net2),tensorflow,tensorflow-estimator,Tensorflow,Tensorflow Estimator,我试图使用tf.cond()根据一个条件创建两个不同的图形。在这两个图上,我们都希望有权重正则化损失,因此我们使用tf.loss.get\u regulation\u loss()。这是我们项目的伪代码 def net_1(x,y): statement 1 (has trainable params) statement 2 (has trainable params) return 如果我们保留tf.loss.get\u regularization\u loss()我们会
tf.cond()
根据一个条件创建两个不同的图形。在这两个图上,我们都希望有权重正则化损失,因此我们使用tf.loss.get\u regulation\u loss()
。这是我们项目的伪代码
def net_1(x,y):
statement 1 (has trainable params)
statement 2 (has trainable params)
return
如果我们保留tf.loss.get\u regularization\u loss()
我们会得到错误:
Retval[0]没有值
否则,就没有错误
如果我们想施加
tf.loss.get\u regulation\u loss()
,tf.cond()有什么特别的注意事项吗?同样的问题,我通过用两个类似的函数替换tf.cond(与正则化相关)来修复它…现在找不到更好的解决方案。在伪代码中,您只需调用return。是否返回值?看一看文档中的。完全相同的问题在这里也试图根据步骤使用不同的损失。即使返回张量,它也不起作用。作品中的玩具例子。下面的答案是错误的。Doc明确指出tf.cond使用函数而不是张量。作为一种解决方法,它通过预先创建张量并为张量提供lambda来工作,但这意味着张量一直都在创建。我不知道如何解决这个问题。您使用的是哪个TensorFlow版本?
def net_2(x,y):
statement 1 (has trainable params)
statement 2 (has trainable params)
statement 3 (has trainable params)
return
step = tf.get_or_create_global_step()
tf.cond(tf.greater(step, 100), net_1, net_2)
loss = 0.0
loss += tf.losses.get_regularization_loss()