浅谈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计算圆周长、面积、球体体积并画出圆
Apr 08 Python
Python创建系统目录的方法
Mar 11 Python
[原创]Python入门教程5. 字典基本操作【定义、运算、常用函数】
Nov 01 Python
python从子线程中获得返回值的方法
Jan 30 Python
Python 虚拟空间的使用代码详解
Jun 10 Python
Python 多个图同时在不同窗口显示的实现方法
Jul 07 Python
np.newaxis 实现为 numpy.ndarray(多维数组)增加一个轴
Nov 30 Python
使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
Mar 16 Python
python中使用input()函数获取用户输入值方式
May 03 Python
解决Python安装cryptography报错问题
Sep 03 Python
Pycharm编辑器功能之代码折叠效果的实现代码
Oct 15 Python
Python图像处理之图像拼接
Apr 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执行速度全攻略(上)
2006/10/09 PHP
php自动给文章加关键词链接的函数代码
2012/11/29 PHP
javascript中的一些注意事项 更新中
2010/12/06 Javascript
jquery利用event.which方法获取键盘输入值的代码
2011/10/09 Javascript
JS获取select的value和text值的简单实例
2014/02/26 Javascript
深入理解JavaScript系列(19):求值策略(Evaluation strategy)详解
2015/03/05 Javascript
js实现带按钮的上下滚动效果
2015/05/12 Javascript
JavaScript判断DIV内容是否为空的方法
2016/01/29 Javascript
基于BootStrap Metronic开发框架经验小结【九】实现Web页面内容的打印预览和保存操作
2016/05/12 Javascript
Vue.js 2.0 和 React、Augular等其他前端框架大比拼
2016/10/08 Javascript
js实现自动图片轮播代码
2017/03/22 Javascript
简单好用的nodejs 爬虫框架分享
2017/03/26 NodeJs
Easyui和zTree两种方式分别实现树形下拉框
2017/08/04 Javascript
JS设计模式之命令模式概念与用法分析
2018/02/06 Javascript
解决nodejs的npm命令无反应的问题
2018/05/17 NodeJs
浅谈HTTP 缓存的那些事儿
2018/10/17 Javascript
Python中类的继承代码实例
2014/10/28 Python
利用Python绘制数据的瀑布图的教程
2015/04/07 Python
Python SVM(支持向量机)实现方法完整示例
2018/06/19 Python
Python socket套接字实现C/S模式远程命令执行功能案例
2018/07/06 Python
一百行python代码将图片转成字符画
2021/02/19 Python
pandas分别写入excel的不同sheet方法
2018/12/11 Python
Python如何爬取qq音乐歌词到本地
2020/06/01 Python
matlab、python中矩阵的互相导入导出方式
2020/06/01 Python
BeautifulSoup中find和find_all的使用详解
2020/12/07 Python
使用spring mvc+localResizeIMG实现HTML5端图片压缩上传的功能
2016/12/16 HTML / CSS
波兰在线香水店:Perfumy.pl
2019/08/12 全球购物
W Hamond官网:始于1979年的钻石专家
2020/07/20 全球购物
新学期国旗下演讲稿
2014/05/08 职场文书
会计专业毕业生自荐书
2014/06/25 职场文书
教师个人读书活动总结
2014/07/08 职场文书
北京离婚协议书范文2014
2014/09/29 职场文书
安娜卡列尼娜观后感
2015/06/11 职场文书
Go语言基础map用法及示例详解
2021/11/17 Golang
开发微信小程序之WXSS样式教程
2022/04/18 HTML / CSS
TS 类型兼容教程示例详解
2022/09/23 Javascript