Theano.function 在 Tensorflow 中等效

Theano.function equivalent in 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 中

tf.Session class 上的 run 方法非常接近 theano.function。它的 fetchesfeed_dict 论点在道德上等同于 outputsgivens

Theano 的 function returns 一个对象,其行为类似于 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]