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)
此代码段应计算训练阶段的user_input平均值和测试阶段的输出平均值。
这是输出结果:
('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)])
但是,即使ema_mean
,在调用sess.run([mean])
时始终会对isTrain = False
进行评估。
代码中有错误吗? tensorflow版本是0.7.1
答案 0 :(得分:1)
我认为这与answered here相同。条件语句中的tf.control_dependencies
会将依赖项添加到tf.cond
本身。
因此,请尝试在ema_apply_op
函数中创建mean_var_with_update
。
答案 1 :(得分:0)
我添加了一些日志记录语句,ema_mean
似乎仅在isTrain
为假时进行评估
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)
你看
[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]
请注意,在评估完所有输入后评估Print
语句,以便最后打印外部打印语句