From 170cd45bb654980a654bcfeda5e795d03b66cb71 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Sun, 9 Jun 2024 08:32:53 +0800 Subject: [PATCH] 0.1.109 --- PyPI/setup.cfg | 2 +- PyPI/src/guan.egg-info/PKG-INFO | 2 +- PyPI/src/guan/machine_learning.py | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/PyPI/setup.cfg b/PyPI/setup.cfg index 556ff54..f2483d3 100644 --- a/PyPI/setup.cfg +++ b/PyPI/setup.cfg @@ -1,7 +1,7 @@ [metadata] # replace with your username: name = guan -version = 0.1.108 +version = 0.1.109 author = guanjihuan author_email = guanjihuan@163.com description = An open source python package diff --git a/PyPI/src/guan.egg-info/PKG-INFO b/PyPI/src/guan.egg-info/PKG-INFO index 6f8b933..aafa889 100644 --- a/PyPI/src/guan.egg-info/PKG-INFO +++ b/PyPI/src/guan.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: guan -Version: 0.1.108 +Version: 0.1.109 Summary: An open source python package Home-page: https://py.guanjihuan.com Author: guanjihuan diff --git a/PyPI/src/guan/machine_learning.py b/PyPI/src/guan/machine_learning.py index 455f084..b70c380 100644 --- a/PyPI/src/guan/machine_learning.py +++ b/PyPI/src/guan/machine_learning.py @@ -254,6 +254,12 @@ def save_model(model, filename='./model.pth'): import torch torch.save(model, filename) +# 使用TorchScript形式保存模型到文件(TorchScript允许模型在不依赖Python解释器的情况下运行,适合在生产环境中部署) +def save_model_with_torch_jit_script(model, filename='model_scripted_with_torch_jit.pth'): + import torch + scripted_model = torch.jit.script(model) + scripted_model.save(filename) + # 以字典的形式保存模型的所有信息到文件(保存时需要模型的类可访问,此外还要输入模型的实例化函数) def save_model_with_all_information(model, model_class, model_instantiation, note='', filename='./model_with_all_information.pth'): import torch @@ -281,6 +287,12 @@ def load_model(filename='./model.pth'): model = torch.load(filename) return model +# 加载TorchScript形式的模型 +def load_model_with_torch_jit_script(filename='model_scripted_with_torch_jit.pth'): + import torch + scripted_model = torch.jit.load(filename) + return scripted_model + # 加载包含所有信息的模型(包含了模型的类和实例化函数等,返回的是模型对象) def load_model_with_all_information(filename='./model_with_all_information.pth', note_print=0): import torch