首页 > 解决方案 > 如何手动计算 tensorflow 中 conv3d 的反向传播?

问题描述

在 tensorflow 中,对于 2D 卷积,有一个有用的评论是关于如何将输入和权重的反向传播再次映射到 2D 卷积。你可以在这里找到它。

我知道卷积反向传播的基本原理。但是,我不能像 2D 卷积那样将反向传播映射到前向卷积。我也不想对反向道具使用矩阵乘法。

现在,我正在为 3D 卷积寻找类似的东西。我们如何通过使用另一个 3D 卷积计算输入和权重的 3D 卷积的反向传播?

我只想让代码如下所示:

with tf.GraientTape() as tape:
  y = tf.nn.conv3d(x, w) # forward pass
  L = tf.math.reduce_mean(y, axis = [1, 2, 3, 4]) # calculate loss for each sample in the batch which can be anything.

dl_dy, dl_dx, dl_dw = tape.gradient(L, [y, x, w])

dl_dx_est = tf.nn.conv3d(dl_dy, w) 
dl_dw_est = tf.nn.conv3d(x, dl_dy)

在上面的代码中,我们有:

x.shape = [batch, in_depth, in_height, in_width, in_channels]
w.shape = [filter_depth, filter_height, filter_width, in_channels, out_channels]
y.shape = [batch, out_depth, out_height, out_width, out_channels]

现在的目标是拥有tf.reduce_mean(tf.abs(dl_dx_est - dl_dx)) == 0.0和相同的东西dl_dw_est。我可能需要填充/旋转/转置 w 和 dl_dy 和 x 但我不知道怎么做?

总之,在 tensorflow 代码中包含类似于注释(提到的链接)的内容将是理想的。

标签: tensorflowconv-neural-networkgradientconvolutionbackpropagation

解决方案


推荐阅读