繁体   English   中英

如何在张量流中窗口化或重置流操作?

[英]How to window or reset streaming operations in tensorflow?

Tensorflow提供各种漂亮的流操作来汇总统计信息,例如tf.metrics.mean

但是,我发现从一开始就积累所有值通常没有多大意义。 例如,一个宁愿希望每个时期或在给定上下文中有意义的任何其他时间窗口都有统计信息。

是否有任何方法可以限制此类流统计的历史记录,例如通过重置流操作以使它们重新开始累积?

解决方法:

  • 整批手工积累
  • 使用EMA使用“软”滑动窗口

一种方法是在流操作中调用相关变量的初始化程序。 例如,

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM