浅谈keras 模型用于预测时的注意事项

作者:机器AI 时间:2022-10-16 13:23:04 

为什么训练误差比测试误差高很多?

一个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就是具体的标签了。

来源:https://blog.csdn.net/xiaojiajia007/article/details/73771311

标签:keras,模型,预测
0
投稿

猜你喜欢

  • openCV显著性检测的使用

    2022-10-20 12:25:02
  • Python 计算机视觉编程进阶之OpenCV 图像锐化及边缘检测

    2021-07-31 11:03:29
  • PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】

    2023-07-23 02:36:20
  • python 的 scapy库,实现网卡收发包的例子

    2021-08-08 17:25:05
  • Python中Selenium上传文件的几种方式

    2022-08-13 04:10:40
  • 基于sqlserver的四种分页方式总结

    2024-01-13 22:34:33
  • asp如何用Jmail组件的发送电子邮件?

    2010-06-12 12:51:00
  • Python 中导入文本文件的示例代码

    2021-04-18 04:53:01
  • python numpy库之如何使用matpotlib库绘图

    2023-02-07 22:22:24
  • Asp中通过简单的例子理解下ByVal和ByRef的用法

    2011-02-20 10:57:00
  • vue2.x集成百度UEditor富文本编辑器的方法

    2024-05-28 15:47:59
  • Python3.x和Python2.x的区别介绍

    2022-01-05 12:36:27
  • 基于Element的组件改造的树形选择器(树形下拉框)

    2024-05-28 16:01:30
  • python中defaultdict的用法详解

    2021-05-02 11:11:07
  • java连接mysql数据库乱码的解决方法

    2024-01-21 06:26:15
  • echarts动态获取Django数据的实现示例

    2022-08-26 02:29:06
  • Python中if语句的基本格式实例代码

    2023-12-02 14:31:20
  • Python全栈之线程详解

    2021-05-21 17:44:21
  • Ie6不支持max的解决办法

    2008-12-31 13:11:00
  • Windows10下 python3.7 安装 facenet的教程

    2022-06-21 11:17:01
  • asp之家 网络编程 m.aspxhome.com