Tensorflow:为什么tf.case会给我错误的结果?

时间:2017-03-10 21:45:31

标签: python tensorflow python-2.x

我尝试使用learning_ratehttps://www.tensorflow.org/api_docs/python/tf/case)来有条件地更新Tensor。如图所示,我尝试在0.01时将global_step == 2更新为0.001,并在global_step == 4时更新为global_step == 2

然而,当learning_rate = 0.001时,我已经获得了tf.case。进一步检查后,global_step == 2 0.0010.01代替0.01)时,0.001给出了错误的结果。即使import tensorflow as tf global_step = tf.Variable(0, dtype=tf.int64) train_op = tf.assign(global_step, global_step + 1) learning_rate = tf.Variable(0.1, dtype=tf.float32, name='learning_rate') # Update the learning_rate tensor conditionally # When global_step == 2, update to 0.01 # When global_step == 4, update to 0.001 cases = [] case_tensors = [] for step, new_rate in [(2, 0.01), (4, 0.001)]: pred = tf.equal(global_step, step) fn_tensor = tf.constant(new_rate, dtype=tf.float32) cases.append((pred, lambda: fn_tensor)) case_tensors.append((pred, fn_tensor)) update = tf.case(cases, default=lambda: learning_rate) updated_learning_rate = tf.assign(learning_rate, update) print tf.__version__ with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for _ in xrange(6): print sess.run([global_step, case_tensors, update, updated_learning_rate]) sess.run(train_op) 的谓词评估为True,并且1.0.0 [0, [(False, 0.0099999998), (False, 0.001)], 0.1, 0.1] [1, [(False, 0.0099999998), (False, 0.001)], 0.1, 0.1] [2, [(True, 0.0099999998), (False, 0.001)], 0.001, 0.001] [3, [(False, 0.0099999998), (False, 0.001)], 0.001, 0.001] [4, [(False, 0.0099999998), (True, 0.001)], 0.001, 0.001] [5, [(False, 0.0099999998), (False, 0.001)], 0.001, 0.001] 的谓词评估为False,也会发生这种情况。

我做错了什么,或者这是一个错误?

TF版本:1.0.0

代码:

    <recordTarget>
        <patientRole>
          <patient>
           <religiousAffiliationCode code="1013" displayName="Christian (non-Catholic, non-specific)" codeSystem="2.16.840.1.113883.5.1076" codeSystemName="HL7 Religious Affiliation"/>
   </patient>
</patientRole>
</recordTarget>

结果:

<xsl:variable name="religion" select="n1:recordTarget/n1:patientRole/n1:patient/n1:religiousAffilliationCode/@displayName"/>

1 个答案:

答案 0 :(得分:1)

https://github.com/tensorflow/tensorflow/issues/8776

回答了这个问题

事实证明,tf.case行为是未定义的,如果在fn_tensors中,lambdas返回一个在lambda之外创建的张量。正确的用法是定义lambdas,使它们返回一个新创建的张量。

根据链接的Github问题,这种用法是必需的,因为tf.case必须创建张量本身,以便将张量的输入连接到谓词的正确分支。