动手学深度学习(四、深度学习计算--模型构造)

动手学深度学习(四、深度学习计算--模型构造)

一、模型构造

  • 可以通过继承Module类来构造模型。
  • SequentialModuleListModuleDict类都继承自Module类。
  • Sequential不同,ModuleListModuleDict并没有定义一个完整的网络,它们只是将不同的模块存放在一起,需要自己定义forward函数。
  • 虽然Sequential等类可以使模型构造更加简单,但直接继承Module类可以极大地拓展模型构造的灵活性。

1.继承Module类来构造模型

Module类是nn模块里提供的一个模型构造类,是所有神经网络模块的基类,我们可以继承它来定义我们想要的模型。定义的模型类重载了Module类的__init__函数和forward函数。它们分别用于创建模型参数和定义前向计算。前向计算也即正向传播。无须定义反向传播函数。系统将通过自动求梯度而自动生成反向传播所需的backward函数。

2.Module的子类

(1)Module类是一个通用的部件。PyTorch还实现了继承自Module的可以方便构建模型的类: 如SequentialModuleListModuleDict等等。

Sequential类可以接收一个子模块的有序字典(OrderedDict)或者一系列子模块作为参数来逐一添加Module的实例,而模型的前向计算就是将这些实例按添加的顺序逐一计算。

(2)ModuleList接收一个子模块的列表作为输入,然后也可以类似List那样进行append和extend操作

SequentialModuleList都可以进行列表化构造网络。ModuleList仅仅是一个储存各种模块的列表,这些模块之间没有联系也没有顺序(所以不用保证相邻层的输入输出维度匹配),而且没有实现forward功能需要自己实现;而Sequential内的模块需要按照顺序排列,要保证相邻层的输入输出大小相匹配,内部forward功能已经实现。ModuleList不同于一般的Python的list,加入到ModuleList里面的所有模块的参数会被自动添加到整个网络中。

net = nn.ModuleList([nn.Linear(784, 256), nn.ReLU()])
net.append(nn.Linear(256, 10))  #类似List的append操作
print(net[-1])
print(net)
# net(torch.zeros(1, 784)) # 会报NotImplementedError
Linear(in_features=256, out_features=10, bias=True)
ModuleList(
  (0): Linear(in_features=784, out_features=256, bias=True)
  (1): ReLU()
  (2): Linear(in_features=256, out_features=10, bias=True)
)

(3)ModuleDict接收一个子模块的字典作为输入, 然后也可以类似字典那样进行添加访问操作。和ModuleList一样,ModuleDict实例仅仅是存放了一些模块的字典,并没有定义forward函数需要自己定义。同样,ModuleDict也与Python的Dict有所不同,ModuleDict里的所有模块的参数会被自动添加到整个网络中。

net = nn.ModuleDict({
    'linear':nn.Linear(784, 256),
    'act':nn.ReLU(),
})
net['output'] = nn.Linear(256, 10)#添加
print(net['linear'])
print(net.output)
print(net)
Linear(in_features=784, out_features=256, bias=True)
Linear(in_features=256, out_features=10, bias=True)
ModuleDict(
  (linear): Linear(in_features=784, out_features=256, bias=True)
  (act): ReLU()
  (output): Linear(in_features=256, out_features=10, bias=True)
)

3.构造复杂的模型

虽然上面介绍的这些类可以使模型构造更加简单,且不需要定义forward函数,但直接继承Module类可以极大地拓展模型构造的灵活性。下面我们构造一个稍微复杂点的网络FancyMLP。在这个网络中,我们通过get_constant函数创建训练中不被迭代的参数,即常数参数。在前向计算中,除了使用创建的常数参数外,我们还使用Tensor的函数和Python的控制流,并多次调用相同的层。因为FancyMLPSequential类都是Module类的子类,所以我们可以嵌套调用它们。

