飞蚊症是什么症状| 痰是绿色的是什么原因| 裙带菜不能和什么一起吃| 什么的嘴| 马克笔是什么笔| 血钾查什么项目| 异类是什么意思| 外痔疮是什么样子图片| 6月12日是什么节日| 派特ct主要检查什么| 血液净化是什么意思| 尿酸高是什么问题| 心跳快是什么原因| 中学校长什么级别| 痤疮是什么意思| 什么人没有国籍| 拉肚子是什么原因引起的怎么办| 液蜡是什么| 尿蛋白三个加号吃什么药| 云南有什么| 迎风流泪用什么眼药水| jeans什么意思| 偏头疼吃什么药| 乳腺靶向检查是什么| 水果之王是什么| 90年属什么| 抽动症是什么原因造成的| 中秋节适合吃什么菜| aq是什么标准| 夏天怕热冬天怕冷是什么体质| 08年属什么| 金字旁的字与什么有关| 咖啡加什么最好喝| 昙花什么时候开花| 转氨酶是什么| 泰格豪雅属于什么档次| 慢性鼻窦炎吃什么药| 过敏性结膜炎用什么药| 甲亢多吃什么食物比较好| 神经性皮炎用什么药膏效果最好| 2型糖尿病吃什么药降糖效果好| 贫血严重会得什么病| 吃什么东西对眼睛好| 急性肠胃炎应该吃什么药| 历年是什么意思| 低压太低是什么原因| 契丹族现在是什么族| 办香港通行证要准备什么材料| sanag是什么牌子| 弦脉是什么意思| 婚姻宫是什么意思| 什么是性骚扰| 爽约是什么意思| 男人鼻子大代表什么| 乌龟肺炎用什么药| 为什么今年有两个六月| 客之痣是什么意思| 世界上什么最大| 什么血型不招蚊子| 男孩小名叫什么好听| 体温偏高的人说明什么| 带状疱疹什么样子| 什么生金| 8月28号是什么星座| 蛋白电泳是查什么的| 桃子有什么功效| 小路像什么| 烟火气是什么意思| 慢性咽炎挂什么科| 脑梗适合吃什么食物| 3月21是什么星座| 安全期是什么时候| 孩子咳嗽有痰吃什么药| 做阴超有黄体说明什么| 猪脚焖什么好吃| 丰的部首是什么偏旁| 尿频尿急吃什么药最好| 188是什么意思| 手指关节发黑是什么原因| 犀利的眼神是什么意思| 喝什么牌子的水最健康| 2019是什么生肖| 痛经是什么原因引起的| 铁锈红配什么颜色好看| 陈醋与香醋有什么区别| 春的五行属性是什么| 10月2号是什么星座| 什么的镜子| 最贵的榴莲是什么品种| 塌陷是什么意思| 什么的动作| 出轨是什么意思| 多发结节是什么意思| 面碱是什么| 官印是什么意思| 吃什么容易瘦| 水瓶座是什么象| 为什么会湿气重| 为什么早上起来眼睛肿| 换药挂什么科| 妖是什么意思| 请示是什么意思| 相亲为什么不能拖太久| 右腿麻木是什么征兆| 流鼻血是什么病| 天气一热身上就痒是什么原因| 完犊子是什么意思| 灌肠是什么意思| 吃完芒果后不能吃什么食物| 肚子胀痛吃什么药| 囊肿什么意思| 旌旗是什么意思| 霏字五行属什么| 白羊座前面是什么星座| 在什么的前面用英语怎么说| 淋巴结肿吃什么消炎药| 嘴里发酸是什么原因| 吃谷维素有什么副作用| 舒畅的舅舅是做什么的| 哥哥的哥哥叫什么| 天冬与麦冬有什么区别| 核医学科主要治什么病| 居士什么意思| 战狼三什么时候上映| 阴茎越来越小是什么原因| 什么可以治早泄| 司空见惯的惯是什么意思| 嗓子痒吃什么药| 甲亢有什么反应| 璨字五行属什么| 农历八月初五是什么星座| 焦虑是什么意思| 压寨夫人是什么意思| 欧诗漫适合什么年龄| theme什么意思| 发来贺电是什么意思| 对牛弹琴是什么意思| 理数是什么意思| 腰脱什么症状| affairs是什么意思| 碧血是什么意思| 盗汗什么意思| 阴道长什么样| 和什么细什么的成语| 蜂王浆是什么| 夏天吃什么食物| 梦见前婆婆是什么意思| 尿蛋白阳性什么意思| 积福是什么意思| 梨涡是什么意思| 眼睛视力模糊是什么原因| 蔓越莓有什么功效和作用| 舌头有裂纹是什么原因| 岁寒三友是什么意思| 胆囊炎吃什么消炎药| 男生生日礼物送什么| 梦见买床是什么意思| 耳鸣吃什么药| 为什么会胃胀气| 皮脂膜是什么| 疲惫是什么意思| 紫色搭配什么颜色| 黑豆加红枣有什么功效| 什么东西解酒最快| 榴莲坏了是什么味道| 欧是什么意思| 放生鱼有什么好处| 胃气胃胀吃什么药最好| 碧玺是什么| 血色素是什么意思| 胃气不足吃什么中成药| 痤疮是什么东西| 为什么一直打哈欠| 血压高压高低压正常是什么原因| 鸡炖什么好吃又有营养| 胎脂是什么原因造成的| 大象是什么颜色| 盥洗室什么意思| 麦冬的功效与作用是什么| 安全感是什么意思| 白蛋白低是什么原因| 垂死病中惊坐起什么意思| 喝绿豆汤有什么好处| wifi用户名是什么| 做梦捡到钱了什么预兆| 11月25日什么星座| 全身发黄是什么原因| 增生期子宫内膜是什么意思| 什么是再生纤维| 做护士需要什么条件| 妊娠期是什么意思| 女人脾胃虚弱吃什么好| 长骨刺是什么原因导致的| 胶水用什么能洗掉| 什么情况会导致月经推迟不来| 常喝黑苦荞茶有什么好处| 怀孕初期吃什么食物好| 肠道功能紊乱吃什么药| 9.30号是什么星座| 阳强易举是什么意思| td代表什么意思| 酒糟是什么| 日匀念什么| 水清则无鱼什么意思| 骨折恢复期吃什么好| 焦油是什么| 什么能代替润滑油| 祸水什么意思| 汾酒是什么香型| 做肠镜要做什么准备| act是什么| 手会抖是什么原因| 男生被口什么感觉| 黄喉是什么部位| 彘是什么意思| 总恶心是什么病的前兆| 医保什么时候到账| 什么时候量血压最准| 甲鱼和什么不能一起吃| 中将是什么级别的干部| 藏干是什么意思| 这是什么车| 血小板低有什么危害| 六月一日是什么星座| 林彪为什么出逃| 蚂蚱吃什么| 输卵管堵塞吃什么药能打通| 皮肤暗黄是什么原因造成的| 亚洲没有什么气候| 两融余额是什么意思| 7月去英国穿什么| 腾云驾雾是什么生肖| 松鼠喜欢吃什么食物| 咳嗽有痰是什么原因| 北京的简称是什么| 寻麻疹是什么症状| 县级市市长什么级别| 4月14日是什么星座| 樵夫是什么意思| 有趣的灵魂是什么意思| 哺乳期是什么意思| 坐月子可以吃什么菜| fsw是什么意思| 睡觉流口水是什么原因引起的| nt是什么| 必修课是什么意思| 猪头猪脑是什么生肖| 小孩子睡觉流口水是什么原因| 脚没力气是什么原因| 貘是什么| 宫颈糜烂用什么药好得快| 精力是什么意思| 积液是什么原因造成的怎么治疗| 腋窝爱出汗是什么原因| 饭后胃胀吃什么药| 被蝎子蛰了用什么药| 胃病吃什么食物养胃| 水泡长什么样子图片| 女命正印代表什么| 接吻要注意什么| 腮腺炎是什么| 黄芪和什么搭配不上火| 茉莉花茶有什么功效| 木须是什么| 百度
Skip to content

