文章前面提到的BatchNormalization的核心思想数据生成器数据部分,展示了带BN逻辑回归带BN的宽模型带BN的深度模型
前文一、Windows系统下Tensorflow2.x(2.6 )二、深度学习-读取数据三、Tensorflow图像处理预算四、线性回归模型的Tensorflow实现五、深度学习-读取楷书VGG16bn(BatchNormalization )实现鸟类数据库分类Batchnormalization的核心思想
BN的基本想法其实相当直观。 深层神经网络对于进行非线性变换前的激活输入值(其x=WU B,u为输入) 3358www.Sina.com/) Sigmoid函数来说,激活输入值WU B是较大的负值因此,这个3358www.Sina.com/是训练为深层神经网络收敛越来越慢的随着网络深度加深或者在训练过程中,其分布逐渐发生偏移或者变动,之所以训练收敛慢,一般是整体分布逐渐往非线性函数的取值区间的上下限两端靠近,导致反向传播时低层神经网络的梯度消失,实际上
数据生成器数据部分是#数据生成训练集和测试集# 猫数据from keras.preprocessing.imageimportimagedatageneratorimsize=224 train _ generator=image数据生成器255 ).florata dogs-vs-cats/smalldata/train ),target_size=) IMSIZE, IMSIZE class_mode='categorical ' ) validation _ generator=image数据生成器(rescale=1./255 ).flow _ from _
数据源kaggle中的猫狗数据
#x (图像)和y (原因变量) import numpy as npX,y=next(validation_generator ) print ) x.shape (打印) y.shape ) y [ :
#展示图片frommatplotlibimportpyplotasplt.figure (fig,ax=PLT.subplots ) 2,5 ) fig.set_figheight(6)6) fig.set y
带BN的逻辑回归#带BN的逻辑回归模型from keras.layers import Flatten,Input,BatchNormalization, densefromkerasimportmodelinput _ layer=input3) x=input _ layerx=batch normalization (x ) x=Flatten ) ) x=x=densut
带#bn的逻辑回归模型和拟合from keras.optimizersimportadammodel1.com pile (loss=' categorical _ cross entropy ', optimizer=Adam (lr=metrics=[ ' accuracy ' ] )模型1.fit _ generator (train _ generator,epochs=200,validation )
Batch Normalization对于特定模型、特定数据集非常有用
BN宽模型#扩展,BN宽模型from keras.layers import Conv2D,max pooling 2d n _ channel=100 input _ layer=input ([ im size,大小,共享) 3 ) x=input _ layerx=batch normalization () x=conv2d(n_channel,[ 2,2 ],activation=' relu ' (x ) ) )
带#bn的宽模型编译和拟合模型2.com pile (loss=' categorical _ cross entropy ',optimizer=Adam(lr=0.001 ),metrical
比逻辑回归带BN的结果好得多
带BN的深度模型#带BN的深度模型n _ channel=20 input _ layer=input ([ imsize,im size,3 ) x=input _ layerx=batch normalization 2],paddanel activation='relu ' ) (x ) x=max pooling 2d ([ 2,2 ] ) x=Flatten () x=Dense(2) 2,activivive
#bn深度模型的编译和拟合from keras.optimizersimportadammodel3.com pile (loss=' categorical _ cross entropy ', optimizer=Adam (lr=metrics=[ ' accuracy ' ] )模型3.fit _ generator (train _ generator,epochs=200,validation )
深度模型会更好
BatchNormalization在很多情况下确实有很大的帮助,但并不是所有情况都有用。
##GitHub下载位置:
Tensorflow1.15深度学习