Keras层(函数)对应于tf.nn.conv2d_转置。

时间:2023-02-08 17:40:43

In Keras, what are the layers(functions) corresponding to tf.nn.conv2d_transpose in Tensorflow? I once saw the comment that we can Just use combinations of UpSampling2D and Convolution2D as appropriate. Is that right?

在Keras中,对应于tfnn的层(函数)是什么。在Tensorflow conv2d_transpose吗?我曾经看到过这样的评论,我们可以适当地使用UpSampling2D和卷积2d的组合。是这样吗?

In the following two examples, they all use this kind of combination.

在下面两个例子中,他们都使用这种组合。

1) In Building Autoencoders in Keras, author builds decoder as follows.

1)在Keras中构建Autoencoders时,作者构建了如下的解码器。

Keras层(函数)对应于tf.nn.conv2d_转置。

2) In an u-uet implementation, author builds deconvolution as follows

2)在u-uet实现中,作者建立了如下的反褶积。

up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)
conv6 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(up6)
conv6 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(conv6)

1 个解决方案

#1


2  

The corresponding layers in Keras are Deconvolution2D layers.

在Keras中相应的层是反卷积2d层。

It's worth to mention that you should be really careful with them because they sometimes might behave in unexpected way. I strongly advise you to read this Stack Overflow question (and its answer) before you start to use this layer.

值得一提的是,你应该对他们非常小心,因为他们有时会表现得出乎意料。在开始使用这一层之前,我强烈建议您阅读这个堆栈溢出问题(及其答案)。

UPDATE:

更新:

  1. Deconvolution is a layer which was add relatively recently - and maybe this is the reason why people advise you to use Convolution2D * UpSampling2D.
  2. 反褶积是最近添加的一层,也许这就是为什么人们建议你使用卷积2d * UpSampling2D的原因。
  3. Because it's relatively new - it may not work correctly in some cases. It also need some experience to use them properly.
  4. 因为它是相对较新的——在某些情况下它可能不能正常工作。它还需要一些经验来正确地使用它们。
  5. In fact - from a mathematical point of view - every Deconvolution might be presented as a composition of Convolution2D and UpSampling2D - so maybe this is the reason why it was mentioned in texts you provided.
  6. 事实上,从数学的角度来看,每一次反褶积都可能是由卷积2d和UpSampling2D构成的,所以这可能就是你提供的文本中提到它的原因。

UPDATE 2:

更新2:

Ok. I think I found an easy explaination why Deconvolution2D might be presented in a form of a composition of Convolution2D and UpSampling2D. We would use a definition that Deconvolution2D is a gradient of some convolution layer. Let's consider three most common cases:

好的。我想我找到了一个简单的解释,为什么解卷积2d可以以卷积2d和UpSampling2D的形式呈现。我们会用一个定义来解卷积二维是一个卷积层的梯度。让我们考虑三个最常见的案例:

  1. The easiest one is a Convolutional2D without any pooling. In this case - as it's the linear operation - its gradient is a function itself - so Convolution2D.
  2. 最简单的方法是不使用任何池的卷积。在这种情况下——因为它是线性操作——它的梯度是一个函数本身——所以是卷积2d。
  3. The more tricky one is a gradient of Convolution2D with AveragePooling. So: (AveragePooling2D * Convolution2D)' = AveragePooling2D' * Convolution2D'. But a gradient of AveragePooling2D = UpSample2D * constant - so it's also in this case when the preposition is true.
  4. 比较棘手的一个是卷积2d和平均池的梯度。所以:(AveragePooling2D *卷积2d)' = AveragePooling2D' *卷积2d '。但是,平均pooling2d = UpSample2D *常数的梯度,所以在这种情况下,当介词为真时也是如此。
  5. The most tricky one is one with MaxPooling2D. In this case still (MaxPooling2D * Convolution2D)' = MaxPooling2D' * Convolution2D' But MaxPooling2D' != UpSample2D. But in this case one can easily find an easy Convolution2D which makes MaxPooling2D' = Convolution2D * UpSample2D (intuitively - a gradient of MaxPooling2D is a zero matrix with only one 1 on its diagonal. As Convolution2D might express a matrix operation - it may also represent the injection from a identity matrix to a MaxPooling2D gradient). So: (MaxPooling2D * Convolution2D)' = UpSampling2D * Convolution2D * Convolution2D = UpSampling2D * Convolution2D'.
  6. 最棘手的是使用MaxPooling2D。在这种情况下,仍然(MaxPooling2D *卷积2d)' = MaxPooling2D' *卷积2d ',但MaxPooling2D' != UpSample2D。但在这种情况下,我们可以很容易地找到一个简单的卷积2d,它使MaxPooling2D' =卷积2d * UpSample2D(直观地——MaxPooling2D的梯度是一个零矩阵,在对角线上只有一个1)。由于卷积2d可能表示矩阵运算,它也可以表示从单位矩阵到MaxPooling2D梯度的注入。所以:(MaxPooling2D *卷积2d)' = UpSampling2D *卷积2d = UpSampling2D *卷积2d '。