simon-larsson/keras-swa

Repository files navigation

Keras SWA - Stochastic Weight Averaging

PyPI version License

This is an implemention of SWA for Keras and TF-Keras.

Introduction

Stochastic weight averaging (SWA) is build upon the same principle as snapshot ensembling and fast geometric ensembling. The idea is that averaging select stages of training can lead to better models. Where as the two former methods average by sampling and ensembling models, SWA instead average weights. This has been shown to give comparable improvements confined into a single model.

Illustration

Paper

Installation

pip install keras-swa

SWA API

Keras callback object for SWA.

Arguments

start_epoch - Starting epoch for SWA.

lr_schedule - Learning rate schedule. 'manual' , 'constant' or 'cyclic'.

swa_lr - Learning rate used when averaging weights.

swa_lr2 - Upper bound of learning rate for the cyclic schedule.

swa_freq - Frequency of weight averagining. Used with cyclic schedules.

batch_size - Batch size model is being trained with (only when using batch normalization).

verbose - Verbosity mode, 0 or 1.

Batch Normalization

Last epoch will be a forward pass, i.e. have learning rate set to zero, for models with batch normalization. This is due to the fact that batch normalization uses the running mean and variance of it's preceding layer to make a normalization. SWA will offset this normalization by suddenly changing the weights in the end of training. Therefore, it is necessary for the last epoch to be used to reset and recalculate batch normalization running mean and variance for the updated weights. Batch normalization gamma and beta values are preserved.

When using manual schedule: The SWA callback will set learning rate to zero in the last epoch if batch normalization is used. This must not be undone by any external learning rate schedulers for SWA to work properly.

Learning Rate Schedules

