keras 构建模型很简单,上手很方便,同时又是 tensorflow 的高级 API,所以学学也挺好。

模型复现在我们的实验中也挺重要的,跑出了一个模型,虽然我们可以将模型的 checkpoint 保存,但再跑一遍,怎么都得不到相同的结果。

用 keras 实现模型,想要能够复现,首先需要设置各个可能的随机过程的 seed,如 np.random.seed(1)。然后分为两种情况:

  1. 代码不要在 GPU 上跑,而是限制在 CPU 上跑,此时可以自行设置 fit 函数的 batch_size 参数;
  2. 代码可以在 GPU 上跑,需要设置 fit 函数的参数 batch_size = 1。(当使用 tf.keras.conv2D() 时,似乎在 GPU 上跑没法复现,最好使用第一种方式,只在 CPU 上跑。)

我的 tensorflow+keras 版本:

print(tf.VERSION)    # '1.10.0'
print(tf.keras.__version__)    # '2.1.6-tf'

keras 模型可复现的配置:

import numpy as np
import tensorflow as tf
import random as rnimport os
# run on CPU only, if you want to run code on GPU, you should delete the following line.
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
os.environ["PYTHONHASHSEED"] = '0'# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.np.random.seed(42)# The below is necessary for starting core Python generated random numbers
# in a well-defined state.rn.seed(12345)# Force TensorFlow to use single thread.
# Multiple threads are a potential source of non-reproducible results.
# For further details, see: https://stackoverflow.com/questions/42022950/session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,inter_op_parallelism_threads=1)from keras import backend as K# The below tf.set_random_seed() will make random number generation
# in the TensorFlow backend have a well-defined initial state.
# For further details, see:
# https://www.tensorflow.org/api_docs/python/tf/set_random_seedtf.set_random_seed(1234)sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)# Rest of code follows ...

对于 tensorflow low-level API,即用 tf.variable_scope() 和 tf.get_variable() 自行构建 layers,同样会出现这种问题。

keras 文档 对此的解释是:

Moreover, when using the TensorFlow backend and running on a GPU, some operations have non-deterministic outputs, in particular tf.reduce_sum(). This is due to the fact that GPUs run many operations in parallel, so the order of execution is not always guaranteed. Due to the limited precision of floats, even adding several numbers together may give slightly different results depending on the order in which you add them. You can try to avoid the non-deterministic operations, but some may be created automatically by TensorFlow to compute the gradients, so it is much simpler to just run the code on the CPU.

而 pytorch 是怎么保证可复现:(cudnn中对卷积操作进行了优化,牺牲了精度来换取计算效率。可以看到,下面的代码强制 cudnn 产生确定性的结果,但会牺牲效率。具体参见博客 PyTorch的可重复性问题 (如何使实验结果可复现))

from torch.backends import cudnn
cudnn.benchmark = False            # if benchmark=True, deterministic will be False
cudnn.deterministic = True

References

How can I obtain reproducible results using Keras during development? -- Keras Documentation
具有Tensorflow后端的Keras可以随意使用CPU或GPU吗?
PyTorch的可重复性问题 (如何使实验结果可复现)-- hyk_1996

转载于:https://www.cnblogs.com/wuliytTaotao/p/10883749.html

