浅谈keras 模型用于预测时的注意事项


Posted in Python onJune 27, 2020

为什么训练误差比测试误差高很多?

一个Keras的模型有两个模式:训练模式测试模式一些正则机制,如Dropout,L1/L2正则项在测试模式下将不被启用。

另外,训练误差是训练数据每个batch的误差的平均。在训练过程中,每个epoch起始时的batch的误差要大一些,而后面的batch的误差要小一些。另一方面,每个epoch结束时计算的测试误差是由模型在epoch结束时的状态决定的,这时候的网络将产生较小的误差。

【Tips】可以通过定义回调函数将每个epoch的训练误差和测试误差并作图,如果训练误差曲线和测试误差曲线之间有很大的空隙,说明你的模型可能有过拟合的问题。当然,这个问题与Keras无关。

在keras中文文档中指出了这一误区,笔者认为产生这一问题的原因在于网络实现的机制。即dropout层有前向实现和反向实现两种方式,这就决定了概率p是在训练时候设置还是测试的时候进行设置

利用预训练的权值进行Fine tune时的注意事项:

不能把自己添加的层进行将随机初始化后直接连接到前面预训练后的网络层

in order to perform fine-tuning, all layers should start with properly trained weights: for instance you should not slap a randomly initialized fully-connected network on top of a pre-trained convolutional base. This is because the large gradient updates triggered by the randomly initialized weights would wreck the learned weights in the convolutional base. In our case this is why we first train the top-level classifier, and only then start fine-tuning convolutional weights alongside it.

we choose to only fine-tune the last convolutional block rather than the entire network in order to prevent overfitting, since the entire network would have a very large entropic capacity and thus a strong tendency to overfit. The features learned by low-level convolutional blocks are more general, less abstract than those found higher-up, so it is sensible to keep the first few blocks fixed (more general features) and only fine-tune the last one (more specialized features).

fine-tuning should be done with a very slow learning rate, and typically with the SGD optimizer rather than an adaptative learning rate optimizer such as RMSProp. This is to make sure that the magnitude of the updates stays very small, so as not to wreck the previously learned features.

补充知识:keras框架中用keras.models.Model做的时候预测数据不是标签的问题

我们发现,在用Sequential去搭建网络的时候,其中有predict和predict_classes两个预测函数,前一个是返回的精度,后面的是返回的具体标签。但是,在使用keras.models.Model去做的时候,就会发现,它只有一个predict函数,没有返回标签的predict_classes函数,所以,针对这个问题,我们将其改写。改写如下:

def my_predict_classes(predict_data):
  if predict_data.shape[-1] > 1:
    return predict_data.argmax(axis=-1)
  else:
    return (predict_data > 0.5).astype('int32')
 
# 这里省略网络搭建部分。。。。
 
model = Model(data_input, label_output)
model.compile(loss='categorical_crossentropy',
       optimizer=keras.optimizers.Nadam(lr=0.002),
       metrics=['accuracy'])
model.summary()
 
y_predict = model.predict(X_test)
y_pre = my_predict_classes(y_predict)

这样,y_pre就是具体的标签了。

以上这篇浅谈keras 模型用于预测时的注意事项就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python with的用法
Aug 22 Python
Python中使用摄像头实现简单的延时摄影技术
Mar 27 Python
使用Python的内建模块collections的教程
Apr 28 Python
用Python将动态GIF图片倒放播放的方法
Nov 02 Python
python正则表达式爬取猫眼电影top100
Feb 24 Python
python 快速把超大txt文件转存为csv的实例
Oct 26 Python
对python中xlsx,csv以及json文件的相互转化方法详解
Dec 25 Python
Tensorflow 多线程与多进程数据加载实例
Feb 05 Python
Python发送邮件实现基础解析
Aug 14 Python
Django创建一个后台的基本步骤记录
Oct 02 Python
python中str内置函数用法总结
Dec 27 Python
详解Python中*args和**kwargs的使用
Apr 07 Python
python suds访问webservice服务实现
Jun 26 #Python
解析Python 偏函数用法全方位实现
Jun 26 #Python
Python如何优雅删除字符列表空字符及None元素
Jun 25 #Python
使用pytorch实现论文中的unet网络
Jun 24 #Python
python连接mysql有哪些方法
Jun 24 #Python
pytorch VGG11识别cifar10数据集(训练+预测单张输入图片操作)
Jun 24 #Python
Python Tornado核心及相关原理详解
Jun 24 #Python
You might like
用PHP开发GUI
2006/10/09 PHP
如何在PHP中使用Oracle数据库(3)
2006/10/09 PHP
PHP Zip压缩 在线对文件进行压缩的函数
2010/05/26 PHP
带密匙的php加密解密示例分享
2014/01/29 PHP
php轻量级的性能分析工具xhprof的安装使用
2015/08/12 PHP
30分钟就入门的正则表达式基础教程
2013/02/25 Javascript
JavaScript文本框脚本编写的注意事项
2016/01/25 Javascript
Bootstrap学习笔记之css组件(3)
2016/06/07 Javascript
JavaScript关于提高网站性能的几点建议(一)
2016/07/24 Javascript
利用JQuery实现datatables插件的增加和删除行功能
2017/01/06 Javascript
JS中type="button"和type="submit"的区别
2017/07/04 Javascript
js实现省市级联效果分享
2017/08/10 Javascript
js实现简单数字变动效果
2017/11/06 Javascript
React Native之prop-types进行属性确认详解
2017/12/19 Javascript
nodejs 简单实现动态html的方法
2018/05/12 NodeJs
js代码规范之Eslint安装与配置详解
2018/09/08 Javascript
Vue CLI 3.x 自动部署项目至服务器的方法
2019/04/02 Javascript
JavaScript实现网页计算器功能
2020/10/29 Javascript
[49:20]VG vs TNC Supermajor小组赛B组败者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
Python实现字典依据value排序
2016/02/24 Python
用python统计代码行的示例(包括空行和注释)
2018/07/24 Python
使用pycharm在本地开发并实时同步到服务器
2019/08/02 Python
Python 中pandas索引切片读取数据缺失数据处理问题
2019/10/09 Python
python的pyecharts绘制各种图表详细(附代码)
2019/11/11 Python
Python绘图实现台风路径可视化代码实例
2020/10/23 Python
Python实现网络聊天室的示例代码(支持多人聊天与私聊)
2021/01/27 Python
css3的过滤效果简单实例
2016/08/03 HTML / CSS
FC-Moto瑞典:欧洲最大的摩托车服装和头盔商店之一
2018/11/27 全球购物
美国饼干礼物和美食甜点购买网站:Cheryl’s
2020/05/28 全球购物
大一期末自我鉴定
2013/12/13 职场文书
2014年社区居委会主任重阳节讲话稿
2014/09/25 职场文书
工程资料员岗位职责
2015/04/13 职场文书
员工福利申请报告
2015/05/15 职场文书
《中国古代诗歌散文欣赏》高中语文教材
2019/08/20 职场文书
自己搭建resnet18网络并加载torchvision自带权重的操作
2021/05/13 Python
Python import模块的缓存问题解决方案
2021/06/02 Python