ValueError:操作“cond_25/Shape"已被标记为不可获取 [英] ValueError: Operation 'cond_25/Shape' has been marked as not fetchable

查看:28
本文介绍了ValueError:操作“cond_25/Shape"已被标记为不可获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The above error occurs when I try to do the following:

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)

并不是f1()不能访问变量cont,因为以下正确执行:

It is not that the variable cont is in-accessible by f1(), as the following exectues correctly:

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)

输出:
<代码>[[1.2. 4. 5.][5.2. 7. 8.]]

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

Can someone suggest why is this happening and how to correct it?

推荐答案

你看到的错误解释了 这里

请注意解释中的这一行.

Please note this line in the explanation.

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

Recall that all functions passed to tf.cond() or tf.while_loop() must be pure functions, and so they must not modify their environment.

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)

此代码执行没有错误.

如果您对静态和动态形状感到困惑解释得很好.

If you are confused with static and dynamic shapes this explains it well.

这篇关于ValueError:操作“cond_25/Shape"已被标记为不可获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