The default schedule is 'manual', allowing the learning rate to be controlled by an external learning rate scheduler or the optimizer. Then SWA will only affect the final weights and the learning rate of the last epoch if batch normalization is used. The schedules for the two predefined, 'constant' or 'cyclic' can be observed below.

lr_schedules

Example

For Tensorflow Keras (with constant LR)

from sklearn.datasets import make_blobs
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD

from swa.tfkeras import SWA
 
# make dataset
X, y = make_blobs(n_samples=1000, 
                  centers=3, 
                  n_features=2, 
                  cluster_std=2, 
                  random_state=2)

y = to_categorical(y)

# build model
model = Sequential()
model.add(Dense(50, input_dim=2, activation='relu'))
model.add(Dense(3, activation='softmax'))

model.compile(loss='categorical_crossentropy', 
              optimizer=SGD(lr=0.1))

epochs = 100
start_epoch = 75

# define swa callback
swa = SWA(start_epoch=start_epoch, 
          lr_schedule='constant', 
          swa_lr=0.01, 
          verbose=1)

# train
model.fit(X, y, epochs=epochs, verbose=1, callbacks=[swa])

Or for Keras (with Cyclic LR)

from sklearn.datasets import make_blobs
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, BatchNormalization
from keras.optimizers import SGD

from swa.keras import SWA

# make dataset
X, y = make_blobs(n_samples=1000, 
                  centers=3, 
                  n_features=2, 
                  cluster_std=2, 
                  random_state=2)

y = to_categorical(y)

# build model
model = Sequential()
model.add(Dense(50, input_dim=2, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(3, activation='softmax'))

model.compile(loss='categorical_crossentropy', 
              optimizer=SGD(learning_rate=0.1))

epochs = 100
start_epoch = 75

# define swa callback
swa = SWA(start_epoch=start_epoch, 
          lr_schedule='cyclic', 
          swa_lr=0.001,
          swa_lr2=0.003,
          swa_freq=3,
          batch_size=32, # needed when using batch norm
          verbose=1)

# train
model.fit(X, y, batch_size=32, epochs=epochs, verbose=1, callbacks=[swa])

Output

Model uses batch normalization. SWA will require last epoch to be a forward pass and will run with no learning rate
Epoch 1/100
1000/1000 [==============================] - 1s 547us/sample - loss: 0.5529
Epoch 2/100
1000/1000 [==============================] - 0s 160us/sample - loss: 0.4720
...
Epoch 74/100
1000/1000 [==============================] - 0s 160us/sample - loss: 0.4249

Epoch 00075: starting stochastic weight averaging
Epoch 75/100
1000/1000 [==============================] - 0s 164us/sample - loss: 0.4357
Epoch 76/100
1000/1000 [==============================] - 0s 165us/sample - loss: 0.4209
...
Epoch 99/100
1000/1000 [==============================] - 0s 167us/sample - loss: 0.4263

Epoch 00100: final model weights set to stochastic weight average

Epoch 00100: reinitializing batch normalization layers

Epoch 00100: running forward pass to adjust batch normalization
Epoch 100/100
1000/1000 [==============================] - 0s 166us/sample - loss: 0.4408

Collaborators

About

Simple stochastic weight averaging callback for Keras

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages

乙肝第二针最晚什么时候打 失眠可以吃什么药 2021属什么生肖 为什么会脑梗 仿制药是什么意思
感冒输液用什么药 检查尿液能查出什么病 苏州有什么好玩的地方 什么样的人爱长结节 乐松是什么药
昭和是什么意思 曹操原名叫什么 为什么医生很少开阿斯美 能量是什么 激素六项挂什么科
河东狮吼什么意思 微恶风寒是什么意思 血氧饱和度是什么意思 打三个喷嚏代表什么 外阴苔癣是一种什么病
舌头边缘有齿痕是什么原因hcv9jop3ns3r.cn 乌豆是什么hcv7jop9ns4r.cn 月经为什么推迟不来qingzhougame.com 怀孕的脉搏和正常脉搏有什么区别hcv8jop9ns8r.cn 攥是什么意思xscnpatent.com
一什么烟hcv8jop8ns5r.cn 婴儿什么时候开始说话hcv7jop9ns5r.cn 胸膜炎是什么病hcv7jop7ns3r.cn 什么望外hcv9jop1ns7r.cn 筝是什么意思hcv9jop4ns0r.cn
地瓜什么时候成熟hcv8jop4ns5r.cn 开车是什么意思hcv9jop6ns7r.cn 药店为什么不让卖高锰酸钾hcv9jop3ns2r.cn 包皮溃烂用什么药hcv9jop1ns6r.cn 瘟神是什么意思clwhiglsz.com
讳莫如深是什么意思hcv9jop0ns5r.cn 阑尾炎看什么科室creativexi.com 深入交流是什么意思hcv9jop2ns1r.cn 头发为什么长不长hcv8jop0ns1r.cn 舌头麻是什么病的前兆adwl56.com
百度