【问题标题】:Theano function equivalent in TensorflowTensorflow 中的 Theano 函数等价物
【发布时间】:2017-03-18 12:17:41
【问题描述】:

我想知道这个topic

我想用这个懒惰的 tensorflow 构造来解决 Theano.function 中的更新问题:

class TensorFlowTheanoFunction(object):
def __init__(self, inputs, outputs, session):
    self._inputs = inputs
    self._outputs = outputs
    self.session = session

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

如果我想传递一个 update 参数(如在 Theano 中),我该如何修改这个惰性调用? 我只是希望这也可以在 tensorflow 中工作:

self.new = theano.function([], [], updates=zip(old_params, params))

【问题讨论】:

    标签: tensorflow deep-learning theano


    【解决方案1】:

    只需从该线程修改 Yaroslav 的代码以使用 tf.assign,并具有控制依赖项以确保在分配发生之前计算输出:

    import tensorflow as tf
    
    class TensorFlowTheanoFunction(object):   
      def __init__(self, inputs, outputs, updates=()):
        self._inputs = inputs
        self._outputs = outputs
        self._updates = updates
    
      def __call__(self, *args, **kwargs):
        feeds = {}
        for (argpos, arg) in enumerate(args):
          feeds[self._inputs[argpos]] = arg
        try:
          outputs_identity = [tf.identity(output) for output in self._outputs]
          output_is_list = True
        except TypeError:
          outputs_identity = [tf.identity(self._outputs)]
          output_is_list = False
        with tf.control_dependencies(outputs_identity):
          assign_ops = [tf.assign(variable, replacement) 
                        for variable, replacement in self._updates]
        outputs_list = tf.get_default_session().run(
            outputs_identity + assign_ops, feeds)[:len(outputs_identity)]
        if output_is_list:
          return outputs_list
        else:
          assert len(outputs_list) == 1
          return outputs_list[0]
    
    a = tf.placeholder(dtype=tf.int32)
    b = tf.placeholder(dtype=tf.int32)
    variable = tf.get_variable(
        "variable", shape=[], dtype=tf.int32, initializer=tf.zeros_initializer)
    c = a + b + variable
    d = a - b
    sess = tf.InteractiveSession()
    sess.run(tf.initialize_all_variables())
    f = TensorFlowTheanoFunction([a, b], [c, d], updates=[(variable, variable + 1)])
    print f(1, 2)
    print f(1, 2)
    print f(0, 2)
    f = TensorFlowTheanoFunction([a, b], c, updates=[(variable, variable + 1)])
    print f(1, 2)
    print f(1, 2)
    print f(0, 2)
    

    这会在每次迭代时更新变量:

    [3, -1]
    [4, -1]
    [4, -2]
    6
    7
    7
    

    【讨论】:

    • hm,我之前的代码在这个新的函数实现中出现错误:raise TypeError("'Tensor' object is not iterable.") some stack :prev_call> val = TensorFlowTheanoFunction([self.input], self.get_output(), session)err_call> outputs_identity = [tf.identity(output) for output in self._outputs]
    • 啊,对不起。当输出是标量时,我添加了一个特殊情况。
    • 抱歉耽搁了。在我看来,在这些更正之后它可以工作,但速度要慢一些(慢 100-200 倍)。无论如何,谢谢帮助!
    • 比同等的Theano慢,还是比没有变量更新的版本慢?我很好奇你的基准是什么。
    • 大概是使用新功能的变量更新选项吧?如果您不在乎何时发生变量更新(在计算值之前或之后,或介于两者之间),您可以删除控制依赖项。即使您确实关心,看看这是否会加快速度可能会有所帮助。可能是一个有趣的新问题(我不一定有一个很好的答案给你)。
    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2019-03-24
    相关资源
    最近更新 更多