tensorflow 卷积/反卷积-池化/反池化操作详解

  • Plese see this answer for a detailed example of how tf.nn.conv2d_backprop_input and tf.nn.conv2d_backprop_filter in an example.

In tf.nn, there are 4 closely related 2d conv functions:

  • tf.nn.conv2d
  • tf.nn.conv2d_backprop_filter
  • tf.nn.conv2d_backprop_input
  • tf.nn.conv2d_transpose

Given out = conv2d(x, w) and the output gradient d_out:

  • Use tf.nn.conv2d_backprop_filter to compute the filter gradient d_w
  • Use tf.nn.conv2d_backprop_input to compute the filter gradient d_x
  • tf.nn.conv2d_backprop_input can be implemented by tf.nn.conv2d_transpose
  • All 4 functions above can be implemented by tf.nn.conv2d
  • Actually, use TF‘s autodiff is the fastest way to compute gradients

Long Answer

Now, let‘s give an actual working code example of how to use the 4 functions above to compute d_x and d_w given d_out. This shows how conv2dconv2d_backprop_filterconv2d_backprop_input, and conv2d_transpose are related to each other. Please find the full scripts here.

Computing d_x in 4 different ways:

# Method 1: TF‘s autodiff
d_x = tf.gradients(f, x)[0]

# Method 2: manually using conv2d
d_x_manual = tf.nn.conv2d(input=tf_pad_to_full_conv2d(d_out, w_size),
                          filter=tf_rot180(w),
                          strides=strides,
                          padding=‘VALID‘)

# Method 3: conv2d_backprop_input
d_x_backprop_input = tf.nn.conv2d_backprop_input(input_sizes=x_shape,
                                                 filter=w,
                                                 out_backprop=d_out,
                                                 strides=strides,
                                                 padding=‘VALID‘)

# Method 4: conv2d_transpose
d_x_transpose = tf.nn.conv2d_transpose(value=d_out,
                                       filter=w,
                                       output_shape=x_shape,
                                       strides=strides,
                                       padding=‘VALID‘)

Computing d_w in 3 different ways:

# Method 1: TF‘s autodiff
d_w = tf.gradients(f, w)[0]

# Method 2: manually using conv2d
d_w_manual = tf_NHWC_to_HWIO(tf.nn.conv2d(input=x,
                                          filter=tf_NHWC_to_HWIO(d_out),
                                          strides=strides,
                                          padding=‘VALID‘))

# Method 3: conv2d_backprop_filter
d_w_backprop_filter = tf.nn.conv2d_backprop_filter(input=x,
                                                   filter_sizes=w_shape,
                                                   out_backprop=d_out,
                                                   strides=strides,
                                                   padding=‘VALID‘)

Please see the full scripts for the implementation of tf_rot180tf_pad_to_full_conv2dtf_NHWC_to_HWIO. In the scripts, we check that the final output values of different methods are the same; a numpy implementation is also available.

原文地址:https://www.cnblogs.com/ranjiewen/p/9368359.html

时间: 2024-07-30 05:39:02

tensorflow 卷积/反卷积-池化/反池化操作详解的相关文章

jdbc连接池中c3p0的配置文件的详解以及在在java中如何使用

<c3p0-config> <!-- 默认配置,如果没有指定则使用这个配置 --> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl"> <![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnic

PHP页面静态化3(PHP缓存机制详解)

我们可以使用PHP自带的缓存机制来完成页面静态化,但是仅靠PHP自身的缓存机制并不能完美的解决页面静态化,往往需要和其他静态化技术(通常是伪静态技术)结合使用. output buffer是php自带缓存,可以通过配置php.ini关闭,程序缓存是一直开启状态,没法关闭.程序缓存中内容没法修改,output buffer中内容可以修改,修改完成后全部发给程序缓存. 一个网页对应一个消息,消息包括消息头和消息体,每个消息必须有消息头,消息体可以为空,如果程序中没有定义消息头,使用默认的. 由图可知

