作为变量传递的Tensorflow内置函数不起作用。我怎样才能让它发挥作用?

时间:2017-04-22 04:51:26

标签: python-2.7 tensorflow

当我运行以下代码时,

import numpy as np
import tensorflow as tf

class Config:
    activation = tf.nn.tanh

class Sample:

    def function(self, x):
        return self.config.activation(x)

    def __init__(self, config):
        self.config = config

if __name__ == "__main__":
    with tf.Graph().as_default():
        config = Config()
        sample = Sample(config)
        with tf.Session() as sess:
            a = tf.constant(2.0)
            print sess.run(sample.function(a))

我收到此错误消息:

Traceback (most recent call last):
  File "test.py", line 27, in <module>
    print sess.run(sample.function(a))
  File "test.py", line 11, in function
    return self.config.activation(x)
  File "/Users/byungwookang/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 2019, in tanh
    with ops.name_scope(name, "Tanh", [x]) as name:
  File "/Users/byungwookang/anaconda/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Users/byungwookang/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 4185, in name_scope
    with g.as_default(), g.name_scope(n) as scope:
  File "/Users/byungwookang/anaconda/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Users/byungwookang/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2839, in name_scope
    if name:
  File "/Users/byungwookang/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 541, 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.

相反,此代码按预期工作。

import numpy as np
import tensorflow as tf

class Config:
    activation = np.tanh

class Sample:

    def function(self, x):
        return self.config.activation(x)

    def __init__(self, config):
        self.config = config

if __name__ == "__main__":
    config = Config()
    sample = Sample(config)
    print sample.function(2.0)
    print np.tanh(2.0)

它给出了

0.964027580076
0.964027580076

我很好奇为什么人们不能将张量流内置函数作为变量传递(如上面的第一个代码中所做的那样),以及是否有办法避免上述错误。特别是给出了第二个代码,其中numpy函数很好地作为变量传递,对我来说似乎很奇怪,tensorflow不允许这样做。

1 个答案:

答案 0 :(得分:0)

你的东西不起作用的原因是因为在你的情况下

print sample.function # <bound method Sample.function of <__main__.Sample instance at 0xXXX>>
print tf.nn.tanh      # <function tanh at 0xXXX>

不一样,而在你的第二种情况下,它们匹配。所以当你运行sample.function(a)时,不是tanh而是执行其他操作。

我很难理解所有这些类和函数的目的来做一个简单的工作,所以我找到了最简单的方法来修改为它起作用的任何东西:

import numpy as np
import tensorflow as tf

def config():
    return {'activation': tf.nn.tanh}

class Sample:
    def function(self, x):
        return self.config['activation'](x)

    def __init__(self, config):
        self.config = config

if __name__ == "__main__":
    with tf.Graph().as_default():  # this is also not needed
        sample = Sample(config())
        with tf.Session() as sess:
            a = tf.constant(2.0)
            print sess.run(sample.function(a))