Tensorflow入门——处理overfitting的问题
cn-stem·@hongtao·
0.000 HBDTensorflow入门——处理overfitting的问题
在[之前的文章](https://steemit.com/cn-stem/@hongtao/tensorflow-keras-classification-with-keras)中,我们发现训练组(篮)和验证组(红)的损失函数在20个Epoch之后,向着相反方向变化。训练组损失函数继续下降,验证组损失函数反而在上升,这就是典型的Overfitting(过拟合)现象。  过拟合就是模型过度地学习了训练集的特征,反而没法处理测试集中更一般化的问题。处理过拟合最根本的解决方法当然是获得更多的训练样本。 但是在无法获得更多的训练样本的时候,也有两个最简单的方法,一是对权重进行正则化处理,二就对神经元随机dropout. 关于更多Keras的入门介绍,感兴趣的朋友可以参考[Google的官方教程](https://www.tensorflow.org/tutorials/),更多关于过拟合和欠拟合的相关资料请参考[这里](https://en.wikipedia.org/wiki/Overfitting) 在Keras中我们只需要对模型进行简单改造就能实现正则化和dropout,同样的,为了方便与读者交流,所有的代码都放在了这里: https://github.com/zht007/tensorflow-practice ## L1,L2正则化 模型Overfiting其中一个原因就是某些权重在训练的过程中会被放大,L1正则化相当于给权重加了惩罚因子,从而限制了某些权重过度膨胀。L2相当于对L1惩罚因子乘了个平方,对权重的膨胀加大了惩罚力度。 在Keras的模型中引入L1,或者L2也非常简单,只需要在建立模型的时候加入: ```python kernel_regularizer = keras.regularizers.l1 或 kernel_regularizer = keras.regularizers.l2 ``` 模型如下所示 ```python model = Sequential() model.add(Dense(20,input_shape = (X_train.shape[1],), activation = 'relu', kernel_regularizer = keras.regularizers.l2(0.001))) model.add(Dense(20,input_shape = (X_train.shape[1],), activation = 'relu', kernel_regularizer = keras.regularizers.l2(0.001))) model.add(Dense(10,activation = 'relu', kernel_regularizer = keras.regularizers.l2(0.001))) model.add(Dense(2, activation = 'softmax')) ``` ## Dropout 在需要Dropout的Dense层之后加上: ```python model.add(Dense(2, activation = 'softmax')) ``` 最后我们看看加上L2正则化和Dropout之后的模型是怎么样的。 ```python model = Sequential() model.add(Dense(20,input_shape = (X_train.shape[1],), activation = 'relu', kernel_regularizer = keras.regularizers.l2(0.001))) model.add(keras.layers.Dropout(0.5)) model.add(Dense(20,input_shape = (X_train.shape[1],), activation = 'relu', kernel_regularizer = keras.regularizers.l2(0.001))) model.add(keras.layers.Dropout(0.5)) model.add(Dense(10,activation = 'relu', kernel_regularizer = keras.regularizers.l2(0.001))) model.add(keras.layers.Dropout(0.5)) model.add(Dense(2, activation = 'softmax')) model.summary() ``` Model.summary可以查看整个模型的架构和参数的个数 ``` _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_19 (Dense) (None, 20) 180 _________________________________________________________________ dropout_7 (Dropout) (None, 20) 0 _________________________________________________________________ dense_20 (Dense) (None, 20) 420 _________________________________________________________________ dropout_8 (Dropout) (None, 20) 0 _________________________________________________________________ dense_21 (Dense) (None, 10) 210 _________________________________________________________________ dropout_9 (Dropout) (None, 10) 0 _________________________________________________________________ dense_22 (Dense) (None, 2) 22 ================================================================= Total params: 832 Trainable params: 832 Non-trainable params: 0 _________________________________________________________________ ``` ## 训练结果 最后我们看看正则化和Dropout后端的训练结果吧,是不是比之前漂亮多了。  --- 参考资料和数据来源 https://www.kaggle.com/uciml/pima-indians-diabetes-database https://www.tensorflow.org/tutorials/ https://en.wikipedia.org/wiki/Overfitting --- 同步到我的简书 https://www.jianshu.com/u/bd506afc6fc1
👍 ushkurwike, sweatsavory, americamode, searchmalt, bohrbowling, fimschell, laithut, oryow, dooworisa, duromyed, fararlue, doorpedal, kosheliuk, snoozesalsa, borndead05, justyy, dailychina, superbing, dailystats, turtlegraphics, witnesstools, ilovecoding, steemfuckeos, jianan, woolfe19861008, dongfengman, sweet-jenny8, anxin, lilypang22, busy.org, ethanlee, laiyuehta, cnbuddy, cryptocurrencyhk, shentrading, hongtao, robertyan, steemstem, kevinwong, lemouth, enzor, robotics101, effofex, dna-replication, alexander.alexis, alexzicky, tanyaschutte, felixrodriguez, mayowadavid, tristan-muller, fejiro, sco, rharphelle, kingabesh, de-stem, temitayo-pelumi, arconite, helo, howo, tsoldovieri, esteemguy, mr-aaron, terrylovejoy, olajidekehinde, ibk-gabriel, javier.dejuan, ascorphat, kryzsec, abigail-dantes, gra, stemng, flugschwein, lesmouths-travel, derbesserwisser, michaelwrites, purelyscience, jrevilla, cameravisual, jent, tombstone, curie, bloom, samminator, mountain.phil28, gentleshaid, monie, shoganaii, real2josh, geopolis, alexdory, francostem, croctopus, doctor-cog-diss, steemzeiger, moniroy, stem.witness, kingnosa, cerd26, cryptokrieg, corsica, zonguin, kingswisdom, agbona, kenadis, darkiche, mathowl, dexterdev, deholt, biomimi, emperorhassy, sciencetech, liberosist, aboutyourbiz, howtostartablog, skycae, sissyjill, morbyjohn, ninjace, locikll, makrotheblack, muliadi, vadimlasca, markmorbidity, vact, nitego, zipporah, schroders, rwilday, suesa, clweeks, erikkun28, emdesan, operahoser, hiddenblade, niouton, anwenbaumeister, hendrikdegrote, thatsweeneyguy, szokerobert, kjaeger, g0nr0gue, jasonbu, coolbuddy, tuoficinavirtual, joendegz, aalok, mattiarinaldoni, traviseric, intellihandling, emmanuel293, cryptofuwealth, scoora82, alexworld, casiloko, hansmast, praditya, cutnadhifa, cutnur, n4dh1f4, pamahdoo, reedhhw, amin-ove, huilco, hanyseek, donasys, mtfmohammad, pflanzenlilly, faberleggenda, mohaaking, imaloser, moksamol, getrichordie, stahlberg, bavi, neumannsalva, creatrixity, didic, misia1979, avizor, jesse5th, whoib, poodai, drmake, clement.poiret, gangstayid, vilda, juliocaraballo, diebaasman, hotsteam, reaverza, jesusj1, gabrielatravels, reavercois, tajstar, wstanley226, suasteguimichel, rhethypo, predict-crypto, chickenmeat, thewhalehunter, chrisluke, lola-carola, gmedley, mininthecity, cjunros, semtroneum, slickhustler007, pinksteam, zlatkamrs, samlee2018, annaabi, jlsplatts, attoan.cmt, pechichemena, hkmoon, gordon92, reizak, payger, trixie, trang, eu-id, sarhugo, andiblok, double-negative, herculean, dokter-purnama, florian-glechner, teukurival, jcalero, elsll, blewitt, bflanagin, lekang, allcapsonezero, eurodale, ambitiouslife, jingis07, gabyoraa, edanya, kakakk, jubei333, shayekh2, ykdesign, delegate.lafona, wisewoof, yaelg, serylt, perpetuum-lynx, fanta-steem, zest, cryptowrld, xuhi, stem-espanol, carloserp-2000, miguelangel2801, emiliomoron, ulisesfl17, tomastonyperez, elvigia, josedelacruz, joseangelvs, viannis, majapesi, erickyoussif, ubaldonet, reinaseq, lupafilotaxia, fran.frey, lorenzor, iamphysical, azulear, amestyj, ivymalifred, vjap55, eliaschess333, ydavgonzalez, yrmaleza, arac, luiscd8a, elpdl, andrick, yusvelasquez, joelsegovia, mirzantorres, jesusfl17, mary11, amart29, alfonzoasdrubal, crassipes, giulyfarci52, wilmer14molina, joannar, bearded-benjamin, andypalacios, alaiza, lapp, steemtpistia, agrovision, smalltall, anthive, psicoluigi, flores39, peaceandwar, thescubageek, veteranforcrypto, cleiver, angelica7, mahdiyari, lamouthe, vodonik, eniolw, yestermorrow, wackou, uceph, drifter1, mammasitta, stevenwood, gpcx86, celine-robichaud, frost1903, longer, jiujitsu, vegan.niinja, gracelbm, combatsports, danaedwards, shinedojo, drsensor, rival, massivevibration, lacher-prise, gribouille, benleemusic, eric-boucher, robertbira, nicole-st, geadriana, acont, hhtb, yomismosoy, alex-hm, lekosvapenglass, goodway, kafupraise, langford, steepup, wargof, guga34, xanderslee, cordeta, yashshah991, oghie, ivan-g, faithfullwills, abraham10, carlos84, sandracarrascal, douglimarbalzan, ennyta, gaming.yer, steem-familia, evangelista.yova, jenniferjulieth, ajfernandez, endopediatria, ingmarvin, alix96, elimao, anaestrada12, yorgermadison, alexjunior, antunez25, haf67, chavas, eglinson, uzcateguiazambra, asmeira, garrillo, pfernandezpetit, mgarrillogonzale, rubenp, jeferc, hirally, emynb, eugenialobo, ballesteroj, jcmontilva, rodriguezr, marbely20, moyam, emilycg, darys, sibaja, balcej, lmanjarres, anaka, benhurg, judisa, juddarivv, mariamo, kimmorales, loraine25, skorup87, arcange, raphaelle,