Keras-保存和恢复模型

Keras-保存和恢复模型
转载自:
1,share的内容

code to create the model, and
the trained weights, or parameters, for the model
2,ways

There are different ways to save TensorFlow models—depending on the API you’re using

3,Checkpoint callback usage

3.1,以callback方式触发对checkpoint的在fit过程中的记录

checkpoint_path = "training_1/cp.ckpt"checkpoint_dir = os.path.dirname(checkpoint_path)

Create checkpoint callback

cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, save_weights_only=True,verbose=1)model = create_model()model.fit(train_images, train_labels,  epochs = 10, validation_data = (test_images,test_labels),callbacks = [cp_callback])  # pass callback to training

3.2,检查目录

! ls {checkpoint_dir}

3.3,找出最近的

latest=tf.train.latest_checkpoint(checkpoint_dir)

4,恢复至最近的checkpoint

model = create_model()model.load_weights(latest)#用于仅保存了权重时loss, acc = model.evaluate(test_images, test_labels)print("Restored model, accuracy: {:5.2f}%".format(100*acc))tf.train.latest_checkpoint(checkpoint_dir)

5,手动save和restore

Save the weights

model.save_weights(’./checkpoints/my_checkpoint’)

Restore the weights

model = create_model()
model.load_weights(’./checkpoints/my_checkpoint’)

loss,acc = model.evaluate(test_images, test_labels)
print(“Restored model, accuracy: {:5.2f}%”.format(100*acc))

6,保存和恢复整个模型

6.1,save

contains the weight values, the model’s configuration, and even the optimizer’s configuration (depends on set up). This allows you to checkpoint a model and resume training later—from the exact same state—without access to the original code

model = create_model()model.fit(train_images, train_labels, epochs=5)# Save entire model to a HDF5 file
model.save('my_model.h5')

6.2,恢复

new_model = keras.models.load_model('my_model.h5')
new_model.summary()

7,keras如何保存和恢复模型

7.1,创建模型

model = create_model()model.fit(train_images, train_labels, epochs=5)

7.2,保存模型

Keras saves models by inspecting the architecture. Currently, it is not able to save TensorFlow optimizers (from tf.train). When using those you will need to re-compile the model after loading, and you will lose the state of the optimizer.

saved_model_path = tf.contrib.saved_model.save_keras_model(model, "./saved_models")!ls -l saved_models

7.3,恢复模型

new_model = tf.contrib.saved_model.load_keras_model(saved_model_path)
new_model.summary()

7.4,编译模型(因为不保存模型的优化器)

The model has to be compiled before evaluating.

This step is not required if the saved model is only being deployed.

new_model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])

Evaluate the restored model.

loss, acc = new_model.evaluate(test_images, test_labels)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/481502.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

72名图灵奖获得者的成就

来源:图灵教育从“图灵机”到“图灵测试”,从破译德军的 Enigma 到自杀之谜,图灵一生都是传奇,关于图灵的故事我们不在这里赘述,感兴趣的读者请看文末推荐阅读。今天我们更想聊聊,计算机领域最高奖项 —— …

18-Gm-TransH:Group-Constrained Embedding of Multi-fold Relations in Knowledge Bases,嵌入,transH,n-ary

文章目录abstract1. introduction2 Related Work2.1 Binary Relation Embedding2.2 Multi-fold Relation Embedding3 Group-Constrained Embedding3.1 Framework3.2 Optimizing Method3.3 Proposed Model3.4 Complexity Analysis4.实验4.1 数据集4.2 Link Prediction4.3 Instan…

统计学权威盘点过去50年最重要的统计学思想

