代码之家  ›  专栏  ›  技术社区  ›  Avedis

求和技巧的梯度如何在keras中获得最大池位置?

  •  0
  • Avedis  · 技术社区  · 7 年前

    keras examples目录包含堆叠what-where自动编码器(SWWAE)的轻量级版本,它们在MNIST数据上进行训练。( https://github.com/fchollet/keras/blob/master/examples/mnist_swwae.py

    在最初的SWWAE论文中,作者使用软函数计算what和where。然而,在keras实现中,他们使用了一种技巧来获取这些位置。我想了解这个技巧。

    def getwhere(x):
        ''' Calculate the 'where' mask that contains switches indicating which
        index contained the max value when MaxPool2D was applied.  Using the
        gradient of the sum is a nice trick to keep everything high level.'''
        y_prepool, y_postpool = x
        return K.gradients(K.sum(y_postpool), y_prepool)  # How exactly does this line work?
    

    其中,y\u prepol是MxN矩阵,y\u postpool是M/2 x N/2矩阵(假设大小为2像素的标准池)。

    1 回复  |  直到 7 年前
        1
  •  2
  •   lejlot    7 年前

    让我们专注于最简单的例子,而不是真正讨论卷积,假设我们有一个向量

    x = [1 4 2]
    

    我们最大限度地利用这一点(有一个大窗口),我们得到

    mx = 4
    

    mx = x[argmax(x)]
    

    现在,恢复池使用的一个热掩码的“技巧”是

    magic = d mx / dx
    

    argmax没有梯度,但它将相应的梯度“传递”到向量中最大元素位置的元素,因此:

    d mx / dx = [0/dx[1] dx[2]/dx[2] 0/dx[3]] = [0 1 0]
    

    然而,请注意,如果内核重叠严重,则此技巧将不起作用-最终可能会得到大于“1”的值。基本上,如果一个像素被K个核最大化,那么它将具有值K,而不是1,例如:

         [1 ,2, 3]
    x =  [13,3, 1]
         [4, 2, 9]
    

    mx = [13,3]
         [13,9]
    

    梯度技巧给你

            [0, 0, 1]
    magic = [2, 0, 0]
            [0, 0, 1]