简体   繁体   中英

How to window or reset streaming operations in tensorflow?

Tensorflow provides all sorts of nice streaming operations to aggregate statistics along batches, such as tf.metrics.mean .

However I find that accumulating all values since the beginning often does not make a lot of sense. For example, one could rather want to have statistics per epoch, or any other time window that makes sense in a given context.

Is there any way to restrict the history of such streaming statistics, for example by reseting streaming operations so that they start over the accumulation?

Work-arounds:

  • accumulate by hand accross batch
  • use a "soft" sliding window using EMA

One way to do it is to call the initializer of the relevant variables in the streaming op. For example,

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()操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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