浅谈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实现文件分组复制到不同目录的例子
Jun 04 Python
go语言计算两个时间的时间差方法
Mar 13 Python
python根据京东商品url获取产品价格
Aug 09 Python
详解python中的json和字典dict
Jun 22 Python
在Python dataframe中出生日期转化为年龄的实现方法
Oct 20 Python
对Python中DataFrame选择某列值为XX的行实例详解
Jan 29 Python
在Python 中同一个类两个函数间变量的调用方法
Jan 31 Python
python 日期排序的实例代码
Jul 11 Python
Python小整数对象池和字符串intern实例解析
Mar 21 Python
python matplotlib模块基本图形绘制方法小结【直线,曲线,直方图,饼图等】
Apr 26 Python
小 200 行 Python 代码制作一个换脸程序
May 12 Python
python 基于selectors库实现文件上传与下载
Dec 31 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中如何定义和使用常量
2013/02/28 PHP
php+redis实现注册、删除、编辑、分页、登录、关注等功能示例
2017/02/15 PHP
PHP如何搭建百度Ueditor富文本编辑器
2018/09/21 PHP
Nigma vs AM BO3 第一场2.13
2021/03/10 DOTA
完整显示当前日期和时间的JS代码
2007/09/17 Javascript
jquery实现的带缩略图的焦点图片切换(自动播放/响应鼠标动作)
2013/01/23 Javascript
在JS中如何调用JSP中的变量
2014/01/22 Javascript
从零学JS之你需要了解的几本书
2014/05/19 Javascript
JavaScript通过Date-Mask将日期转换成字符串的方法
2015/06/04 Javascript
BootStrap中的table实现数据填充与分页应用小结
2016/05/26 Javascript
关于Function中的bind()示例详解
2016/12/02 Javascript
BootStrap Fileinput上传插件使用实例代码
2017/07/28 Javascript
详解bootstrap用dropdown-menu实现上下文菜单
2017/09/22 Javascript
vue实现a标签点击高亮方法
2018/03/17 Javascript
由浅入深讲解python中的yield与generator
2017/04/05 Python
python中的迭代和可迭代对象代码示例
2017/12/27 Python
Python对excel文档的操作方法详解
2018/12/10 Python
opencv与numpy的图像基本操作
2019/03/08 Python
Keras使用ImageNet上预训练的模型方式
2020/05/23 Python
python小白学习包管理器pip安装
2020/06/09 Python
pandas DataFrame运算的实现
2020/06/14 Python
Marmot土拨鼠官网:美国专业户外运动品牌
2018/01/11 全球购物
香港交友网站:be2香港
2018/07/22 全球购物
小学教师国培感言
2014/02/08 职场文书
工程资料员岗位职责
2014/03/10 职场文书
捐助倡议书范文
2014/04/15 职场文书
气象学专业个人求职信
2014/04/22 职场文书
2014年护理工作总结范文
2014/11/14 职场文书
作弊检讨书
2015/01/27 职场文书
市场部经理岗位职责
2015/02/02 职场文书
前台文员岗位职责
2015/02/04 职场文书
老乡聚会通知
2015/04/23 职场文书
2019年暑期法院实习报告
2019/12/18 职场文书
如何在CocosCreator里画个炫酷的雷达图
2021/04/16 Javascript
MySQL root密码的重置方法
2021/04/21 MySQL
在ubuntu下安装go开发环境的全过程
2022/08/05 Golang