[英]How to window or reset streaming operations in tensorflow?
Tensorflow提供各種漂亮的流操作來匯總統計信息,例如tf.metrics.mean
。
但是,我發現從一開始就積累所有值通常沒有多大意義。 例如,一個寧願希望每個時期或在給定上下文中有意義的任何其他時間窗口都有統計信息。
是否有任何方法可以限制此類流統計的歷史記錄,例如通過重置流操作以使它們重新開始累積?
解決方法:
一種方法是在流操作中調用相關變量的初始化程序。 例如,
import tensorflow as tf
x = tf.random_normal(())
mean_x, update_op = tf.metrics.mean(x, name='mean_x')
# get the initializers of the local variables (total and count)
my_metric_variables = [v for v in tf.local_variables() if v.name.startswith('mean_x/')]
# or maybe just
# my_metric_variables = tf.get_collection('metric_variables')
reset_ops = [v.initializer for v in my_metric_variables]
with tf.Session() as sess:
tf.local_variables_initializer().run()
for _ in range(100):
for _ in range(100):
sess.run(update_op)
print(sess.run(mean_x))
# if you comment the following out, the estimate of the mean converges to 0
sess.run(reset_ops)
如果要重置其內部變量,則可以調用tf.contrib.eager.metrics
的指標(無論是否執行都可以工作)具有init_variable()
操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.