python - 为什么我得到 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)
)
)
)
解决方案
tf.cond 将操作作为 lambda 函数。
请阅读https://github.com/tensorflow/tensorflow/issues/4094了解更多详情
推荐阅读
- mdx - Mdx,大于 String 而不是 Numeric (Saiku)
- flask - Flask UWSGI ModuleNotFound
- javascript - 快照测试使用 PropTypes 的 React 连接组件
- python-3.x - 在 glob 中使用隐式路径名参数
- c++ - 如何调整 ICU 的 UnicodeString::caseCompare(或获得相同的效果)
- ruby-on-rails - 如何在 Rails 测试中正确发布记录
- javascript - Node.js mysql 结果到数组
- powershell - PowerShell 对对象进行分组并返回 ID 最大的对象
- vba - 在 vba 中获取自定义数据库属性
- java - @RequestMapping(value = "v1/firewall/policy/{zoneId:.*\\D+.*}") 做什么?