tensorflow实现batch normalization

撰写时间:2017.08.31
撰写内容:使用tensorflow实现batch normalization



写在开头:batch normalization不是很难的内容,但是如果不自己通过一些低级的API实现总会觉得有些细节很难触碰到,所以下面的内容就是如何使用tensorflow实现batch normalization。

文章分为两部分:
1.实现过程中使用的API,务必在实现之前了解这些api的使用
2.实现batch normalization,代码来自于stackoverflow上面的一个回答。

代码是否可用,并没有测试。我也是今天早上做项目的时候需要使用bn,所以花了一天时间,边学习,边记录才有了这篇博文。对于文中API解释不清楚的,可以搜一搜tensorflow的官方问答,或者其他博文。最后所有的测试代码,可以从我的github地址上下载(还没上传。。)


2017.9.4更新:

代码可以使用,而且效果很好,训练速度有明显的提成,我的项目的代码地址


tensorflow API

assign

返回值是op,而不是tensor
1.tf.assign()
2.tf.assign_sub()
3.tf.assign_add()

dependency

1.tf.control_dependency()

人为的在两个操作之间添加关联关系

with tf.control_dependency([op1,...])
    op2

即op2是依赖于op1的,在运行op2时需要调用op1

2.tf.identity()

Return a new tensor with the same shape and contents as the input tensor or value.

import tensorflow as tf
x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)

with tf.control_dependencies([x_plus_1]):
    y = tf.identity(x)
    #y = x


init = tf.global_variables_initializer()

with tf.Session() as session:
    init.run()
    for i in xrange(5):
        print(y.eval())

with “y=x”,并不会创建变量,而且不是op,所以tf.control_dependencies并不会生效。
这里写图片描述
with “y=tf.identity(x)”,每次取y的值,x都会自动加1
这里写图片描述

ExponentialMovingAverage

1.apply()

1.adds shadow copies of trained variables
2.add ops that maintain a moving average of the trained variables

调用形式

with tf.control_dependencies([op1]):
    ema_op = ema.apply([var1])

注:op1是更新变量var1的,然后先储存var1修改前的变量值,shadow variables。然后执行op1,取出修改后变量为variable。最后创建一个更新shadow variables的op,

shadow_variable = decay * shadow_variable + (1 - decay) * variable

2.average()

Returns the Variable holding the average of var.
取出apply中计算的值

import tensorflow as tf
w = tf.Variable(1.0)
b = tf.Variable(1.0)
ema = tf.train.ExponentialMovingAverage(0.9)
#使add操作的返回值为op,而不是数值
#如果是数值,那么control_dependency就不能依赖了
update_w = tf.assign_add(w, 1.0)
update_b = tf.assign_sub(b, 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值