TensorFlow gfile文件操作详解

转:https://blog.csdn.net/u014182497/article/details/80681331 一.gfile模块是什么 gfile模块定义在tensorflow/python/platform/gfile.py,但其源代码实现主要位于tensorflow/tensorflow/python/lib/io/file_io.py,那么gfile模块主要功能是什么呢? tf.gfile模块的主要角色是:1.提供一个接近Python文件对象的API,以及2.提供基于Tensor

CentOS Minimal版最小化安装后VMware联网详解

最近想搞个mailman邮件列表,又不想在我常用的CentOS 6.4上做实验,怕破坏了环境,于是就想装个试验机,又嫌它占空间太大,于是找了半天发现CentOS 6.0的minimal版本最适合了,装完后发现真的是很小,才600多M,不过因为太精简而导致连网络都没有.下面介绍怎么样配置网络,装完系统后紧接着就能按下面步骤来进行了.(注意这是VMware虚拟机,要是你用物理机的话直接用rp-pppoe拨号就行了,详情请见上一篇博文.) 首先打开虚拟机软件,virtual network edito

第十四节,TensorFlow中的反卷积,反池化操作以及gradients的使用

反卷积是指,通过测量输出和已知输入重构未知输入的过程.在神经网络中,反卷积过程并不具备学习的能力,仅仅是用于可视化一个已经训练好的卷积神经网络,没有学习训练的过程.反卷积有着许多特别的应用,一般可以用于信道均衡.图像恢复.语音识别.地震学.无损探伤等未知输入估计和过程辨识方面的问题. 在神经网络的研究中,反卷积更多的是充当可视化的作用,对于一个复杂的深度卷积网络,通过每层若干个卷积核的变换,我们无法知道每个卷积核关注的是什么,变换后的特征是什么样子.通过反卷积的还原,可以对这些问题有个清晰的可视

第十五节,利用反卷积技术复原卷积网络各层图像

在第十三节,我们已经介绍了使用带有全局平均池化层的CNN对CIFAR10数据集分类,在学习了反卷积神经网络之后我们把第十三节那个程序里的卷积层可视化出来. 一 替换掉tf.nn.max_pool()函数 这里不再使用自己定义的max_pool_2x2函数,改成新加入的带有mask返回值得max_pool_with_argmax()函数: #定义占位符 input_x = tf.placeholder(dtype=tf.float32,shape=[None,24,24,3]) #图像大小24x2

Convolution Network及其变种(反卷积、扩展卷积、因果卷积、图卷积)

今天,主要和大家分享一下最近研究的卷积网络和它的一些变种. 首先,介绍一下基础的卷积网络. 通过PPT上的这个经典的动态图片可以很好的理解卷积的过程.图中蓝色的大矩阵是我们的输入,黄色的小矩阵是卷积核(kernel,filter),旁边的小矩阵是卷积后的输入,通常称为feature map. 从动态图中,我们可以很明白的看出卷积实际上就是加权叠加. 同时,从这个动态图可以很明显的看出,输出的维度小于输入的维度.如果我们需要输出的维度和输入的维度相等,这就需要填充(padding). 现在我们来看

深度学习—反卷积的理解

1.Deconvolution大致可以分为以下几个方面: (1)非监督学习:unsupervised learning,其实就是covolutional sparse coding:这里的deconv只是观念上和传统的conv反向,传统的conv是从图片生成feature map,而deconv是用unsupervised的方法找到一组kernel和feature map,让它们重建图片. (2)CNN可视化:通过deconv将CNN中conv得到的feature map还原到像素空间,以观察特

Transposed Convolution 反卷积

Transposed convolutions也称作fractionally strided convolutions(本人比较喜欢这个称呼,比较直观),Upconvolution,deconvolutions i:表示一般卷积时候的输入图片的大小i*i               i':表示反卷积时候的输入图片的大小 k:表示一般卷积时候的kernel的大小i*i                 k'=k s:表示stride大小