diff --git a/PyPI/setup.cfg b/PyPI/setup.cfg index 5074404..b5baba6 100644 --- a/PyPI/setup.cfg +++ b/PyPI/setup.cfg @@ -1,7 +1,7 @@ [metadata] # replace with your username: name = guan -version = 0.1.58 +version = 0.1.59 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 b0c59af..431308b 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.58 +Version: 0.1.59 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 d16cc16..291cd8d 100644 --- a/PyPI/src/guan/machine_learning.py +++ b/PyPI/src/guan/machine_learning.py @@ -1,11 +1,10 @@ # Module: machine_learning import guan -# 全连接神经网络模型(包含一个隐藏层) +# 全连接神经网络模型(包含一个隐藏层)(模型的类定义成全局的) @guan.function_decorator def fully_connected_neural_network_with_one_hidden_layer(input_size=1, hidden_size=10, output_size=1, activation='relu'): import torch - # 如果在函数中定义模型类,尽量定义成全局的,这样可以防止在保存完整模型到文件时,无法访问函数中的模型类。 global model_class_of_fully_connected_neural_network_with_one_hidden_layer class model_class_of_fully_connected_neural_network_with_one_hidden_layer(torch.nn.Module): def __init__(self): @@ -28,7 +27,7 @@ def fully_connected_neural_network_with_one_hidden_layer(input_size=1, hidden_si model = model_class_of_fully_connected_neural_network_with_one_hidden_layer() return model -# 全连接神经网络模型(包含两个隐藏层) +# 全连接神经网络模型(包含两个隐藏层)(模型的类定义成全局的) @guan.function_decorator def fully_connected_neural_network_with_two_hidden_layers(input_size=1, hidden_size_1=10, hidden_size_2=10, output_size=1, activation_1='relu', activation_2='relu'): import torch @@ -67,7 +66,7 @@ def fully_connected_neural_network_with_two_hidden_layers(input_size=1, hidden_s model = model_class_of_fully_connected_neural_network_with_two_hidden_layers() return model -# 全连接神经网络模型(包含三个隐藏层) +# 全连接神经网络模型(包含三个隐藏层)(模型的类定义成全局的) @guan.function_decorator def fully_connected_neural_network_with_three_hidden_layers(input_size=1, hidden_size_1=10, hidden_size_2=10, hidden_size_3=10, output_size=1, activation_1='relu', activation_2='relu', activation_3='relu'): import torch @@ -145,28 +144,28 @@ def train_model(model, x_data, y_data, optimizer='Adam', learning_rate=0.001, cr print(epoch) return model, losses -# 保存完整模型到文件 -@guan.function_decorator -def save_model(model, filename='./model.pth'): - import torch - torch.save(model, filename) - # 保存模型参数到文件 @guan.function_decorator def save_model_parameters(model, filename='./model_parameters.pth'): import torch torch.save(model.state_dict(), filename) -# 加载完整模型 +# 保存完整模型到文件(保存时需要模型的类可访问) @guan.function_decorator -def load_model(filename='./model.pth'): +def save_model(model, filename='./model.pth'): import torch - model = torch.load(filename) - return model + torch.save(model, filename) -# 加载模型参数(需要输入模型) +# 加载模型参数(需要输入模型,加载后,原输入的模型参数也会改变) @guan.function_decorator def load_model_parameters(model, filename='./model_parameters.pth'): import torch model.load_state_dict(torch.load(filename)) + return model + +# 加载完整模型(不需要输入模型,但加载时需要原定义的模型的类可访问) +@guan.function_decorator +def load_model(filename='./model.pth'): + import torch + model = torch.load(filename) return model \ No newline at end of file