当前位置: 首页 > news >正文

机器学习 - save和load训练好的模型

如果已经训练好了一个模型,你就可以save和load这模型。

For saving and loading models in PyTorch, there are three main methods you should be aware of.

PyTorch methodWhat does it do?
torch.saveSaves a serialized object to disk using Python’s pickle utility. Models, tensors and various other Python objects like dictionaries can be saved using torch.save
torch.loadUses pickle’s unpickling features to deserialize and load pickled Python object files (like models, tensors or dictionaries) into memory. You can also set which device to load the object to (CPU, GPU etc)
torch.nn.Module.load_state_dictLoads a model’s parameter dictionary (model.state_dict()) using a saved state_dict() object

在 PyTorch 中,pickle 是一个用于序列化和反序列化Python对象的标准库模块。它可以将Python对象转换为字节流 (即序列化),并将字节流转换回Python对象 (即反序列化)。pickle模块在很多情况下都非常有用,特别是在保存和加载模型,保存训练中间状态等方面。
在深度学习中,经常需要保存训练好的模型或者训练过程中的中间结果,以便后续的使用或分析。PyTorch提高了方便的API来保存和加载模型,其中就包括了使用pickle模块进行对象的序列化和反序列化。


save model

import torch
from pathlib import Path 

# 1. Create models directory
MODEL_PATH = Path("models")
MODEL_PATH.mkdir(parents = True, exist_ok = True)

# 2. Create model save path
MODEL_NAME = "trained_model.pth"
MODEL_SAVE_PATH = MODEL_PATH / MODEL_NAME

# 3. Save the model state dict 
print(f"Saving model to: {MODEL_SAVE_PATH}")
torch.save(obj = model_0.state_dict(),
			f = MODEL_SAVE_PATH)

就能看到 trained_model.pth 文件下载到所属的文件夹位置。


Load the saved PyTorch model
You can load it in using torch.nn.Module.load_state_dict(torch.load(f)) where f is the filepath of the saved model state_dict().

Why call torch.load() inside torch.nn.Module.load_state_dict()?
Because you only saved the model’s state_dict() which is a dictionary of learned parameters and not the entire model, you first have to load the state_dict() with torch.load() and then pass that state_dict() to a new instance of the model (which is a subclass of nn.Module).

# Instantiate a new instance of the model 
loaded_model_0 = LinearRegressionModel()

# Load the state_dict of the saved model
loaded_model_0.load_state_dict(torch.load(f=MODEL_SAVE_PATH))

# 结果如下
<All keys matched successfully>

测试 loaded model。

# Put the loaded model into evaluation model 
loaded_model_0.eval() 

# 2. Use the inference mode context manager to make predictions
with torch.inference_mode():
  loaded_model_preds = loaded_model_0(X_test)

# Compare previous model predictions with loaded model predictions
print(y_preds == loaded_model_preds) 

# 结果如下
tensor([[True],
        [True],
        [True],
        [True],
        [True],
        [True],
        [True],
        [True],
        [True],
        [True]])


看到这了,点个赞呗~

相关文章:

  • PHP 读取嵌入式数据 SQLite3
  • 一个单生产-多消费模式下无锁方案(ygluu/卢益贵)
  • 数字乡村引领新风尚:科技赋能农村实现全面进步
  • 零基础机器学习(3)之机器学习的一般过程
  • GPT4.0
  • [flask]flask的路由
  • 35 跨域相关问题, 以及常见的解决方式
  • 【笔记】Hbase基础笔记
  • 刚进公司第一天-电脑环境搭建
  • 基于AT89C51单片机的智能交通灯设计
  • APS54083 大功率深度调光降压恒流驱动IC PWM 线性调光 车灯IC
  • 数据加载器
  • AI开源概览及工具使用
  • 在Sequence中缓存Niagara粒子轨道
  • CI/CD实战-jenkins部署 3
  • css的text-shadow详解
  • Python并发编程的三种方式:多线程(threading)、多进程(multiprocessing),以及基于协程的异步I/O(asyncio)
  • 02 React 组件使用
  • RAFT: Adapting Language Model to Domain Specific RAG
  • 钡铼技术R40路由器助力构建无人值守的智能化污水处理厂
  • https://app.hackthebox.com/machines/Inject
  • Spring —— Spring简单的读取和存储对象 Ⅱ
  • 渗透测试之冰蝎实战
  • Mybatis、TKMybatis对比
  • Microsoft Office 2019(2022年10月批量许可版)图文教程
  • 《谷粒商城基础篇》分布式基础环境搭建
  • 哈希表题目:砖墙
  • Vue 3.0 选项 生命周期钩子
  • 【车载嵌入式开发】AutoSar架构入门介绍篇
  • 【计算机视觉 | 目标检测】DETR风格的目标检测框架解读