首页 > 解决方案 > 为什么我得到 Operation 'cond/IsVariableInitialized' 已被标记为不可获取

问题描述

所以当我运行下面的代码时:

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)
    )
  )
)

标签: pythontensorflowkeras

解决方案


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

请阅读https://github.com/tensorflow/tensorflow/issues/4094了解更多详情


推荐阅读