class FancyMLP(nn.Module):
    def __init__(self, **kwargs):
        super(FancyMLP, self).__init__(**kwargs)
        #不可训练参数, 常数参数
        self.rand_weight = torch.rand((20, 20), requires_grad = False)
        self.linear = nn.Linear(20, 20)
    
    def forward(self, x):
        x = self.linear(x)
        x = nn.functional.relu(torch.mm(x, self.rand_weight.data) + 1)
        
        #复用全连接层,等价于两个全连接层共享参数
        x = self.linear(x)
        #控制流,调用item函数来返回标量进行比较
        while x.norm().item() > 1:
            x /= 2
        if x.norm().item() < 0.8:
            x *= 10
        return x.sum()
    
X = torch.rand(2, 20)
net = FancyMLP()
print(net)
print(net(X))
FancyMLP(
  (linear): Linear(in_features=20, out_features=20, bias=True)
)
tensor(2.7923, grad_fn=<SumBackward0>)
class NestMLP(nn.Module):
    def __init__(self, **kwargs):
        super(NestMLP, self).__init__(**kwargs)
        self.net = nn.Sequential(nn.Linear(40, 30), nn.ReLU())
        
    def forward(self, x):
        return self.net(x)

net = nn.Sequential(NestMLP(), nn.Linear(30, 20), FancyMLP())

X = torch.rand(2, 40)
print(net)
print(net(X))
Sequential(
  (0): NestMLP(
    (net): Sequential(
      (0): Linear(in_features=40, out_features=30, bias=True)
      (1): ReLU()
    )
  )
  (1): Linear(in_features=30, out_features=20, bias=True)
  (2): FancyMLP(
    (linear): Linear(in_features=20, out_features=20, bias=True)
  )
)
tensor(-1.6331, grad_fn=<SumBackward0>)

二、模型参数

  • 有多种方法来访问、初始化和共享模型参数。
  • 可以自定义初始化方法。

(1)访问模型参数

import torch
from torch import nn
from torch.nn import init

net = nn.Sequential(nn.Linear(4, 3), nn.ReLU(), nn.Linear(3, 1))#Pytorch进行了默认初始化

print(net)
X = torch.rand(2, 4)
Y = net(X).sum()

print(X)
print(Y)

print(type(net.named_parameters()))
for name, param in net.named_parameters():
    print(name, param.size())
    
for name, param in net[0].named_parameters():
    print(name, param.size(), type(param))

weight_0 = list(net[0].parameters())[0]
print(weight_0.data)
print(weight_0.grad)

Sequential(
  (0): Linear(in_features=4, out_features=3, bias=True)
  (1): ReLU()
  (2): Linear(in_features=3, out_features=1, bias=True)
)
tensor([[0.3539, 0.5159, 0.8734, 0.6483],
        [0.3209, 0.4507, 0.4653, 0.6654]])
tensor(1.0598, grad_fn=<SumBackward0>)
<class 'generator'>
0.weight torch.Size([3, 4])
0.bias torch.Size([3])
2.weight torch.Size([1, 3])
2.bias torch.Size([1])
weight torch.Size([3, 4]) <class 'torch.nn.parameter.Parameter'>
bias torch.Size([3]) <class 'torch.nn.parameter.Parameter'>
tensor([[-0.4747, -0.2484,  0.3790,  0.4386],
        [-0.1928,  0.2830,  0.0164, -0.4802],
        [ 0.1226, -0.3081,  0.4474,  0.0765]])
None
class MyModel(nn.Module):
    def __init__(self, **kwargs):
        super(MyModel, self).__init__(**kwargs)
        self.weight1 = nn.Parameter(torch.rand(20, 20))
        self.weight2 = torch.rand(20, 20)
    def forward(self, x):
        pass

n = MyModel()
for name, param in n.named_parameters():
    print(name)

输出:weight1

(2)初始化模型参数

import torch
from torch import nn
from torch.nn import init

