深度学习--魔法类nn.Module
作用
pytorch 封装了一些基本的网络类,可以直接调用
好处:
- 可以直接调用现有的类
- 容器机制:self.net = nn.Sequential(
- 参数返回:list(net.parameters([0].shape #返回对应的参数的shape
list(net.named_parameters([0] #返回对应的参数
dict(net.named_parameters(.items(# 返回所有的参数 - 模型modules:children(这个没太懂)
- to(device:选择运行设备
- save and load :net.load_state_dict(torch.load('ckpt.mdl'
net.save(net.state_dict(,'ckpt.mdl' - train/test的行为切换,net.train( 训练 net.eval(测试
- 实现我们自己的类:self.w = nn.Parameter(torch.randn(outp,inp,requires_grad = True
import torch
import torch.nn as nn
#继承nn.Module来实现自己的模型
class MyLinear(nn.Module:
def __init__(self, inp, outp:
super(MyLinear,self.__init__(
# requires_grad = True
self.w = nn.Parameter(torch.randn(outp,inp
self.b = nn.Parameter(torch.randn(outp
def forward(self,x:
x = x @ self.w.t( +self.b
return x
图像增强操作
数据增强:低网络容量、regularization规范化、Data argumentation数据推论
data argumentation的手段:
-
Rotate:旋转
-
GAN:生成对抗网络