It is really useful to save and reload the model and its parameters during or after training in deep learning.

Pytorch provides two methods to do so.

1. Only restore the parameters (recommended)

torch.save(the_model.state_dict(), PATH)    # save parameters to PATH

the_model = TheModelClass(*args, **kwargs)    # declare the_model as a object of TheModelClass
the_model.load_state_dict(torch.load(PATH)) # load parameters from PATH

2. Save all structure and parameters

torch.save(the_model, PATH)

the_model = torch.load(PATH)

3. Get parameters of certain layer

params=model.state_dict()
for k,v in params.items():
print(k) # print the variable names in networks
print(params['conv1.weight']) #print conv1's weight
print(params['conv1.bias']) #print conv1's bias

  

reference:http://www.pytorchtutorial.com/pytorch-note5-save-and-restore-models/

  

Pytorch model saving and loading 模型保存和读取的更多相关文章

  1. Sklearn,TensorFlow,keras模型保存与读取

    一.sklearn模型保存与读取 1.保存 from sklearn.externals import joblib from sklearn import svm X = [[0, 0], [1, ...

  2. [MISS静IOS开发原创文摘]-AppDelegate存储全局变量和 NSUserDefaults standardUserDefaults 通过模型保存和读取数据,存储自定义的对象

    由于app开发的需求,需要从api接口获得json格式数据并保存临时的 app的主题颜色 和 相关url 方案有很多种: 1, 通过AppDelegate保存为全局变量,再获取 2,使用NSUSerD ...

  3. [PyTorch 学习笔记] 7.1 模型保存与加载

    本章代码: https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson7/model_save.py https://githu ...

  4. 10 Tensorflow模型保存与读取

    我们的模型训练出来想给别人用,或者是我今天训练不完,明天想接着训练,怎么办?这就需要模型的保存与读取.看代码: import tensorflow as tf import numpy as np i ...

  5. 从头学pytorch(十二):模型保存和加载

    模型读取和存储 总结下来,就是几个函数 torch.load()/torch.save() 通过python的pickle完成序列化与反序列化.完成内存<-->磁盘转换. Module.s ...

  6. keras模型保存和权重保存

    模型保存和读取(包括权重): model.save('./model.h5') from keras import models model = models.load_model(./model.h ...

  7. 【NLP学习其五】模型保存与载入的注意事项(记问题No module named 'model')

    这是一次由于路径问题(找不到模型)引出模型保存问题的记录 最近,我试着把使用GPU训练完成的模型部署至预发布环境时出现了一个错误,以下是log节选 unpickler.load() ModuleNot ...

  8. Keras入门(二)模型的保存、读取及加载

    本文将会介绍如何利用Keras来实现模型的保存.读取以及加载.   本文使用的模型为解决IRIS数据集的多分类问题而设计的深度神经网络(DNN)模型,模型的结构示意图如下: 具体的模型参数可以参考文章 ...

  9. keras中的模型保存和加载

    tensorflow中的模型常常是protobuf格式,这种格式既可以是二进制也可以是文本.keras模型保存和加载与tensorflow不同,keras中的模型保存和加载往往是保存成hdf5格式. ...

随机推荐

  1. Linux创建连接命令 ln -s创建软连接

    ln -s 是linux中一个非常重要命令,一定要熟悉.它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s, 具体用法是:ln -s 源文件 目标文件. 当 我们需要在 ...

  2. ubuntu16 安装opencv3.4.2

    下载好opencv3.4.2.zip 执行命令: unzip opencv3.4.2.zip 进入解压后的文件夹: cd opencv3.4.2/ 创建编译路径: mkdir release 进入新创 ...

  3. MySQL - Schema和Database的区别

    问题来源 在pycharm发现Create new schema的效果和新建数据库一样,所以产生这个问题 参考 https://stackoverflow.com/questions/11618277 ...

  4. 使用 vant 的 v-lazy 实现图片 vue 在移动端的懒加载

    官方文档:https://youzan.github.io/vant/#/zh-CN/lazyload 引入 Lazyload 是 Vue 指令,使用前需要对指令进行注册 import Vue fro ...

  5. 吴裕雄--天生自然TensorFlow2教程:函数优化实战

    import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...

  6. POJ1797 Heavy Transportation (堆优化的Dijkstra变形)

    Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...

  7. java里判断字符串是否为数字类型的方法

    String type = "数字类型";if(StringUtils.isNotBlank(value)){ //区分正负数 if(value.startsWith(" ...

  8. JAVA 开学测试

    package StudentScore; public class ScoreInformation { String stunumber; //学号 String name; //姓名 doubl ...

  9. 【原】Jenkins pipeline中资料总结

    docker-compose 快速部署持续集成测试环境 Gitlab+Harbor+Jenkins pipeline 实现 tag run docker Images https://www.cnbl ...

  10. js判断ie和edge是否安装Adobe Reader PDF阅读器

    ie浏览器和edge浏览器,必须用Adobe Reader PDF阅读器才可以打开pdf文件,其他现代浏览器自带pdf阅读器,无需安装. 判断ie或者edge如果安装了,就浏览pdf文件:如果没安装就 ...