net = nn.Sequential(nn.Linear(4, 3), nn.ReLU(), nn.Linear(3, 1))

# for name, param in net.named_parameters():
#     print(name, param)
    
for name, param in net.named_parameters():
    if 'weight' in name:
        init.normal_(param, mean = 0, std = 0.01)
        print(name, param.data)

for name, param in net.named_parameters():
    if 'bias' in name:
        init.constant_(param, val = 0)
        print(name, param.data)
0.weight tensor([[-0.0171,  0.0063,  0.0107,  0.0005],
        [-0.0144, -0.0241,  0.0160,  0.0111],
        [-0.0123, -0.0143,  0.0043,  0.0087]])
2.weight tensor([[-0.0002, -0.0102,  0.0031]])
0.bias tensor([0., 0., 0.])
2.bias tensor([0.])

权重 正态分布, 偏差 初始化为全0

(3)自定义初始化方法

pytorch自定义初始化方法

均匀分布    服从  U(a,b)

torch.nn.init.uniform_(tensor, a=0, b=1)

正态分布    服从  N(mean,std)

torch.nn.init.normal_(tensor, mean=0, std=1)

常数初始化    初始化整个矩阵为常数val

torch.nn.init.constant_(tensor, val)

def normal_(tensor, mean=0, std=1):
    with torch.no_grad():
        return tensor.normal_(mean, std)

def init_weight_(tensor):
    # torch.no_grad() 是一个上下文管理器,被该语句 wrap 起来的部分将不会track 梯度。
    with torch.no_grad():
        #从均匀分布中抽样值填充
        tensor.uniform_(-10, 10)
        #tensor *= (tensor.abs() >= 5).float()
        
for name, param in net.named_parameters():
    if 'weight' in name:
        init_weight_(param)
        print(name, param.data)
0.weight tensor([[ 9.6487,  5.7188, -3.0445,  2.3392],
        [ 3.6029,  5.0284, -1.8101,  1.1862],
        [-4.2232,  3.8367,  2.1078, -3.9020]])
2.weight tensor([[-4.7711,  7.9412,  6.3838]])
def init_weight_(tensor):
    # torch.no_grad() 是一个上下文管理器,被该语句 wrap 起来的部分将不会track 梯度。
    with torch.no_grad():
        #从均匀分布中抽样值填充
        tensor.uniform_(-10, 10)
        #tensor *= (tensor.abs() >= 5).float()
        
for name, param in net.named_parameters():
    if 'weight' in name:
        init_weight_(param)
        print(name, param.data)
        
for name, param in net.named_parameters():
    if 'bias' in name:
        param.data += 1
        print(name, param.data)
0.weight tensor([[-1.9599, -4.7342,  3.8280, -2.8934],
        [ 9.2476, -9.0658,  1.4095, -4.1868],
        [ 5.4428, -3.1554,  9.6436,  6.2261]])
2.weight tensor([[ 6.9514,  6.7034, -0.0466]])
0.bias tensor([1., 1., 1.])
2.bias tensor([1.])

(4)共享模型参数

linear = nn.Linear(1, 1, bias=False)
net = nn.Sequential(linear, linear) 
print(net)
for name, param in net.named_parameters():
    init.constant_(param, val=3)
    print(name, param.data)
print(id(net[0]) == id(net[1]))
print(id(net[0].weight) == id(net[1].weight))
输出:
Sequential(
  (0): Linear(in_features=1, out_features=1, bias=False)
  (1): Linear(in_features=1, out_features=1, bias=False)
)
0.weight tensor([[3.]])
True
True
x = torch.ones(1, 1)
y = net(x).sum()
print(y)
y.backward()
print(net[0].weight.grad) # 单次梯度是3,两次所以就是6
tensor(9., grad_fn=<SumBackward0>)
tensor([[6.]])

三、自定义层

  • 可以通过Module类自定义神经网络中的层,从而可以被重复调用。

