Python ValueError:操作';条件25/形状和x27;已标记为不可获取

Python ValueError:操作';条件25/形状和x27;已标记为不可获取,python,tensorflow,Python,Tensorflow,当我尝试执行以下操作时,会出现上述错误: se = tf.Session() cont = tf.constant([[1., 2., 4., 5.], [5., 2., 7., 8.]]) def f1(): print(se.run(tf.shape(cont))) return True def f2(): return False r = tf.cond(tf.greater(tf.constant(10), tf.constant(9)), f1, f2) 完

当我尝试执行以下操作时,会出现上述错误:

se = tf.Session()
cont = tf.constant([[1., 2., 4., 5.], [5., 2., 7., 8.]])
def f1():
    print(se.run(tf.shape(cont)))
    return True
def f2():
    return False
r = tf.cond(tf.greater(tf.constant(10), tf.constant(9)), f1, f2)
完整的错误日志如下所示:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-44-ca1189c6f7a2>", line 7, in <module>
    r = tf.cond(tf.greater(tf.constant(10), tf.constant(9)), f1, f2)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 2086, in cond
orig_res_t, res_t = context_t.BuildCondBranch(true_fn)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1930, in BuildCondBranch
original_result = fn()
  File "<ipython-input-44-ca1189c6f7a2>", line 3, in f1
    print(se.run(tf.shape(cont)))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1137, in _run
    self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 484, in __init__
    self._assert_fetchable(graph, fetch.op)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 497, in _assert_fetchable
    'Operation %r has been marked as not fetchable.' % op.name)
输出:
[[1.2.4.5]
[5.2.7.8.]


有人能建议为什么会发生这种情况,以及如何纠正这种情况吗?

您看到的错误已得到解释

请注意解释中的这一行

回想一下,传递给tf.cond()或tf.while\u loop()的所有函数都必须是纯函数,因此它们不能修改其环境

此代码执行时没有错误

如果您对静态和动态形状感到困惑,那么可以很好地解释这一点

cont = tf.constant([[1., 2., 4., 5.], [5., 2., 7., 8.]])
def f1():
    print(se.run((cont)))
    return True
def f2():
    return False
r = tf.cond(tf.greater(tf.constant(10), tf.constant(9)), f1, f2)
import tensorflow as tf


se = tf.Session()
cont = tf.constant([[1., 2., 4., 5.], [5., 2., 7., 8.]])
def f1():
    print('Shape is ',tf.shape(cont))
    return True
def f2():
    return False
r = tf.cond(tf.greater(tf.constant(10), tf.constant(9)), f1, f2)