tensorflow - 张量流中的条件打印节点
问题描述
我正在寻找一种在张量流中使用条件打印节点的方法,使用下面的示例代码行,其中每 10 个循环计数,它应该在控制台中打印一些东西。但这对我不起作用。任何人都可以建议吗?
谢谢,哈米德雷萨,
epsilon = tf.cond(tf.constant(counter % 10 == 0, dtype=tf.bool), true_fn=lambda:tf.Print(epsilon, [counter, epsilon], 'batch: ', summarize=10), false_fn=lambda:epsilon)
解决方案
我遇到了类似的问题,并用以下丑陋的解决方案解决了它。
我正在使用tf.print
而不是'tf.Print',因为它已被解密:
由于 true_func 和 false_func 应该返回相同的类型和形状,所以我返回一个无意义的 const。
def true_func():
printfunc = tf.print(TENSOR_TO_PRINT,summarize=-1)
with tf.control_dependencies([printfunc]):
return tf.constant(1)
def false_func():
return tf.constant(1)
shouldprint = tf.cond(YOUR_CONDITION ,true_fn = true_func ,false_fn=false_func)
然后您可以运行该shouldprint
操作或将其添加为其他操作的依赖项。
推荐阅读
- python - 如何在python中为Matrix选择特定列
- json - “方法不允许” Graph API 发布到批处理端点
- haskell - 在 Haskell 中使用 Dynamic/fromDynamic 时是否可以恢复约束?
- rust - 对存储在向量中的值的引用的生命周期
- c# - 我无法随着时间的推移(每秒)提高预制件的速度(Unity)
- javascript - 如何使用 Surveyjs 限制调查中每页的问题
- python - 我正在尝试驼峰式四个短语
- core-data - 如何在 SwiftUI 中的 Picker 选择所在的同一行包含一个 Circle,例如 HStack?
- flutter - 带有滑块颤动的网格视图
- python - 如何使用endswith()查找一个单词/siring是否以数组中的子词结尾?