1.不含模型参数的自定义层

2.含模型参数的自定义层

Parameter类其实是Tensor的子类,如果一个TensorParameter,那么它会自动被添加到模型的参数列表里。所以在自定义含模型参数的层时,我们应该将参数定义成Parameter,除了直接定义成Parameter类外,还可以使用ParameterListParameterDict分别定义参数的列表和字典。

ParameterList接收一个Parameter实例的列表作为输入然后得到一个参数列表,使用的时候可以用索引来访问某个参数,另外也可以使用appendextend在列表后面新增参数。

ParameterDict接收一个Parameter实例的字典作为输入然后得到一个参数字典,然后可以按照字典的规则使用了。例如使用update()新增参数,使用keys()返回所有键值,使用items()返回所有键值对等等

我们也可以使用自定义层构造模型。它和PyTorch的其他层在使用上很类似。

四、读取和存储

  • 通过save函数和load函数可以很方便地读写Tensor
  • 通过save函数和load_state_dict函数可以很方便地读写模型的参数。

处理数据, 构建、训练、测试深度学习模型。实际中需要将训练好的模型部署到不同设备上, 可以将内存中训练好的模型参数存储在硬盘上供后续使用

1.读写Tensor

使用save函数和load函数分别存储和读取Tensorsave使用Python的pickle实用程序将对象进行序列化,然后将序列化的对象保存到disk,使用save可以保存各种对象,包括模型、张量和字典等。而load使用pickle unpickle工具将pickle的对象文件反序列化为内存。

import torch
from torch import nn

x = torch.ones(3)
torch.save(x, 'x.pt')

x2 = torch.load('x.pt')
print(x2)

y = torch.zeros(4)
torch.save([x, y], 'xy.pt')
xy_list = torch.load('xy.pt')
print(xy_list)

torch.save({'x':x, 'y':y}, 'xy_dict.pt')
xy = torch.load('xy_dict.pt')
print(xy)

后缀pt为pytorch保存模型的文件, 可以存储变量、Tensor列表、Tensor字典。

2.读写模型

state_dict

#Module的可学习参数(即权重和偏差),模块模型包含在参数中(通过model.parameters()访问)
#state_dict是一个从参数名称隐射到参数Tesnor的字典对象。
#只有具有可学习参数的层(卷积层、线性层等)才有state_dict中的条目。优化器(optim)也有一个state_dict,其中包含关于优化器状态以及所使用的超参数的信息。
 

class MLP(nn.Module):
    def __init__(self):
        super(MLP, self).__init__()
        self.hidden = nn.Linear(3, 2)
        self.act = nn.ReLU()
        self.output = nn.Linear(2, 1)
    
    def forward(self, x):
        a = self.act(self.hidden(x))
        return self.output(a)
    
net = MLP()
net.state_dict()

optimizer = torch.optim.SGD(net.parameters(), lr = 0.001, momentum = 0.9)
optimizer.state_dict()
输出:
OrderedDict([('hidden.weight',
              tensor([[-0.0924,  0.5103,  0.5446],
                      [ 0.3580,  0.3385, -0.5413]])),
             ('hidden.bias', tensor([0.1804, 0.4146])),
             ('output.weight', tensor([[-0.2419, -0.0634]])),
             ('output.bias', tensor([0.6661]))])
{'state': {},
 'param_groups': [{'lr': 0.001,
   'momentum': 0.9,
   'dampening': 0,
   'weight_decay': 0,
   'nesterov': False,
   'params': [0, 1, 2, 3]}]}

保存和加载模型

PyTorch中保存和加载训练模型有两种常见的方法:

  1. 仅保存和加载模型参数(state_dict);
  2. 保存和加载整个模型。

1.仅保存和加载模型参数(state_dict)

保存:

torch.save(model.state_dict(), PATH) # 推荐的文件后缀名是pt或pth

加载:

model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH))

