深度学习pytorch——nn.Module(持续更新)

作为一个初学者,发现构建一个简单的线性模型都能看到nn.Module的身影,初学者疑惑了,nn.Module到底是干什么的,如此形影不离,了解之后,很牛。

1、nn.Module是所有层的父类,比如Linear、BatchNorm2d、Conv2d、ReLU、Sigmoid、ConvTranposed、Dropout等等这些都是它的儿子(子类),你可以直接拿来使用。

2、nn.Module还支持一个nn.Module嵌套另一个nn.Module。

3、并且可以自动完成forward,你只需要nn.Sequential()这个容器就可以了,代码示例如下:

python 复制代码
class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()

        self.net = nn.Sequential(BasicNet(),
                                 nn.ReLU(),
                                 nn.Linear(3, 2))

    def forward(self, x):
        return self.net(x)

4、深度学习中参数可谓是量产,如果手动进行参数管理将会是一个庞大的工程,导师问你在干什么就不是在训练模型了,而是处理那些无处安放的参数,而使用nn.Module就提供的parameters就可以秒出结果,代码示例如下:

python 复制代码
    for name, t in net.named_parameters():
        print('parameters:', name, t.shape)
    # parameters: net.0.net.weight torch.Size([3, 4])
    # parameters: net.0.net.bias torch.Size([3])
    # parameters: net.2.weight torch.Size([2, 3])
    # parameters: net.2.bias torch.Size([2])

然后将参数直接传入优化器进行优化,代码示例如下:

python 复制代码
optimizer=optim.SGD(net.parameters(),lr=1e-3)

5、有很多的孩子,并且你还可以很简单的知道他孩子长什么样,我先介绍一下他的孩子们:

我们可以通过 net.named_children()了解他的亲孩子(children),也就是直系亲属,net.named_modules()了解他所有的孩子(modules),直系亲属外亲都算,代码示例如下:

python 复制代码
    for name, m in net.named_children():
        print('children:', name, m)
    # children: net Sequential(
    #     (0): BasicNet(
    #     (net): Linear(in_features=4, out_features=3, bias=True)
    # )
    # (1): ReLU()
    # (2): Linear(in_features=3, out_features=2, bias=True)
    # )

    for name, m in net.named_modules():
        print('modules:', name, m)

# modules:  Net(
#   (net): Sequential(
#     (0): BasicNet(
#       (net): Linear(in_features=4, out_features=3, bias=True)
#     )
#     (1): ReLU()
#     (2): Linear(in_features=3, out_features=2, bias=True)
#   )
# )
# modules: net Sequential(
#   (0): BasicNet(
#     (net): Linear(in_features=4, out_features=3, bias=True)
#   )
#   (1): ReLU()
#   (2): Linear(in_features=3, out_features=2, bias=True)
# )
# modules: net.0 BasicNet(
#   (net): Linear(in_features=4, out_features=3, bias=True)
# )
# modules: net.0.net Linear(in_features=4, out_features=3, bias=True)
# modules: net.1 ReLU()
# modules: net.2 Linear(in_features=3, out_features=2, bias=True)

6、可以非常方便的将网络运行在不同的设备,这里的设备是指cuda、gpu之类的,代码示例如下:

python 复制代码
device = torch.device('cuda')
net = Net()
net.to(device)

7、方便对模型进行保存和加载,一个模型一般需要训练好久,但是我们并没有如此连续的时间,我们可以将现在训练好的模型进行保存,下次加载继续训练,代码示例如下:

python 复制代码
# 加载
net.load_state_dict(torch.load('ckpt.mdl'))
# 保存
torch.save(net.state_dict(),'ckpt.mdl')

8、方便进行训练和测试状态的切换,代码示例如下:

python 复制代码
# 训练
net.train()
# 测试
net.eval()

9、可以实现自己构建的模型,代码示例如下:

python 复制代码
# 以下是一个展平的实现
class Flatten(nn.Module):

    def __init__(self):
        super(Flatten, self).__init__()

    def forward(self, input):
        return input.view(input.size(0), -1)  #[b,打平],保留b,其他的全部打平



class TestNet(nn.Module):

    def __init__(self):
        super(TestNet, self).__init__()

        self.net = nn.Sequential(nn.Conv2d(1, 16, stride=1, padding=1),
                                 nn.MaxPool2d(2, 2),
                                 Flatten(),
                                 nn.Linear(1*14*14, 10))

    def forward(self, x):
        return self.net(x)
python 复制代码
# 构建自己的线性层
class MyLinear(nn.Module):

    def __init__(self, inp, outp):
        super(MyLinear, self).__init__()
        # 这里使用nn.Parameter代表了可以回传给nn.model,进行更新,所以不用写requires_grad = True
        # 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

如果你还有什么更好的idea,欢迎分享!!!

相关推荐
草莓熊Lotso8 小时前
Linux 文件描述符与重定向实战:从原理到 minishell 实现
android·linux·运维·服务器·数据库·c++·人工智能
Coder_Boy_9 小时前
技术发展的核心规律是「加法打底,减法优化,重构平衡」
人工智能·spring boot·spring·重构
会飞的老朱11 小时前
医药集团数智化转型,智能综合管理平台激活集团管理新效能
大数据·人工智能·oa协同办公
聆风吟º13 小时前
CANN runtime 实战指南:异构计算场景中运行时组件的部署、调优与扩展技巧
人工智能·神经网络·cann·异构计算
Codebee15 小时前
能力中心 (Agent SkillCenter):开启AI技能管理新时代
人工智能
聆风吟º15 小时前
CANN runtime 全链路拆解:AI 异构计算运行时的任务管理与功能适配技术路径
人工智能·深度学习·神经网络·cann
uesowys15 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
AI_567815 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
User_芊芊君子15 小时前
CANN大模型推理加速引擎ascend-transformer-boost深度解析:毫秒级响应的Transformer优化方案
人工智能·深度学习·transformer
智驱力人工智能16 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算