浅谈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列表操作实例
Jan 14 Python
Python实现队列的方法
May 26 Python
python魔法方法-属性访问控制详解
Jul 25 Python
Windows系统下多版本pip的共存问题详解
Oct 10 Python
TensorFlow实现AutoEncoder自编码器
Mar 09 Python
对Pandas DataFrame缺失值的查找与填充示例讲解
Nov 06 Python
python 将有序数组转换为二叉树的方法
Mar 26 Python
pandas 使用均值填充缺失值列的小技巧分享
Jul 04 Python
python配置文件写入过程详解
Oct 19 Python
numpy库ndarray多维数组的维度变换方法(reshape、resize、swapaxes、flatten)
Apr 28 Python
python爬虫selenium模块详解
Mar 30 Python
matlab xlabel位置的设置方式
May 21 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
星际争霸教主Flash的ID由来:你永远不会知道他之前的ID是www!
2019/01/18 星际争霸
php生成txt文件标题及内容的方法
2014/01/16 PHP
解析arp病毒背后利用的Javascript技术附解密方法
2007/08/06 Javascript
Javascript 网页黑白效果实现代码(兼容IE/FF等)
2010/04/23 Javascript
js数据验证集合、js email验证、js url验证、js长度验证、js数字验证等简单封装
2010/05/15 Javascript
在js(jquery)中获得文本框焦点和失去焦点的方法
2012/12/04 Javascript
基于jquery实现控制经纬度显示地图与卫星
2013/05/20 Javascript
动态加载jquery库的方法
2014/02/12 Javascript
js单词形式的运算符
2014/05/06 Javascript
Jquery之Bind方法参数传递与接收的三种方法
2014/06/24 Javascript
JS实现超过长度限制后自动跳转下一款文本框的方法
2015/02/23 Javascript
Bootstrap 附加导航(Affix)插件实例详解
2016/06/01 Javascript
jQuery实现简单的网页换肤效果示例
2016/09/18 Javascript
jquery获取链接地址和跳转详解(推荐)
2017/08/15 jQuery
JS实现将对象转化为数组的方法分析
2019/01/21 Javascript
Vue组件内部实现一个双向数据绑定的实例代码
2019/04/04 Javascript
这应该是最详细的响应式系统讲解了
2019/07/22 Javascript
uniapp实现可以左右滑动导航栏
2020/10/21 Javascript
Python调用SQLPlus来操作和解析Oracle数据库的方法
2016/04/09 Python
Python实现二维有序数组查找的方法
2016/04/27 Python
python2.7 mayavi 安装图文教程(推荐)
2017/06/22 Python
Python使用sort和class实现的多级排序功能示例
2018/08/15 Python
Python中collections模块的基本使用教程
2018/12/07 Python
在Pytorch中使用样本权重(sample_weight)的正确方法
2019/08/17 Python
python爬虫中多线程的使用详解
2019/09/23 Python
python 实现仿微信聊天时间格式化显示的代码
2020/04/17 Python
HTML5本地存储之Web Storage详解
2016/07/04 HTML / CSS
php优化查询foreach代码实例讲解
2021/03/24 PHP
在校大学生个人的自我评价
2014/02/13 职场文书
《月球之谜》教学反思
2014/04/10 职场文书
2014年入党积极分子党校培训心得体会
2014/07/08 职场文书
文秘班元旦晚会活动策划方案
2014/08/28 职场文书
课外访万家心得体会
2014/09/03 职场文书
试用期旷工辞退通知书
2015/04/17 职场文书
师德承诺书2015
2015/04/28 职场文书
宪法宣传标语100条
2019/10/15 职场文书