create account

Tensorflow入门——处理overfitting的问题 by hongtao

View this thread on: hive.blogpeakd.comecency.com
· @hongtao · (edited)
$2.61
Tensorflow入门——处理overfitting的问题
在[之前的文章](https://steemit.com/cn-stem/@hongtao/tensorflow-keras-classification-with-keras)中,我们发现训练组(篮)和验证组(红)的损失函数在20个Epoch之后,向着相反方向变化。训练组损失函数继续下降,验证组损失函数反而在上升,这就是典型的Overfitting(过拟合)现象。

![](https://ws2.sinaimg.cn/large/006tKfTcgy1g18l4i0ja7j30al0703z2.jpg)

过拟合就是模型过度地学习了训练集的特征,反而没法处理测试集中更一般化的问题。处理过拟合最根本的解决方法当然是获得更多的训练样本。

但是在无法获得更多的训练样本的时候,也有两个最简单的方法,一是对权重进行正则化处理,二就对神经元随机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://ws2.sinaimg.cn/large/006tKfTcgy1g18lqpke82j30al0703yx.jpg)

---
参考资料和数据来源
 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
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 341 others
properties (23)
authorhongtao
permlinktensorflow-overfitting
categorycn-stem
json_metadata{"community":"busy","app":"steemit/0.1","format":"markdown","tags":["cn-stem","tensorflow","keras","busy","cn"],"links":["https://steemit.com/cn-stem/@hongtao/tensorflow-keras-classification-with-keras","https://www.tensorflow.org/tutorials/","https://en.wikipedia.org/wiki/Overfitting","https://github.com/zht007/tensorflow-practice","https://www.kaggle.com/uciml/pima-indians-diabetes-database","https://www.jianshu.com/u/bd506afc6fc1"],"image":["https://ws2.sinaimg.cn/large/006tKfTcgy1g18l4i0ja7j30al0703z2.jpg","https://ws2.sinaimg.cn/large/006tKfTcgy1g18lqpke82j30al0703yx.jpg"]}
created2019-03-19 17:14:24
last_update2019-03-20 10:21:21
depth0
children10
last_payout2019-03-26 17:14:24
cashout_time1969-12-31 23:59:59
total_payout_value1.940 HBD
curator_payout_value0.670 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,802
author_reputation3,241,267,862,629
root_titleTensorflow入门——处理overfitting的问题
beneficiaries
0.
accountbusy.org
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,581,525
net_rshares4,194,329,033,010
author_curate_reward""
vote details (405)
@arcange ·
Congratulations @hongtao!
Your post was mentioned in the [Steem Hit Parade for newcomers](https://steemit.com/hit-parade/@arcange/daily-hit-parade-for-newcomers-20190319) in the following category:

* Upvotes - Ranked 9 with 385 upvotes

I also upvoted your post to increase its reward
If you like my work to promote newcomers and give them more visibility on the Steem blockchain, consider to [vote for my witness](https://steemit.com/~witnesses)!
properties (22)
authorarcange
permlinkre-tensorflow-overfitting-20190319t170645000z
categorycn-stem
json_metadata""
created2019-03-20 16:07:30
last_update2019-03-20 16:07:30
depth1
children2
last_payout2019-03-27 16:07:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length448
author_reputation1,146,606,601,469,178
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,629,315
net_rshares0
@hongtao ·
$0.07
Thanks, I voted your witness to support your work for newcomers.
👍  ,
properties (23)
authorhongtao
permlinkre-arcange-re-tensorflow-overfitting-20190320t161214776z
categorycn-stem
json_metadata{"tags":["cn-stem"],"app":"steemit/0.1"}
created2019-03-20 16:12:15
last_update2019-03-20 16:12:15
depth2
children1
last_payout2019-03-27 16:12:15
cashout_time1969-12-31 23:59:59
total_payout_value0.050 HBD
curator_payout_value0.016 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length64
author_reputation3,241,267,862,629
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,629,547
net_rshares101,342,221,916
author_curate_reward""
vote details (2)
@arcange ·
Thank you for you support and witness vote @Hongtao, really appreciated.
properties (22)
authorarcange
permlinkre-hongtao-re-arcange-re-tensorflow-overfitting-20190320t170043839z
categorycn-stem
json_metadata{"tags":["cn-stem"],"users":["hongtao"],"app":"steemit/0.1"}
created2019-03-20 17:00:45
last_update2019-03-20 17:00:45
depth3
children0
last_payout2019-03-27 17:00:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length72
author_reputation1,146,606,601,469,178
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,631,346
net_rshares0
@cnbuddy ·
帅哥/美女!想要参加活动但是不知道从何开始?关注寻宝团@cn-activity每日整理社区活动!倘若你想让我隐形,请回复“取消”。
properties (22)
authorcnbuddy
permlinkre-hongtao-tensorflow-overfitting-20190319t174009158z
categorycn-stem
json_metadata""
created2019-03-19 17:40:09
last_update2019-03-19 17:40:09
depth1
children0
last_payout2019-03-26 17:40:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation-1,449,160,991,441
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,582,432
net_rshares0
@robertyan · (edited)
提一些建议哦:

1. Github repo似乎重命名了文件夹,所以无法访问了,可以更新一下链接;
2. 可以最后加一个参考/引用的部分,包括:使用了哪些使用的公开数据(如Kaggle Dataset, US Census), 参考代码(如果有的话),参考文档或书籍(如TensorFlow 或 Keras的文档,如果值得读者去了解的话);
3. 不太清楚Steem的markdown是否有可能显示Jupyter Notebook或者RMarkdown,可以直接嵌入的话可能效果更好。(可能需要作者做些调研)
4. 内容方面,我觉得可以加入更多自己的实验或者批判,如果只是把最基本的回归、分类、过拟合等问题介绍一遍,我觉得效果可能不如直接看文档或教材。所以,最好能添加更多自己的想法,比如学习的感悟和建议等等,这对于作者的学习以及读者的借鉴,都会有所助益。
👍  
properties (23)
authorrobertyan
permlinkre-hongtao-tensorflow-overfitting-20190320t072747208z
categorycn-stem
json_metadata{"tags":["cn-stem"],"app":"steemit/0.1"}
created2019-03-20 07:27:48
last_update2019-03-20 07:46:09
depth1
children2
last_payout2019-03-27 07:27:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length379
author_reputation16,909,391,530,163
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,610,830
net_rshares455,229,484
author_curate_reward""
vote details (1)
@hongtao ·
谢谢建议,
1. 昨天重新commit 了代码,整理了子目录,忘了改链接地址了,已经将子目录替换成了项目地址。
2.公开数据的来源在源代码中已经做了说明,接受建议,在文末添加了。
3. Steemit的Markdown确实对代码支持不太友好,没有语法高亮,目前除了截图好像还没找到其他都方法。
4. 本人在这个领域也是初学者,我会在后续的文章中尽量加入自己的思考
👍  
properties (23)
authorhongtao
permlinkre-robertyan-re-hongtao-tensorflow-overfitting-20190320t102155337z
categorycn-stem
json_metadata{"tags":["cn-stem"],"app":"steemit/0.1"}
created2019-03-20 10:21:54
last_update2019-03-20 10:21:54
depth2
children1
last_payout2019-03-27 10:21:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length181
author_reputation3,241,267,862,629
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,616,129
net_rshares4,418,924,261
author_curate_reward""
vote details (1)
@robertyan · (edited)
哈哈,不客气,我只是从写此类分享文章的角度提一下自己的看法。😀

Steemit代码展示我也有些不了解,晚些看到方法我分享一下😛

你有加入新手村吗?可以联系一下村长 [@ericet](https://steemit.com/@ericet) 他的微信也是ericet
properties (22)
authorrobertyan
permlinkrobertyan-re-hongtao-re-robertyan-re-hongtao-tensorflow-overfitting-20190320t104058198z
categorycn-stem
json_metadata{"app":"partiko","client":"ios"}
created2019-03-20 10:41:45
last_update2019-03-20 10:43:36
depth3
children0
last_payout2019-03-27 10:41:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length135
author_reputation16,909,391,530,163
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,616,711
net_rshares0
@softmetal ·
结尾漏了同步到你的简书。。。
properties (22)
authorsoftmetal
permlinkre-hongtao-tensorflow-overfitting-20190319t174728357z
categorycn-stem
json_metadata{"tags":["cn-stem"],"app":"steemit/0.1"}
created2019-03-19 17:47:27
last_update2019-03-19 17:47:27
depth1
children1
last_payout2019-03-26 17:47:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length14
author_reputation146,291,910,015,030
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,582,702
net_rshares0
@hongtao ·
哈哈 谢谢提醒

Posted using [Partiko iOS](https://partiko.app/referral/hongtao)
properties (22)
authorhongtao
permlinkhongtao-re-softmetal-re-hongtao-tensorflow-overfitting-20190319t190356605z
categorycn-stem
json_metadata{"app":"partiko","client":"ios"}
created2019-03-19 19:04:03
last_update2019-03-19 19:04:03
depth2
children0
last_payout2019-03-26 19:04:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length73
author_reputation3,241,267,862,629
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,585,303
net_rshares0
@steemstem ·
$0.79
re-hongtao-tensorflow-overfitting-20190320t131706039z
<div class='text-justify'> <div class='pull-left'> <br /> <center> <img width='125' src='https://i.postimg.cc/9FwhnG3w/steemstem_curie.png'> </center>  <br/> </div> <br /> <br /> 

 This post has been voted on by the **SteemSTEM** curation team and voting trail in collaboration with **@curie**. <br /> 
 If you appreciate the work we are doing then consider [voting](https://www.steemit.com/~witnesses) both projects for witness by selecting [**stem.witness**](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=stem.witness) and [**curie**](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=curie)! <br /> 
For additional information please join us on the [**SteemSTEM discord**]( https://discord.gg/BPARaqn) and to get to know the rest of the community! </div>
👍  , , ,
properties (23)
authorsteemstem
permlinkre-hongtao-tensorflow-overfitting-20190320t131706039z
categorycn-stem
json_metadata{"app":"bloguable-bot"}
created2019-03-20 13:17:12
last_update2019-03-20 13:17:12
depth1
children0
last_payout2019-03-27 13:17:12
cashout_time1969-12-31 23:59:59
total_payout_value0.593 HBD
curator_payout_value0.197 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length800
author_reputation262,017,435,115,313
root_titleTensorflow入门——处理overfitting的问题
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,622,131
net_rshares1,178,205,220,694
author_curate_reward""
vote details (4)