首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tensorflow Tf.cond提供意外输出

tensorflow Tf.cond提供意外输出
EN

Stack Overflow用户
提问于 2018-10-31 08:46:24
回答 1查看 237关注 0票数 0

我似乎对tf.cond的工作方式产生了误解。在tensorflow 文档中,给出了以下示例:

代码语言:javascript
复制
z = tf.multiply(a, b)
result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y))

示例的结果是,如果x<yTrue,则为tf.add(x,z) tf.square(y) tf.square(y)

按照这个示例,我尝试用tf.cond构建一个小示例,其结果与文档中提到的不同。

在我的例子中,deterministic_action = 4random_action = 11chose_random=Falsestochastic_action应该是4,而不是1。1的价值从何而来?

代码语言:javascript
复制
#!/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)

这是输出:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-31 13:27:46

这是因为您再次在新的sess.run中进行评估。由于您正在为deterministic_action生成一个随机数,结果是4之后的下一个随机数,即1。这是您的代码的结果,在最后一步提取deterministic_action的值时。

修改:

代码语言:javascript
复制
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)

结果:

代码语言:javascript
复制
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中运行所有的东西。

代码语言:javascript
复制
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)

结果:

代码语言:javascript
复制
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对象修改代码,会发生这样的情况:

代码语言:javascript
复制
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)

结果:

代码语言:javascript
复制
s_ph =  Tensor("stochastic:0", shape=(), dtype=bool, device=/device:CPU:0)
det_action=  4
s_action=  4
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53079436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档