Tensorflow tf.cond评估两个山脚
此代码段应计算整个培训阶段的用户输入平均值和测试阶段的输出平均值 这是输出结果:Tensorflow tf.cond评估两个山脚,tensorflow,Tensorflow,此代码段应计算整个培训阶段的用户输入平均值和测试阶段的输出平均值 这是输出结果: import tensorflow as tf import numpy as np isTrain = tf.placeholder(tf.bool) user_input = tf.placeholder(tf.float32) # ema = tf.train.ExponentialMovingAverage(decay=.5) with tf.device('/cpu:0'): beta =
import tensorflow as tf
import numpy as np
isTrain = tf.placeholder(tf.bool)
user_input = tf.placeholder(tf.float32)
# ema = tf.train.ExponentialMovingAverage(decay=.5)
with tf.device('/cpu:0'):
beta = tf.Variable(tf.ones([1]))
batch_mean = beta.assign(user_input)
ema = tf.train.ExponentialMovingAverage(decay=0.5)
ema_apply_op = ema.apply([batch_mean])
ema_mean = ema.average(batch_mean)
def mean_var_with_update():
with tf.control_dependencies([ema_apply_op]):
return tf.identity(batch_mean)
mean = tf.cond(isTrain,
mean_var_with_update,
lambda: (ema_mean))
# ======= End Here ==========
saver = tf.train.Saver()
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
u_input = [[2], [3], [4] ]
for u in u_input:
aa = sess.run([mean], feed_dict={user_input:u, isTrain: True })
print("Train", aa)
for u in u_input:
aa = sess.run([ema_mean], feed_dict={user_input:u, isTrain: False })
print("Test correct", aa)
for u in u_input:
aa = sess.run([mean], feed_dict={user_input:u, isTrain: False })
print("Test", aa)
然而,ema\u mean
在调用sess.run([mean])
时总是得到评估,即使isTrain=False
代码中有错误吗?tensorflow版本是0.7.1我添加了一些日志语句,
ema\u mean
似乎只有在isTrain
为false时才会得到评估
('Train', [array([ 2.], dtype=float32)])
('Train', [array([ 3.], dtype=float32)])
('Train', [array([ 4.], dtype=float32)])
('Test correct', [array([ 3.], dtype=float32)])
('Test correct', [array([ 3.], dtype=float32)])
('Test correct', [array([ 3.], dtype=float32)])
('Test', [array([ 2.5], dtype=float32)])
('Test', [array([ 2.75], dtype=float32)])
('Test', [array([ 3.375], dtype=float32)])
你看
tf.reset_default_graph()
isTrain = tf.placeholder(tf.bool)
user_input = tf.placeholder(tf.float32)
# ema = tf.train.ExponentialMovingAverage(decay=.5)
with tf.device('/cpu:0'):
beta = tf.Variable(tf.ones([1]))
batch_mean = beta.assign(user_input)
ema = tf.train.ExponentialMovingAverage(decay=0.5)
ema_apply_op = ema.apply([batch_mean])
ema_mean = ema.average(batch_mean)
def mean_var_with_update():
with tf.control_dependencies([ema_apply_op]):
return tf.Print(tf.identity(batch_mean), ["mean_var_with_update"])
#return tf.identity(batch_mean)
mean = tf.Print(tf.cond(isTrain,
mean_var_with_update,
lambda: (tf.Print(ema_mean, ["ema_mean"]))),
["evaluating mean", isTrain])
# ======= End Here ==========
saver = tf.train.Saver()
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
u_input = [[2], [3], [4] ]
for u in u_input:
aa = sess.run([mean], feed_dict={user_input:u, isTrain: True })
print("Train", aa)
for u in u_input:
aa = sess.run([ema_mean], feed_dict={user_input:u, isTrain: False })
print("Test correct", aa)
for u in u_input:
aa = sess.run([mean], feed_dict={user_input:u, isTrain: False })
print("Test", aa)
请注意,
Print
语句是在对所有输入进行求值之后进行求值的,因此外部Print语句是最后一次打印的我认为这与。条件中的tf.control\u依赖项将把依赖项添加到tf.cond
本身
因此,尝试在具有更新功能的mean\u var\u函数中创建ema\u apply\u op
。如果isTrain=False
则不应评估批处理平均值如果isTrain=True
则不应评估ema\u平均值,无论isTrain
值如何,都会评估这两个值。这是正确的行为吗?@24hours:可能是。尝试在带有更新功能的mean\u var\u内创建ema\u apply\u op
。
[mean_var_with_update]
[evaluating mean][True]
[mean_var_with_update]
[evaluating mean][True]
[mean_var_with_update]
[evaluating mean][True]
[ema_mean]
[evaluating mean][False]
[ema_mean]
[evaluating mean][False]
[ema_mean]
[evaluating mean][False]