我似乎对tf.cond的工作方式产生了误解。在tensorflow 文档中,给出了以下示例:
z = tf.multiply(a, b)
result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y))示例的结果是,如果x<y是True,则为tf.add(x,z) tf.square(y) tf.square(y)。
按照这个示例,我尝试用tf.cond构建一个小示例,其结果与文档中提到的不同。
在我的例子中,deterministic_action = 4,random_action = 11,chose_random=False。stochastic_action应该是4,而不是1。1的价值从何而来?
#!/usr/bin/env python3
import tensorflow as tf
import numpy as np
with tf.Graph().as_default():
with tf.device('/cpu:0'):
stochastic_ph = tf.placeholder(tf.bool, (), name="stochastic")
eps = tf.get_variable("eps", (), initializer=tf.constant_initializer(0))
with tf.variable_scope('test_cond') as sc:
deterministic_action = tf.random_uniform([], minval=0, maxval=15, dtype=tf.int64, seed=0) # 4
random_action = tf.random_uniform([], minval=0, maxval=15, dtype=tf.int64, seed=1) # 11
chose_random = tf.random_uniform([], minval=0, maxval=1, dtype=tf.float32) < eps # False because eps = 0
stochastic_action = tf.cond(chose_random, lambda: random_action, lambda: deterministic_action) # S_action should be 4 but it is 1
#output_action = tf.cond(stochastic_ph, lambda: stochastic_action, lambda: deterministic_action)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init, feed_dict={stochastic_ph: True})
print ("s_ph = ", stochastic_ph)
d_action = sess.run(deterministic_action)
print ("det_action= ", d_action)
r_action = sess.run(random_action)
print ("rand_action= ", r_action)
e = sess.run(eps)
c_action = sess.run(chose_random)
print ("chose_rand= ", c_action)
s_action = sess.run(stochastic_action)
print ("s_action= ", s_action)
#output = sess.run(output_action)这是输出:
python random_vec.py
2018-10-31 09:46:15.028376: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
s_ph = Tensor("stochastic:0", shape=(), dtype=bool, device=/device:CPU:0)
det_action= 4
rand_action= 11
chose_rand= False
s_action= 1发布于 2018-10-31 13:27:46
这是因为您再次在新的sess.run中进行评估。由于您正在为deterministic_action生成一个随机数,结果是4之后的下一个随机数,即1。这是您的代码的结果,在最后一步提取deterministic_action的值时。
修改:
print ("s_ph = ", stochastic_ph)
d_action = sess.run(deterministic_action)
print ("det_action= ", d_action)
r_action = sess.run(random_action)
print ("rand_action= ", r_action)
e = sess.run(eps)
c_action = sess.run(chose_random)
print ("chose_rand= ", c_action)
s_action, d_action = sess.run([stochastic_action, deterministic_action])
print ("s_action= ", s_action)
print ("det_action= ", d_action)结果:
s_ph = Tensor("stochastic:0", shape=(), dtype=bool, device=/device:CPU:0)
det_action= 4
rand_action= 11
chose_rand= False
s_action= 1
det_action= 1现在您所需要做的就是在一个sess.run中运行所有的东西。
d_action, r_action, e, c_action, s_action = sess.run([deterministic_action, random_action, eps, chose_random, stochastic_action])
print ("det_action= ", d_action)
print ("rand_action= ", r_action)
print ("chose_rand= ", c_action)
print ("s_action= ", s_action)结果:
s_ph = Tensor("stochastic:0", shape=(), dtype=bool, device=/device:CPU:0)
det_action= 4
rand_action= 11
chose_rand= False
s_action= 4更新:
当设置种子时,我不清楚为什么random_uniform会生成不同的值。这是因为代码使用它初始化变量的同一个会话对象运行。使用新的session对象修改代码,会发生这样的情况:
print ("s_ph = ", stochastic_ph)
d_action = sess.run(deterministic_action)
print ("det_action= ", d_action)
sess.close()
sess = tf.Session()
sess.run(init, feed_dict={stochastic_ph: True})
s_action = sess.run(stochastic_action)
print ("s_action= ", s_action)结果:
s_ph = Tensor("stochastic:0", shape=(), dtype=bool, device=/device:CPU:0)
det_action= 4
s_action= 4https://stackoverflow.com/questions/53079436
复制相似问题