TypeError(“不允许使用`tf.Tensor`作为Python`bool`。”

时间:2016-12-22 19:50:10

标签: python tensorflow

我的主要任务是对conv1_2生成的张量值求和 以下是我的代码

class vgg16:
    def __init__(self, imgs, weights=None, sess=None): 
        self.imgs = imgs
        self.convlayers()
        self.fc_layers()
    self.sum_pool()
        self.probs = tf.nn.softmax(self.fc3l) #this is your inference
        if weights is not None and sess is not None:
            self.load_weights(weights, sess)
    def sum_pool(self):
    if self.conv1_2 is not None:
        return tf.add_n(self.conv1_2)

    def convlayers(self):
       .....
        ....

        # conv1_1
        with tf.name_scope('conv1_1') as scope:
           ......

            self.conv1_1 = tf.nn.relu(out, name=scope)
            self.parameters += [kernel, biases]

        # conv1_2
       .....
            self.conv1_2 = tf.nn.relu(out, name=scope)
            self.parameters += [kernel, biases]

        # pool1

    tf.Print(self.conv1_2,self.parameters)

        self.pool1=self.sum_pool()

当我运行此代码时,它会给我以下错误

TypeError: 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.

这个错误来自我调用函数add_n()的行 我无法弄清楚这一点。请告诉我我做错了什么。

回溯:

Traceback (most recent call last):
  File "/usr/lib/python2.7/pdb.py", line 1314, in main
    pdb._runscript(mainpyfile)
  File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
    self.run(statement)
  File "/usr/lib/python2.7/bdb.py", line 400, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "vgg16.py", line 268, in <module>
    vgg = vgg16(imgs, 'vgg16_weights.npz', sess)
  File "vgg16.py", line 22, in __init__
    self.convlayers()
  File "vgg16.py", line 71, in convlayers
    self.pool1=self.sum_pool()
  File "vgg16.py", line 31, in sum_pool
    return tf.add_n(self.conv1_2)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1826, in add_n
    if not inputs or not isinstance(inputs, (list, tuple)):
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 547, in __nonzero__
    raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
TypeError: 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.
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py(547)__nonzero__()
-> raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "

1 个答案:

答案 0 :(得分:0)

函数 tf.add_n 是一个逐元素的加法,并且需要一个张量列表,而不是一个张量。我不确定你想要完成什么,但是像 tf.reduce_sum 这样的东西会更合适吗?

触发问题的一个简单示例:

>>> import tensor flow as tf
>>> x = tf.zeros([10,10])
>>> y = tf.add_n(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/site-packages/tensorflow/python/ops/math_ops.py", line 1826, in add_n
    if not inputs or not isinstance(inputs, (list, tuple)):
  File "/usr/local/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 533, in __bool__
    raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
TypeError: 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.

尽管

>>> import tensorflow as tf
>>> x = tf.zeros([10,10])
>>> y = tf.add_n([x])

工作正常(但没有真正完成任何事情)。