CS 20SI|Lecture 7 Playing with convolutions in TensorFlow

Agenda

  • Convolution support in TF
  • More MNIST!!!
  • Variable scope
  • Autoencoder

Convolution in TF

tf​.​nn​.​conv2d​(​input​,​ filter​,​ strides​,​ padding​,​ use_cudnn_on_gpu​=​None​,​ data_format​=​None​, name​=​None)

Input​:​ ​Batch​ size x ​Height​ x ​Width​ x ​Channels 
Filter​:​ ​Height​ x ​Width​ x ​Input​ ​Channels​ x ​Output​ ​Channels (​e​.​g​.​ ​[​5​,​ ​5​,​ ​3​,​ ​64​]) 
Strides​:​ ​4​ element ​1​-​D tensor​,​ strides ​in​ each direction (​often ​[​1​,​ ​1​,​ ​1​,​ ​1​]​ ​or​ ​[​1​,​ ​2​,​ ​2​,​ ​1​]) 
Padding​:​ ​'​SAME​'​ ​or​ ​'​VALID' Data_format​:​ ​default​ to NHWC 

Filter就是权重weight,shape=HWIO
biasshape=O
对于strides 参数来说,第一个和第四个一般都是1。
VALID Padding计算:

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

SAME Padding计算:

out_height = ceil(float(in_height)/float(strides[1]))
out_width = ceil(float(in_width)/float(strides[2]))

Conv on MNIST

这里写图片描述

Variable scope 变量域

因为我们要处理多网络层,下面介绍变量域,类似于命名空间。比如,一个定义在作用域conv1中的变量weights将变为conv1-weights。通常的做法是为每一层创建一个变量域,这样不同层相同名称的变量不会产生命名冲突。
在变量域中,我们不使用tf.Variable创建变量,而用tf.get_variable
tf​.​get_variable​(<​name​>,​ ​<shape>​,​ ​<initializer>)
如果一个变量在该作用域中已经存在,那么直接使用。否则,创建变量。
这种机制使得在不用网络架构中共享变量变得容易,可以在一个地方初始化所有的变量。
在同一个变量域中的结点会组织在一起,因此不再需要使用name scope。声明一个变量域,with tf.variable_scope('conv1') as scope:
例如:

with​ tf​.​variable_scope​(​'conv1'​)​ ​as​ scope:       
    w ​=​ tf​.​get_variable​(​'weights'​,​ ​[​5​,​ ​5​,​ ​1​,​ ​32​])     
    conv ​=​ tf​.​nn​.​conv2d​(​images​,​ w​,​ strides​=[​1​,​ ​1​,​ ​1​,​ ​1​],​ padding​=​'SAME')     
    conv1 ​=​ tf​.​nn​.​relu​(​conv ​+​ b​,​ name​=​scope​.​name) 
with​ tf​.​variable_scope​(​'conv2'​)​ ​as​ scope:       
    w ​=​ tf​.​get_variable​(​'weights'​,​ ​[​5​,​ ​5​,​ ​32​,​ ​64​])     
    b ​=​ tf​.​get_variable​(​'biases'​,​ ​[​64​],​ initializer​=​tf​.​random_normal_initializer​())     
    conv ​=​ tf​.​nn​.​conv2d​(​conv1​,​ w​,​ strides​=[​1​,​ ​1​,​ ​1​,​ ​1​],​ padding​=​'SAME')     
    conv2 ​=​ tf​.​nn​.​relu​(​conv ​+​ b​,​ name​=​scope​.​name) 

Autoencoder

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值