The final remark is that all parts of the proof have shown that Deconvolution2D is a composition of UpSampling2D and Convolution2D instead of opposite. One can easily proof that every function of a form a composition of UpSampling2D and Convolution2D might be easily presented in a form of a composition of UpSampling2D and Convolution2D. So basically - the proof is done :)

最后的结论是,证明的所有部分都表明,反卷积2d是一种UpSampling2D和卷积2d的合成,而不是相反的。我们可以很容易地证明,一个构成UpSampling2D和卷积2d的每一个函数都可以很容易地以UpSampling2D和卷积2d的形式呈现出来。基本上,证明是这样的

#1


2  

The corresponding layers in Keras are Deconvolution2D layers.

在Keras中相应的层是反卷积2d层。

It's worth to mention that you should be really careful with them because they sometimes might behave in unexpected way. I strongly advise you to read this Stack Overflow question (and its answer) before you start to use this layer.

值得一提的是,你应该对他们非常小心,因为他们有时会表现得出乎意料。在开始使用这一层之前,我强烈建议您阅读这个堆栈溢出问题(及其答案)。

UPDATE:

更新:

  1. Deconvolution is a layer which was add relatively recently - and maybe this is the reason why people advise you to use Convolution2D * UpSampling2D.
  2. 反褶积是最近添加的一层,也许这就是为什么人们建议你使用卷积2d * UpSampling2D的原因。
  3. Because it's relatively new - it may not work correctly in some cases. It also need some experience to use them properly.
  4. 因为它是相对较新的——在某些情况下它可能不能正常工作。它还需要一些经验来正确地使用它们。
  5. In fact - from a mathematical point of view - every Deconvolution might be presented as a composition of Convolution2D and UpSampling2D - so maybe this is the reason why it was mentioned in texts you provided.
  6. 事实上,从数学的角度来看,每一次反褶积都可能是由卷积2d和UpSampling2D构成的,所以这可能就是你提供的文本中提到它的原因。

UPDATE 2:

更新2:

Ok. I think I found an easy explaination why Deconvolution2D might be presented in a form of a composition of Convolution2D and UpSampling2D. We would use a definition that Deconvolution2D is a gradient of some convolution layer. Let's consider three most common cases:

好的。我想我找到了一个简单的解释,为什么解卷积2d可以以卷积2d和UpSampling2D的形式呈现。我们会用一个定义来解卷积二维是一个卷积层的梯度。让我们考虑三个最常见的案例:

  1. The easiest one is a Convolutional2D without any pooling. In this case - as it's the linear operation - its gradient is a function itself - so Convolution2D.
  2. 最简单的方法是不使用任何池的卷积。在这种情况下——因为它是线性操作——它的梯度是一个函数本身——所以是卷积2d。
  3. The more tricky one is a gradient of Convolution2D with AveragePooling. So: (AveragePooling2D * Convolution2D)' = AveragePooling2D' * Convolution2D'. But a gradient of AveragePooling2D = UpSample2D * constant - so it's also in this case when the preposition is true.
  4. 比较棘手的一个是卷积2d和平均池的梯度。所以:(AveragePooling2D *卷积2d)' = AveragePooling2D' *卷积2d '。但是,平均pooling2d = UpSample2D *常数的梯度,所以在这种情况下,当介词为真时也是如此。
  5. The most tricky one is one with MaxPooling2D. In this case still (MaxPooling2D * Convolution2D)' = MaxPooling2D' * Convolution2D' But MaxPooling2D' != UpSample2D. But in this case one can easily find an easy Convolution2D which makes MaxPooling2D' = Convolution2D * UpSample2D (intuitively - a gradient of MaxPooling2D is a zero matrix with only one 1 on its diagonal. As Convolution2D might express a matrix operation - it may also represent the injection from a identity matrix to a MaxPooling2D gradient). So: (MaxPooling2D * Convolution2D)' = UpSampling2D * Convolution2D * Convolution2D = UpSampling2D * Convolution2D'.
  6. 最棘手的是使用MaxPooling2D。在这种情况下,仍然(MaxPooling2D *卷积2d)' = MaxPooling2D' *卷积2d ',但MaxPooling2D' != UpSample2D。但在这种情况下,我们可以很容易地找到一个简单的卷积2d,它使MaxPooling2D' =卷积2d * UpSample2D(直观地——MaxPooling2D的梯度是一个零矩阵,在对角线上只有一个1)。由于卷积2d可能表示矩阵运算,它也可以表示从单位矩阵到MaxPooling2D梯度的注入。所以:(MaxPooling2D *卷积2d)' = UpSampling2D *卷积2d = UpSampling2D *卷积2d '。

The final remark is that all parts of the proof have shown that Deconvolution2D is a composition of UpSampling2D and Convolution2D instead of opposite. One can easily proof that every function of a form a composition of UpSampling2D and Convolution2D might be easily presented in a form of a composition of UpSampling2D and Convolution2D. So basically - the proof is done :)

最后的结论是,证明的所有部分都表明,反卷积2d是一种UpSampling2D和卷积2d的合成,而不是相反的。我们可以很容易地证明,一个构成UpSampling2D和卷积2d的每一个函数都可以很容易地以UpSampling2D和卷积2d的形式呈现出来。基本上,证明是这样的