为什么我将操作'cond / IsVariableInitialized'标记为不可提取

时间:2019-09-27 04:28:35

标签: python tensorflow keras

因此,当我运行以下代码时:

import tensorflow as tf
from keras import backend as K

tensor_a = K.variable([])
def return_zero():
    return 0
def get_mean():
    return K.get_value(K.tf.reduce_mean(tensor_a))

is_equal = tf.equal(tf.size(tensor_a), 0)
r = tf.cond(is_equal, return_zero, get_mean)

print(r)

我得到了错误:

ValueError: Operation 'cond/IsVariableInitialized' has been marked as not fetchable.

但是,如果运行此代码,则没有错误。有什么作用?

tensor_a = K.variable([])

print(
  K.get_value(
    tf.cond(
      tf.equal(tf.size(tensor_a), 0), 
      lambda : tf.constant(0.0), lambda: tf.reduce_mean(tensor_a)
    )
  )
)

1 个答案:

答案 0 :(得分:0)

tf.cond将操作作为lambda函数。

请阅读https://github.com/tensorflow/tensorflow/issues/4094以获得更多详细信息