2017-04-22 11 views
2

当我运行下面的代码,作为变量传递的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 

我很好奇,为什么一个不能越过tensorflow内置函数作为变量(如在第一代码完成以上),以及是否有办法以避免上述错误。特别是在第二个代码中,numpy函数作为变量很好地传递,我感到tensorflow不允许这样做。

+1

这很好奇! – Aaron

回答

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)) 
+0

代码在tensorflow中不起作用,但在numpy中起作用仍然很奇怪。 – Aaron

+0

@Salvardor Dali非常感谢您提供解决方案。我上面的代码是作为一个玩具代码来解释我在一个更大的代码中遇到的问题。虽然你的建议代码肯定有效,但我很好奇是否有办法使我的张量流代码的工作方式更接近我的numpy代码。另外,我很难理解为什么我会收到这样的错误信息。 – BK736

相关问题