来源:量化研究方法作者:陈彩娴、Mr Bear编辑:青暮近日,图灵奖得主、“贝叶斯网络之父”Judea Pearl在Twitter上分享了一篇新论文“What are the most important statistical ideas of the past 50 years?”(过去50年中…

19-A Walk-based Model on Entity Graphs for Relation Extraction(句内多对,多关系,多元,2018ACL

文章目录abstract1.introduction2 Proposed Walk-based Model2.1 嵌入层2.2 Bidirectional LSTM Layer2.3 Edge Representation Layer2.4 Walk Aggregation Layer2.5 Classification Layer3.实验3.1数据集3.2 Experimental Settings5.结果6.相关工作6.总结Christopoulou, F., e…

CCCF精选 | 李德毅:机器如何像人一样认知——机器的生命观

如果上一代人工智能可以叫做计算机智能,硬核是算力、算法和数据的话,那么新一代人工智能应该是有感知、有认知、有行为、可交互、会学习、自成长的机器智能,其硬核是交互、学习和记忆,而量子认知机的落地应用,则可能是…

量子混沌:相互作用如何影响量子多体系统的局域化?

导语在量子系统中,相干性会打破单个粒子的遍历性,使之进入一种动态局域化状态。对于包含相互作用的量子多体系统,情况会是怎样呢?近日发表于 Nature Physics 的两项研究通过实验证明,相互作用会破坏量子多体系统的动态…

李宏毅2020深度学习-判别方法和生成方法

二分类 数据少的情况下,生成方法可能比判别方法好数据多时,判别方法获得的效果好于生成方法数据量少时 贝叶斯分类可能会脑补 如:类1:(1,1)1个类2:(0,1),&…

可能是全网最简明的量子纠缠科普

前两天有位朋友抱怨,说是看了很多学者关于量子纠缠的科普,但还是一头雾水,没有一个人真的讲明白的。我就上网搜了几个看。确实,大多数科普要么是光顾着讲爱因斯坦和波尔打嘴炮的历史了,不讲物理;要么讲着讲…

20-Joint entity and relation extraction based on a hybrid neural network(LSTM-ED+CNN),考虑长距离的实体标签之间的关

文章目录abstract1.introduction2.相关工作2.1. Named entity recognition2.2. Relation classification2.3 联合模型2.4. LSTM and CNN models On NLP3.模型3.1. Bidirectional LSTM encoding layer3.2. Named entity recognition (NER) module:LSTM decoder3.3. Relation cla…

牛津大学团队采用先进机器人技术,推动「人造肌腱」实际应用

你知道“肩袖撕裂”吗?它是肩关节炎疼痛的常见原因之一。作为肩袖撕裂损伤中最为常见的肌腱损伤,每年世界范围内的患者高达几千万人。该病痛给患者带来巨大疼痛,甚至导致肢体功能丧失,无法正常生活和工作,造成极大的家…

finetune与Bert

文章目录一:过拟合1.1 直接finetune1.2 layer finetune1.3ULMFiT2 Bert节省内存3 Bert蒸馏4.post train一:过拟合 1.1 直接finetune 容易过拟合 1.2 layer finetune 拷贝部分预训练参数,而其他随机初始化 两部分一同训练:提升…

《Nature》长期寻找,终于发现直接促进神经递质“弹药库”的蛋白质

来源:生物通俄勒冈健康与科学大学(Oregon Health & Science University)的科学家们发现了一种长期寻找的基因编码蛋白质,这种蛋白质能使大脑在神经元之间的间隙(称为突触)之间传递广泛的信号。科学家们发现了一种长期寻找的基因编码蛋白质&#xff0…

机器翻译

1 模型 1.1 模型 1.2 RNNBeam searchAttention 损失函数:交叉熵 预测:不使用解码器而是beam search #paddlepaddle from __future__ import print_function import os import six import numpy as np import paddle import paddle.fluid as flui…

姚能伟:以邻盛智能为例,如何在行业大脑进行创新实践

报告内容摘要随着技术发展,未来一定是智慧的时代。为此我们提除了感知世界,透视数据,洞察未知为理念的新型技术探索与实践。依托多年项目经验与行业专业积累,形成了一些行业深度的解决方案。在实践过程中形成了客观世界的泛感知和…

Science:已“死亡”的细菌仍能感知来自环境的信息

根据信号强度对细菌孢子进行颜色编码的显微镜图像:颜色越亮,信号越强细菌依靠其顽强的生命力,在包括人类在内的各种生命体和地球的各个角落繁衍生息。细菌之所以难以被杀死,是因为它们具有独特的生存策略,其中一种是在…

Berttransformer

1.transformer transformer self-attention 当前编码的词和整个句子所有词做attention,权重加在所有句子上获得当前的表示 encoder-decoder-attention 当前解码的单元和编码器的所有输出做attention,权重加在所有编码输出上,获得当前的表示…

GPT1-3(GPT3/few-shot,无需finetune)

GPT1–finetune 12层单向transformer预训练精调被bert比下去了 GPT2-元学习 输入:加上任务描述 “英翻法:This is life” 输出: “C’est la vie” 参数15亿在NLU:比不上bert也比不过其他大参数模型:如Turing-NLG GPT3-Langua…

一种基于人工智能的化学合成机器人,用于纳米材料的探索和优化

编辑 | 萝卜皮格拉斯哥大学(University of Glasgow)的研究人员提出了一种自主化学合成机器人,用于探索、发现和优化由实时光谱反馈、理论和机器学习算法驱动的纳米结构,这些算法控制反应条件并允许选择性地模板化反应。这种方法允…

千脑智能理论:开启创造机器智能的路线图 | 《千脑智能》

导语当人们谈论人工智能时,往往热衷于算法优化、模型迭代、算力提升,海量参数的复杂网络仿佛就是人工智能的发展方向。然而,这就是真正的智能吗?计算机科学家与神经科学家杰夫霍金斯在《千脑智能》中提出了一种关于大脑和智能的理…

spring基于Xml管理bean---Ioc依赖注入:对象类型属性赋值(2)----内部bean的引入(bean和bean之间的引入)、(3)级联方式注入

bean创建对象类型赋值方式 第一&#xff1a;外部bean的引入 第二&#xff1a;内部bean的引入 第三&#xff1a;级联属性赋值 文章目录 bean创建对象类型赋值方式对象类型内部bean赋值代码分析总结 对象类型属性级联方式的赋值扩展知识 对象类型内部bean赋值 代码分析 <b…