Theano.function等效于Tensorflow

时间:2016-02-12 16:00:49

标签: python numpy theano tensorflow

我想知道是否有任何等同于

theano.function(inputs=[x,y], # list of input variables
outputs=..., # what values to be returned
updates=..., # “state” values to be modified
givens=...,  # substitutions to the graph)
TensorFlow中的

2 个答案:

答案 0 :(得分:5)

tf.Session上的run方法非常接近theano.function。它的fetchesfeed_dict参数是outputsgivens的道德等价物。

答案 1 :(得分:1)

Theano的function返回一个像Python函数一样的对象,并在调用时执行计算图。在TensorFlow中,您使用会话的run方法执行计算图。如果你想拥有一个类似的Theano风格的函数对象,你可以使用下面的TensorFlowTheanoFunction包装作为theano的function

的替代品。
class TensorFlowTheanoFunction(object):   
  def __init__(self, inputs, outputs):
    self._inputs = inputs
    self._outputs = outputs

  def __call__(self, *args, **kwargs):
    feeds = {}
    for (argpos, arg) in enumerate(args):
      feeds[self._inputs[argpos]] = arg
    return tf.get_default_session().run(self._outputs, feeds)

a = tf.placeholder(dtype=tf.int32)
b = tf.placeholder(dtype=tf.int32)
c = a+b
d = a-b
sess = tf.InteractiveSession()
f = TensorFlowTheanoFunction([a, b], [c, d])
print f(1, 2)

你会看到

[3, -1]
相关问题