浅谈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 正则表达式操作指南
May 04 Python
Python中的super用法详解
May 28 Python
wxPython定时器wx.Timer简单应用实例
Jun 03 Python
Python的爬虫框架scrapy用21行代码写一个爬虫
Apr 24 Python
python实现协同过滤推荐算法完整代码示例
Dec 15 Python
对numpy中布尔型数组的处理方法详解
Apr 17 Python
了解不常见但是实用的Python技巧
May 23 Python
Python中typing模块与类型注解的使用方法
Aug 05 Python
解决Python在导入文件时的FileNotFoundError问题
Apr 10 Python
python selenium xpath定位操作
Sep 01 Python
python3爬虫中多线程进行解锁操作实例
Nov 25 Python
pytest fixtures装饰器的使用和如何控制用例的执行顺序
Jan 28 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 has encountered an Access Violation 错误的解决方法
2010/01/17 PHP
php 编写安全的代码时容易犯的错误小结
2010/05/20 PHP
列表内容的选择
2006/06/30 Javascript
JS 面向对象的5钟写法
2009/07/31 Javascript
js实现GridView单选效果自动设置交替行、选中行、鼠标移动行背景色
2010/05/27 Javascript
通过jquery的$.getJSON做一个跨域ajax请求试验
2011/05/03 Javascript
onkeypress字符按键兼容所有浏览器使用介绍
2013/04/24 Javascript
轻松创建nodejs服务器(9):实现非阻塞操作
2014/12/18 NodeJs
jquery实现拖动效果
2016/08/10 Javascript
JavaScript 闭包详细介绍
2016/09/28 Javascript
JS对象是否拥有某属性如何判断
2017/02/03 Javascript
详解Vue中使用v-for语句抛出错误的解决方案
2017/05/04 Javascript
javascript编程开发中取色器及封装$函数用法示例
2017/08/09 Javascript
利用JS制作万年历的方法
2017/08/16 Javascript
Vue集成Iframe页面的方法示例
2017/12/12 Javascript
vue实现密码显示隐藏切换功能
2018/02/23 Javascript
Layer.js实现表格溢出内容省略号显示,悬停显示全部的方法
2019/09/16 Javascript
Python连接PostgreSQL数据库的方法
2016/11/28 Python
Python函数和模块的使用总结
2019/05/20 Python
详解利用python+opencv识别图片中的圆形(霍夫变换)
2019/07/01 Python
django框架单表操作之增删改实例分析
2019/12/16 Python
python图片剪裁代码(图片按四个点坐标剪裁)
2020/03/10 Python
python向xls写入数据(包括合并,边框,对齐,列宽)
2021/02/02 Python
Python基于爬虫实现全网搜索并下载音乐
2021/02/14 Python
html svg生成环形进度条的实现方法
2019/09/23 HTML / CSS
澳大利亚便宜的家庭购物网站:CrazySales
2018/02/06 全球购物
Tomcat中怎么使用log4j输出所有的log
2016/07/07 面试题
android面试问题与答案
2016/12/27 面试题
夜大自我鉴定
2013/10/31 职场文书
杠杆的科学教学反思
2014/01/10 职场文书
《青蛙看海》教学反思
2014/04/23 职场文书
国贸专业毕业求职信
2014/06/11 职场文书
社会工作专业求职信
2014/07/15 职场文书
2015年小学开学寄语
2015/02/27 职场文书
八一建军节主持词
2015/07/01 职场文书
2019年大学毕业生个人自我鉴定范文大全
2019/03/21 职场文书