用 TensorFlow 做卷积

让我们用所学知识在 TensorFlow 里构建真的 CNNs。在下面的练习中,你需要设定卷积核滤波器(filters)的维度,weight,bias。这在很大程度上来说是 TensorFlow CNNs 最难的部分。一旦你知道如何设置这些属性的大小,应用 CNNs 会很方便。

回顾

你应该看一下二维卷积的文档。文档大部分都很清楚,padding这一部分,可能会有点难以理解。padding 会根据你给出的 'VALID' 或者 'SAME' 参数,做相应改变。

这些也是需要你回顾的:

  1. TensorFlow 变量。
  2. Truncated 正态分布 - 在 TensorFlow 中你需要在一个正态分布的区间中初始化你的权值。
  3. 根据输入大小、滤波器大小,来决定输出维度(如下所示)。你用这个来决定滤波器应该是什么样:

     new_height = (input_height - filter_height + 2 * P)/S + 1
    new_width = (input_width - filter_width + 2 * P)/S + 1

说明

  1. conv2d函数中完成所有 TODO
  2. 设定 strides 、 padding 、 filter weight/bias (F_w and F_b) 输出是 (1, 2, 2, 3)。除了 strides 所有变量的类型都应该为 TensorFlow 变量。
"""
Setup the strides, padding and filter weight/bias such that
the output shape is (1, 2, 2, 3).
"""
import tensorflow as tf
import numpy as np # `tf.nn.conv2d` requires the input be 4D (batch_size, height, width, depth)
# (1, 4, 4, 1)
x = np.array([
[0, 1, 0.5, 10],
[2, 2.5, 1, -8],
[4, 0, 5, 6],
[15, 1, 2, 3]], dtype=np.float32).reshape((1, 4, 4, 1))
X = tf.constant(x) def conv2d(input):
# Filter (weights and bias)
# The shape of the filter weight is (height, width, input_depth, output_depth)
# The shape of the filter bias is (output_depth,)
# TODO: Define the filter weights `F_W` and filter bias `F_b`.
# NOTE: Remember to wrap them in `tf.Variable`, they are trainable parameters after all.
F_W = ?
F_b = ?
# TODO: Set the stride for each dimension (batch_size, height, width, depth)
strides = [?, ?, ?, ?]
# TODO: set the padding, either 'VALID' or 'SAME'.
padding = ?
# https://www.tensorflow.org/versions/r0.11/api_docs/python/nn.html#conv2d
# `tf.nn.conv2d` does not include the bias computation so we have to add it ourselves after.
return tf.nn.conv2d(input, F_W, strides, padding) + F_b out = conv2d(X)

方案

这是我的做法。注意:有不止一种方法得到正确的输出维度,你的答案可能会跟我的有所不同。

def conv2d(input):
# Filter (weights and bias)
F_W = tf.Variable(tf.truncated_normal((2, 2, 1, 3)))
F_b = tf.Variable(tf.zeros(3))
strides = [1, 2, 2, 1]
padding = 'VALID'
return tf.nn.conv2d(input, F_W, strides, padding) + F_b

我想要把输入的 (1, 4, 4, 1) 转变成 (1, 2, 2, 3)。padding 方法我选择 'VALID'。我觉得这更容易理解,也得到了我想要的结果。

out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))

把值带入:

out_height = ceil(float(4 - 2 + 1) / float(2)) = ceil(1.5) = 2
out_width = ceil(float(4 - 2 + 1) / float(2)) = ceil(1.5) = 2

要把深度从 1 变成 3。我要把我滤波器的输出做相应的设置:

F_W = tf.Variable(tf.truncated_normal((2, 2, 1, 3))) # (height, width, input_depth, output_depth)
F_b = tf.Variable(tf.zeros(3)) # (output_depth)

输入的深度是 1,所以我选择 1 作为滤波器的 input_depth

