不允许将tf.tensor用作python bool

时间:2018-11-30 19:35:07

标签: tensorflow typeerror

我通读了先前所有有关此错误的文章,并查看了我的代码,但我陷于泥潭。有人可以绞死我吗?

我正在尝试创建其他准确性模型。我想确定模型可以正确确定多少次

def compute_accuracy(yhat, yActual):
  i = 0
  count = 0

  c = lambda i, count, y, l: tf.less(i, tf.shape(y)[0])

  b = lambda i, count, y, l: [i+1, tf.cond(((tf.greater(y[i], 0) and tf.greater(l[i], 0)) or (tf.less(y[i], 0) and tf.less(l[i], 0))), lambda: count+1, lambda: count)]

  res = tf.while_loop(c, b, [i, count, y_hat, yActual])
  return count/i 

我仅使用图方法来遍历动态张量。

堆栈跟踪:

---> 20     accuracy, acc_ops = compute_accuracy(y_hat, y)
        21     tf.summary.scalar("Accuracy", accuracy)
        22     # Define the MSE loss and RMSProp optimizer over it.
    \n<ipython-input-22-5cd69b76cb89> in compute_accuracy(y_hat, labels)
    ---> 45     res = tf.while_loop(c, b, [i, count, y_hat, labels])
    \n~\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-
packages\tensorflow\python\ops\control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name)
       2814     loop_context = WhileContext(parallel_iterations, back_prop, swap_memory)  # pylint: disable=redefined-outer-name
       2815     ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, loop_context)
     -> 2816     result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
       2817     return result
       2818 
    \n~\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants)
       2638       self.Enter()
       2639       original_body_result, exit_vars = self._BuildLoop(
    -> 2640           pred, body, original_loop_vars, loop_vars, shape_invariants)
       2641     finally:
       2642       self.Exit()
    \n~\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)
       2588         structure=original_loop_vars,
       2589         flat_sequence=vars_for_body_with_tensor_arrays)
    -> 2590     body_result = body(*packed_vars_for_body)
       2591     if not nest.is_sequence(body_result):
       2592       body_result = [body_result]
    \n<ipython-input-22-5cd69b76cb89> in <lambda>(i, count, y, l)
         31     i = 0
         32     c = lambda i, count, y, l: tf.less(i, tf.shape(y)[0])
    ---> 33     b = lambda i, count, y, l: [i+1, tf.cond(((tf.greater(y[i], 0) and tf.greater(l[i], 0)) or (tf.less(y[i], 0) and tf.less(l[i], 0))), lambda: count+1, lambda: count)]
    \n~\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py in __bool__(self)
        526       `TypeError`.
        527     """
    --> 528     raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
        529                     "Use `if t is not None:` instead of `if t:` to test if a "
        530                     "tensor is defined, and use TensorFlow ops such as "
    \nTypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.\n

0 个答案:

没有答案