tf.ExponentialMovingAverage 的结果不符合预期
The result of tf.ExponentialMovingAverage is not as expected
我想研究一下 tf.ExponentialMovingAverage 是如何工作的。这是代码:
w1 = tf.constant(10., dtype=tf.float32)
w2 = tf.constant(20., dtype=tf.float32)
w3 = tf.constant(40., dtype=tf.float32)
tf.add_to_collection('w', w1)
tf.add_to_collection('w', w2)
tf.add_to_collection('w', w3)
w = tf.get_collection('w')
ema = tf.train.ExponentialMovingAverage(decay=0.9)
ema_op = ema.apply(w)
with tf.control_dependencies([ema_op]):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in w:
print(sess.run(ema.average(i)))
结果是:
1.0000002
2.0000005
4.000001
但是根据tf.ExponentialMovingAverage中的公式,结果应该是
0.9 * 0 + (1 - 0.9) * 10. = 1.0
0.9 * 1.0 + (1 - 0.9) * 20. = 2.9
0.9 * 2.9 + (1 - 0.9) * 40 = 6.61
好像tf.ExponentialMovingAverage没有使用上一个影子值更新影子值,而是为每次迭代独立计算移动平均值。
我是不是想错了?如有任何帮助,我们将不胜感激!
您的示例中存在一些误解:
- 移动平均线是根据变量或张量定义的。您有效地为每个常量创建了移动平均线(解释您得到的结果)。
- 每次要更新移动平均线时都必须调用
ema_op
。
- 移动平均线是用您的变量的初始值初始化的(不是您期望的零)。
以下示例的行为符合您的预期:
import tensorflow as tf
w = tf.Variable(0.0, dtype=tf.float32)
ema = tf.train.ExponentialMovingAverage(decay=0.9)
ema_op = ema.apply([w])
assigns = []
with tf.control_dependencies([ema_op]):
for val in [10., 20., 40.]:
assigns.append(tf.assign(w, tf.constant(val)))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for assign in assigns:
_, _, w_ = sess.run([ema_op, assign, ema.average(w)])
print w_
_, w_ = sess.run([ema_op, ema.average(w)])
print w_
结果输出为:
0.0
1.0000002
2.9000006
6.6100016
这是为 更新的,这样您将始终获得
的正确输出
0.0
1.0000002
2.9000006
6.6100016
代码如下:
import tensorflow as tf
w = tf.Variable(0.0, dtype=tf.float32)
ema = tf.train.ExponentialMovingAverage(decay=0.9)
ema_op = ema.apply([w])
assigns = []
with tf.control_dependencies([ema_op]):
for val in [10., 20., 40.]:
assigns.append(tf.assign(w, tf.constant(val)))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for assign in assigns:
sess.run(assign)
print(sess.run(ema.average(w)))
sess.run(ema_op)
print(sess.run(ema.average(w)))
我想研究一下 tf.ExponentialMovingAverage 是如何工作的。这是代码:
w1 = tf.constant(10., dtype=tf.float32)
w2 = tf.constant(20., dtype=tf.float32)
w3 = tf.constant(40., dtype=tf.float32)
tf.add_to_collection('w', w1)
tf.add_to_collection('w', w2)
tf.add_to_collection('w', w3)
w = tf.get_collection('w')
ema = tf.train.ExponentialMovingAverage(decay=0.9)
ema_op = ema.apply(w)
with tf.control_dependencies([ema_op]):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in w:
print(sess.run(ema.average(i)))
结果是:
1.0000002
2.0000005
4.000001
但是根据tf.ExponentialMovingAverage中的公式,结果应该是
0.9 * 0 + (1 - 0.9) * 10. = 1.0
0.9 * 1.0 + (1 - 0.9) * 20. = 2.9
0.9 * 2.9 + (1 - 0.9) * 40 = 6.61
好像tf.ExponentialMovingAverage没有使用上一个影子值更新影子值,而是为每次迭代独立计算移动平均值。
我是不是想错了?如有任何帮助,我们将不胜感激!
您的示例中存在一些误解:
- 移动平均线是根据变量或张量定义的。您有效地为每个常量创建了移动平均线(解释您得到的结果)。
- 每次要更新移动平均线时都必须调用
ema_op
。 - 移动平均线是用您的变量的初始值初始化的(不是您期望的零)。
以下示例的行为符合您的预期:
import tensorflow as tf
w = tf.Variable(0.0, dtype=tf.float32)
ema = tf.train.ExponentialMovingAverage(decay=0.9)
ema_op = ema.apply([w])
assigns = []
with tf.control_dependencies([ema_op]):
for val in [10., 20., 40.]:
assigns.append(tf.assign(w, tf.constant(val)))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for assign in assigns:
_, _, w_ = sess.run([ema_op, assign, ema.average(w)])
print w_
_, w_ = sess.run([ema_op, ema.average(w)])
print w_
结果输出为:
0.0
1.0000002
2.9000006
6.6100016
这是为
0.0
1.0000002
2.9000006
6.6100016
代码如下:
import tensorflow as tf
w = tf.Variable(0.0, dtype=tf.float32)
ema = tf.train.ExponentialMovingAverage(decay=0.9)
ema_op = ema.apply([w])
assigns = []
with tf.control_dependencies([ema_op]):
for val in [10., 20., 40.]:
assigns.append(tf.assign(w, tf.constant(val)))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for assign in assigns:
sess.run(assign)
print(sess.run(ema.average(w)))
sess.run(ema_op)
print(sess.run(ema.average(w)))