2016-12-22 28 views
0

ここに私の主なタスクは以下のconv1_2 によって生成されたテンソルの値を合計することであるTypeError例外は、(「Pythonの `bool`が許可されていないとして、` tf.Tensor`を使用した。」

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. " 
+0

スタックトレースを表示します。 – user2357112

答えて

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]) 

が正常に動作します(ただし、実際に何かを達成していない)のに対し。

+0

最大プールでは、特徴ベクトル/テンソルの最大要素が使用されます。私がここで達成しようとしているのは、特徴ベクトル/テンソルのすべての値の合計です。 – user3102085

+0

max poolingには* tf.nn.max_pool *があり、軸に沿ってすべてを合計したい場合は* tf.reduce_sum *あなたが望むことをやりなさい。もしあなたがmax poolのようなものを望むならば、合計で* tf.nn.avg_pool *があります。また、標準的な畳み込みを使うこともできますが、重みは定数であると思います。 –

+0

tf.nn.avg_poolは、ウィンドウ内のすべての値の平均を返します。代わりにそれらのすべてを合計したいと思います。ここで問題となるのは、reduce_sum()は1要素だけを返しますが、ベクトルを返す必要があるということです。 – user3102085

関連する問題