【tf.keras】tf.keras模型复现相关推荐

  1. 4-9-6 tf.keras入门(附带复现cvpr论文流程与代码)

    文章目录 前言 keras学习 keras 建模介绍 keras的三个层级 sequential model 的要点 keras layer的理解 sequential 简单演示 Function A ...

  2. 【附代码实现】Attention注意力模块的keras\tf实现(ECA、BAM、Coordinate、DualAttention、GlobalContext等)

    文章目录 前言 1. ECA 2. Coordinate attention 3. Dual attention 4. FrequencyChannelAttention 5. BAM 6.Globa ...

  3. keras学习- No module named ' tensorflow.keras ' 报错,看清 tf.keras与keras

    环境描述: 系统ubantu16.04 安装anaconda  版本conda 4.5.4 创建虚拟环境 tf-gpu tensorflow-gpu版本(1.7.0-gpu, 能够import ten ...

  4. TensorFlow 2.0中的tf.keras和Keras有何区别?为什么以后一定要用tf.keras?

    选自pyimagesearch 作者:Adrian Rosebrock 参与:王子嘉.张倩 本文经机器之心授权转载,禁止二次转载 随着 TensorFlow 2.0 的发布,不少开发者产生了一些疑惑: ...

  5. tensorflow从入门到精通100讲(七)-TensorFlow房价预估使用Keras快速构建模型

    前言 这篇文章承接上一篇tensorflow从入门到精通100讲(二)-IRIS数据集应用实战 https://wenyusuran.blog.csdn.net/article/details/107 ...

  6. as转html5工具,将keras的h5模型转换为tensorflow的pb模型

    背景:目前keras框架使用简单,很容易上手,深得广大算法工程师的喜爱,但是当部署到客户端时,可能会出现各种各样的bug,甚至不支持使用keras,本文来解决的是将keras的h5模型转换为客户端常用 ...

  7. 为什么将表格的method改为post后就无法工作_用Python将Keras深度学习模型部署为Web应用程序...

    构建一个很棒的机器学习项目是一回事,但归根结底,你希望其他人能够看到你的辛勤工作.当然,你可以将整个项目放在GitHub上,但是怎么让你的祖父母也看到呢?我们想要的是将深度学习模型部署为世界上任何人都 ...

  8. Tensorflow.Keras 时序回归模型的建立

    Learn from Keras官方网站 目前tensorflow 2.0版本已经集成了keras的所有功能,所以直接安装tensorflow就可以调用Keras,非常方便. 作为Keras入门可以看 ...

  9. Keras的Model模型使用

    本主题主要阐述下Keras框架中的模型Model的使用,主要包含的内容:   1.模型的两种使用方式:   2.经典模型的实现(定制模型):   3.模型的定制训练: 一. 模型使用的两种方式 Ker ...

最新文章

  1. 如何将本地项目上传到自己的GitHub上
  2. ALEIDoc EDI(4)--change point02
  3. 经典网页设计:10个响应式设计的国外购物网站
  4. boost::hana::when用法的测试程序
  5. 数据挖掘之关联分析六(子图模式)
  6. 走近分形与混沌(part13)--自然现象就其本质来说,是复杂而非线性的
  7. 如何使用Spring Security和Basic身份验证保护Jersey REST服务
  8. 网关 Spring Cloud Gateway
  9. Flink – SlotSharingGroup
  10. 连接本地虚拟机和数据库很慢
  11. sql 时间查询 /sql中判断更新或者插入/查询一年所有双休日
  12. 主动轮廓线模型Snake模型简介amp;openCV中cvSnakeImage()函数代码分析
  13. 会员直推奖php程序_直推奖+对碰奖+*奖+互助奖+见点奖
  14. mysql io瓶颈_服务器IO瓶颈对MySQL性能的影响
  15. Android Studio Gradle build daemon disappeared unexpectedly
  16. ios dyld: Library not loaded: @rpath/xxx.framework/xxx 之根本原因
  17. SEO的关键字分析的浅析
  18. speedoffice(Excel)表格怎么自动求和
  19. 死锁 怎么产生的 四个条件符合其一都不会产生死锁
  20. 【LeetCode】476. 数字的补数 Number Complement

热门文章

  1. st7789 照片转数组软件_干货 | 新闻人实用软件安利
  2. ipv6单播地址包括哪两种类型_IPv6基础介绍
  3. react 界面渲染完成 立即执行_React原理解析fiber、diff
  4. 控制台修改应用端口_应用架构六边型架构:三个原则和一个实现示例
  5. 同步手机文件到家庭服务器,同步文件到服务器
  6. linux ping 虚拟网卡_Linux下添加虚拟网卡,实现一块物理网卡绑定多个IP地址
  7. mysql每10万条数据分区_WebGIS项目中利用mysql控制点库进行千万条数据坐标转换时的分表分区优化方案...
  8. 如何设置电脑自动锁屏_办公族如何设置自动关闭显示器,让显示屏锁屏,防止偷看你电脑。...
  9. linux设备驱动程序的编程思路,Linux设备驱动工程师之路——高级字符设备驱动程序...
  10. python的类和对象例题_Python类、类对象和实例对象、组合、绑定详细讲解