TensorFlow的 卷积层的更多相关文章

  1. 『TensorFlow』卷积层、池化层详解

    一.前向计算和反向传播数学过程讲解

  2. tensorflow实现卷积层的几种方式

    #coding:utf-8 #第一种实现 tf.nn import tensorflow as tf import tensorflow.contrib.slim as slim tf.reset_d ...

  3. CNN中卷积层的计算细节

    原文链接: https://zhuanlan.zhihu.com/p/29119239 卷积层尺寸的计算原理 输入矩阵格式:四个维度,依次为:样本数.图像高度.图像宽度.图像通道数 输出矩阵格式:与输 ...

  4. TensorFlow实现卷积神经网络

    1 卷积神经网络简介 在介绍卷积神经网络(CNN)之前,我们需要了解全连接神经网络与卷积神经网络的区别,下面先看一下两者的结构,如下所示: 图1 全连接神经网络与卷积神经网络结构 虽然上图中显示的全连 ...

  5. tensorflow 1.0 学习:卷积层

    在tf1.0中,对卷积层重新进行了封装,比原来版本的卷积层有了很大的简化. 一.旧版本(1.0以下)的卷积函数:tf.nn.conv2d conv2d( input, filter, strides, ...

  6. TensorFlow与caffe中卷积层feature map大小计算

    刚刚接触Tensorflow,由于是做图像处理,因此接触比较多的还是卷及神经网络,其中会涉及到在经过卷积层或者pooling层之后,图像Feature map的大小计算,之前一直以为是与caffe相同 ...

  7. 82、TensorFlow教你如何构造卷积层

    ''' Created on 2017年4月22日 @author: weizhen ''' import tensorflow as tf #通过tf.get_variable的方式创建过滤器的权重 ...

  8. 81、Tensorflow实现LeNet-5模型,多层卷积层,识别mnist数据集

    ''' Created on 2017年4月22日 @author: weizhen ''' import os import tensorflow as tf import numpy as np ...

  9. tensorflow 卷积层

    TensorFlow 卷积层   让我们看下如何在 TensorFlow 里面实现 CNN. TensorFlow 提供了 tf.nn.conv2d() 和 tf.nn.bias_add() 函数来创 ...

随机推荐

  1. python邮件发送:普通文本、html、添加附件

    # -*- coding: utf-8 -*- # @Time : 2019/9/19 13:46 # @Author : HuangWenjun # @Email : 350920551@qq.co ...

  2. html转换成pdf

    指定html转换成pdf 安装插件: npm install --save html2canvas npm install jspdf --save 引入 plugins/ htmlToPdf.js ...

  3. Leetcode73. Set Matrix Zeroes矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ] 输 ...

  4. html 遮罩层以及弹出框的制作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Scrollerview与listview或者gridview发生冲突

    滑动冲突说实在的就是子view的滑动事件与父view的滑动事件的监听都在同时触发,而导致的activity的点击事件或者布局出问题 常见的就有Scrollerview与Scrollerview与lis ...

  6. Loadrunner常用分析点

    Loadrunner常用的分析点 一.在Vuser(虚拟用户状态)中 1.Running Vusers:提供了生产负载的虚拟用户运行状态的相关信息,可以帮助我们了解负载生成的结果.(即用户在几分钟左右 ...

  7. vue-waterfall2 基于Vue.js 瀑布流组件

    vue-waterfall2 1.宽度自适应,数据绑定特效(适用于上拉加载更多) 2.自定义程度高 3.使用极为简便,适用于PC/移动端 4.提供resize(强制刷新布局-适用于下拉刷新)/mix( ...

  8. 来实现一个缩水版Vuex

    对 Vuex 源码进行浓缩,DIY 一个小型 Vuex 功能如下 通过 $store.commit 改变 $store.state 实现 strict model 源码约70行左右比较好理解,下面讲解 ...

  9. tomcat 端口占用问题解决

    在dos下,输入  netstat   -ano|findstr  8080 //说明:查看占用8080端口的进程 显示占用端口的进程 taskkill  /pid  6856  /f //说明,运行 ...

  10. 【机器学习PAI实战】—— 玩转人工智能之综述

    摘要: 基于人工智能火热的大背景下,通过阿里云的机器学习平台PAI在真实场景中的应用,详细阐述相关算法及使用方法,力求能够让读者读后能够马上动手利用PAI搭建属于自己的机器学习实用方案,真正利用PAI ...