浅谈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 22 Python
python类中super()和__init__()的区别
Oct 18 Python
python快速建立超简单的web服务器的实现方法
Feb 17 Python
Python操作MySQL数据库的两种方式实例分析【pymysql和pandas】
Mar 18 Python
Python 中的 global 标识对变量作用域的影响
Aug 12 Python
Python中的延迟绑定原理详解
Oct 11 Python
pygame实现贪吃蛇游戏(下)
Oct 29 Python
Python处理PDF与CDF实例
Feb 26 Python
Python中os模块功能与用法详解
Feb 26 Python
python异常处理、自定义异常、断言原理与用法分析
Mar 23 Python
Python实现PS滤镜中的USM锐化效果
Dec 04 Python
python之json文件转xml文件案例讲解
Aug 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数组中删除元素的实现代码
2012/06/22 PHP
Session服务器配置指南与使用经验的深入解析
2013/06/17 PHP
php+ajax无刷新上传图片实例代码
2015/11/17 PHP
PHP list() 将数组中的值赋给变量的简单实例
2016/06/13 PHP
PHP使用数组实现矩阵数学运算的方法示例
2017/05/29 PHP
PHP开发API接口签名生成及验证操作示例
2020/05/27 PHP
最常用的12种设计模式小结
2011/08/09 Javascript
jquery乱码与contentType属性设置问题解决方案
2013/01/07 Javascript
jQuery中noconflict函数的实现原理分解
2015/02/03 Javascript
JS点击链接后慢慢展开隐藏着图片的方法
2015/02/17 Javascript
jQuery中closest和parents的区别分析
2015/05/07 Javascript
JS实现部分HTML固定页面顶部随屏滚动效果
2015/12/24 Javascript
JS代码实现百度地图 画圆 删除标注
2016/10/12 Javascript
使用JavaScript触发过渡效果的方法
2017/01/19 Javascript
基于express中路由规则及获取请求参数的方法
2018/03/12 Javascript
node+express框架中连接使用mysql(经验总结)
2018/11/10 Javascript
node express使用HTML模板的方法示例
2019/08/22 Javascript
微信小程序实现比较功能的方法汇总(五种方法)
2020/03/07 Javascript
Python实现的数据结构与算法之快速排序详解
2015/04/22 Python
深度定制Python的Flask框架开发环境的一些技巧总结
2016/07/12 Python
Tensorflow使用支持向量机拟合线性回归
2018/09/07 Python
pandas 空的dataframe 插入列名的示例
2018/10/30 Python
python 产生token及token验证的方法
2018/12/26 Python
Python之列表实现栈的工作功能
2019/01/28 Python
Python线上环境使用日志的及配置文件
2019/07/28 Python
html5的canvas元素使用方法介绍(画矩形、画折线、圆形)
2014/04/14 HTML / CSS
Does C# support multiple inheritance? (C#支持多重继承吗)
2012/01/04 面试题
法人委托书的范本格式
2014/09/11 职场文书
交通事故委托书范本
2014/09/28 职场文书
教师查摆问题及整改措施
2014/10/11 职场文书
2015年学校安全管理工作总结
2015/05/11 职场文书
讲座开场白台词和结束语
2015/05/29 职场文书
心灵捕手观后感
2015/06/02 职场文书
开国大典观后感
2015/06/04 职场文书
读《庄子》有感:美而不自知
2019/11/06 职场文书
前端JS获取URL参数的4种方法总结
2022/04/05 Javascript