2.保存和加载整个模型

保存:

torch.save(model, PATH)

加载:

model = torch.load(PATH)
class MLP(nn.Module):
    def __init__(self):
        super(MLP, self).__init__()
        self.hidden = nn.Linear(3, 2)
        self.act = nn.ReLU()
        self.output = nn.Linear(2, 1)
    
    def forward(self, x):
        a = self.act(self.hidden(x))
        return self.output(a)
    
net = MLP()

X = torch.randn(2, 3)
Y = net(X)

PATH = "./net.pt"
torch.save(net.state_dict(), PATH)

net2 = MLP()
net2.load_state_dict(torch.load(PATH))
Y2 = net2(X)
print(Y2 == Y)
tensor([[True],
        [True]])

注:还有一些其他使用场景,例如GPU与CPU之间的模型保存与读取、使用多块GPU的模型的存储等等

五、GPU计算

  • PyTorch可以指定用来存储和计算的设备,如使用内存的CPU或者使用显存的GPU。在默认情况下,PyTorch会将数据创建在内存,然后利用CPU来计算。
  • PyTorch要求计算的所有输入数据都在内存或同一块显卡的显存上。

显卡信息查看:nvidia-smi

1.计算设备

PyTorch可以指定用来存储和计算的设备,如使用内存的CPU或者使用显存的GPU。默认情况下,PyTorch会将数据创建在内存,然后利用CPU来计算。

#查看GPU是否可用
torch.cuda.is_available() # 输出 True
#查看GPU数量
torch.cuda.device_count() # 输出 1
#查看当前GPU索引号,索引号从0开始:
torch.cuda.current_device() # 输出 0
#根据索引号查看GPU名字:
torch.cuda.get_device_name(0) # 输出 'GeForce GTX 2080 Ti'

2.Tensor的GPU计算

默认情况下,Tensor会被存在内存上。因此,之前我们每次打印Tensor的时候看不到GPU相关标识。使用.cuda()可以将CPU上的Tensor转换(复制)到GPU上。如果有多块GPU,我们用.cuda(i)来表示第 i 块GPU及相应的显存(i从0开始)且cuda(0)cuda()等价。我们可以通过Tensordevice属性来查看该Tensor所在的设备。我们可以直接在创建的时候就指定设备。

需要注意的是,存储在不同位置中的数据是不可以直接进行计算的。即存放在CPU上的数据不可以直接与存放在GPU上的数据进行运算,位于不同GPU上的数据也是不能直接进行计算的。

x = torch.tensor([1, 2, 3])
x 
# tensor([1, 2, 3])
x = x.cuda(0)
x
# tensor([1, 2, 3], device='cuda:0')
x.device
# device(type='cuda', index=0)
#创建的时候就指定设备
device = torch.device('cuda:3' if torch.cuda.is_available() else 'cpu')
x = torch.tensor([1, 2, 3], device=device)
# or
x = torch.tensor([1, 2, 3]).to(device)
x
# tensor([1, 2, 3], device='cuda:3')
y = x**2
y
# tensor([1, 4, 9], device='cuda:0')

3.模型的GPU计算

Tensor类似,PyTorch模型也可以通过.cuda转换到GPU上。我们可以通过检查模型的参数的device属性来查看存放模型的设备。

net = nn.Linear(3, 1)
list(net.parameters())[0].device
#输出: device(type='cpu')
#模型再CPU上,将其转换到GPU上
net.cuda()
list(net.parameters())[0].device
#输出:device(type='cuda', index=0)

同样的,我么需要保证模型输入的Tensor和模型都在同一设备上,否则会报错。

x = torch.rand(2,3).cuda()
net(x)
# 输出:
tensor([[-0.5800],
        [-0.2995]], device='cuda:0', grad_fn=<ThAddmmBackward>)

猜你喜欢

转载自blog.csdn.net/jiangchao98/article/details/115185150