如何在tensorflow中窗口或重置流操作?

时间:2018-05-02 14:41:47

标签: python tensorflow

Tensorflow提供各种不错的流式传输操作,以便按批次聚合统计信息,例如tf.metrics.mean

但是我发现从一开始就累积所有值通常没有多大意义。例如,人们可能更愿意在每个纪元或任何其他在给定上下文中有意义的时间窗口中拥有统计数据。

有没有办法限制此类流式传输统计信息的历史记录,例如重新设置流式传输操作以便重新开始累积?

变通:

  • 手工累积批次
  • 使用" soft"使用EMA滑动窗口

2 个答案:

答案 0 :(得分:0)

一种方法是在流式操作中调用相关变量的初始值设定项。例如,

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)

答案 1 :(得分:0)

tf.contrib.eager.metrics中的指标(无论是否有执行都有效)都有一个init_variable()操作,如果你想重置内部变量,可以调用它。