将模型各层按照顺序传入容器如nn.Sequential即可快速创建模型。torch.nn还内置一些常用的模型如LSTM等,可以直接使用。
import torch.nn as nn
model = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10)
)
print(model)
输出结果:
Sequential(
(0): Linear(in_features=10, out_features=10, bias=True)
(1): ReLU()
(2): Linear(in_features=10, out_features=10, bias=True)
)
在__init__方法中定义模型各层,除了基本的网络层外,还可使用各种容器如前述的nn.Sequential。在forward方法中实现前向传播。
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
super().__init__()
self.layer1 = nn.Linear(10, 10)
self.layer2 = nn.Linear(10, 10)
self.layer3 = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10)
)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
return x
model = Model()
print(model)
输出结果:
Model(
(layer1): Linear(in_features=10, out_features=10, bias=True)
(layer2): Linear(in_features=10, out_features=10, bias=True)
(layer3): Sequential(
(0): Linear(in_features=10, out_features=10, bias=True)
(1): ReLU()
(2): Linear(in_features=10, out_features=10, bias=True)
)
)
方法与将数据转移到GPU类似,都有两种方法:
import torch
import torch.nn as nn
# 创建模型实例
model = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10)
)
# 将模型移动到GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
# 也可以
model = model.cuda()
模型保存和加载使用的python内置的pickle模块。
import torch
import torch.nn as nn
# 创建模型实例
model1 = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10)
)
# 保存和加载参数
torch.save(model1.state_dict(), '../model/model_params.pkl')
model1.load_state_dict(torch.load('../model/model_params.pkl'))
import torch
import torch.nn as nn
# 创建模型实例
model1 = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10)
)
# 保存和加载模型和参数
torch.save(model1, '../model/model.pt')
model2 = torch.load('../model/model.pt')
print(model2)