Python 2.7 作为变量传递的Tensorflow内置函数不';不行。我怎样才能让它工作?
当我运行下面的代码时Python 2.7 作为变量传递的Tensorflow内置函数不';不行。我怎样才能让它工作?,python-2.7,tensorflow,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 __n
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.
它给
0.964027580076
0.964027580076
我很好奇为什么不能将tensorflow内置函数作为变量传递(如上面第一段代码中所做的),以及是否有办法避免上述错误。特别是在第二段代码中,numpy函数作为变量很好地传递,我觉得tensorflow不允许这样做似乎很奇怪 你的东西不起作用的原因是因为在你的情况下
print sample.function # <bound method Sample.function of <__main__.Sample instance at 0xXXX>>
print tf.nn.tanh # <function tanh at 0xXXX>
这很奇怪!奇怪的是,该代码在tensorflow中不起作用,但在numpy中起作用。@Salvardor Dali非常感谢您提出的解决方案。我上面的代码是一个玩具代码,用来解释我在一个更大的代码中遇到的问题。虽然您建议的代码确实有效,但我很好奇是否有一种方法可以使我的tensorflow代码以更类似于我的numpy代码的方式工作。而且,我很难理解为什么会收到这样的错误消息。
print sample.function # <bound method Sample.function of <__main__.Sample instance at 0xXXX>>
print tf.nn.tanh # <function tanh at 0xXXX>
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))