锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

六月份组队学习【深入浅出PyTorch】Task03打卡笔记

时间:2024-01-05 08:37:01 hs3一组常开10a继电器

这个吃瓜教程是Datawhale组织团队学习 。
学习资料由开源学习组织Datawhale提供。
开源贡献:李嘉琪、牛志康、刘洋、陈安东、陈玉立、刘兴、郭棉升、乔斌、匡俊伟
笔记的分来自网络检索。如果有侵权联系,可以删除
本研究的对象:
具有高数、线代、概率论的基础,具有一定的机器学习和深度学习基础,熟悉常见概念Python。
内容说明:PyTorch从基础知识到项目实战,理论与实践相结合。
学习周期:14天

教程链接:https://datawhalechina.github.io/thorough-pytorch/index.html
B站视频:BV1L44y1472Z
学习者手册:https://mp.weixin.qq.com/s/pwWg0w1DL2C1i_Hs3SZedg

Task03学习内容

  • 第五章 PyTorch模型定义
    • 5.1PyTorch模型定义的方式
      • Sequential
      • ModuleList
      • ModuleDict
  • 小结

第五章 PyTorch模型定义

5.1PyTorch模型定义的方式

  • Module 类是 torch.nn 模块中提供的模型结构类型 (nn.Module),它是所有神经网络模块的基类,我们可以继承它来定义我们想要的模型;
  • PyTorch模型定义应包括两个主要部分:每个部分的初始化(init);定义数据流(forward)

基于nn.Module,我们可以通过Sequential,ModuleList和ModuleDict三种定义方法PyTorch模型。

Sequential

对应模块为nn.Sequential()。

当模型的前向计算简单地串联到各层时, Sequential 模型可以用更简单的方式定义。它可以接收子模块的有序字典(OrderedDict) 或者一系列子模块作为参数逐一添加 Module 模型的前向计算是按添加的顺序逐个计算这些例子。
使用Sequential根据不同的层名,有两种方法可以定义模型:

  • 直接排列
import torch.nn as nn net = nn.Sequential(         nn.Linear(784, 256),         nn.ReLU(),         nn.Linear(256, 10),          ) print(net) 
Sequential(   (0): Linear(in_features=784, out_features=256, bias=True)   (1): ReLU()   (2): Linear(in_features=256, out_features=10, bias=True) ) 
  • 使用OrderedDict:

OrderedDict:是python中模块collections里面有一个子类OrderedDict,对字典对象中的元素进行排序。

import collections import torch.nn as nn net2 = nn.Sequential(collections.OrderedDict([ ('fc1', nn.Linear(784, 256)), ('relu1', nn.ReLU()), ('fc2', nn.Linear(256, 10)) ])) print(net2)
Sequential(
  (fc1): Linear(in_features=784, out_features=256, bias=True)
  (relu1): ReLU()
  (fc2): Linear(in_features=256, out_features=10, bias=True)
)

ModuleList

ModuleList 接收一个子模块(或层,需属于nn.Module类)的列表作为输入,然后也可以类似List那样进行append和extend操作。同时,子模块或层的权重也会自动添加到网络中来。

net = nn.ModuleList([nn.Linear(784, 256), nn.ReLU()])
net.append(nn.Linear(256, 10)) # # 类似List的append操作
print(net[-1])  # 类似List的索引访问
print(net)
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)
)

注意:nn.ModuleList 并没有定义一个网络,它只是将不同的模块储存在一起。 ModuleList中元素的先后顺序并不代表其在网络中的真实位置顺序,需要经过forward函数指定各个层的先后顺序后才算完成了模型的定义。具体实现时用for循环即可完成:

class model(nn.Module):
  def __init__(self, ...):
    super().__init__()
    self.modulelist =
  def forward(self, x):
    for layer in self.modulelist:
      x = layer(x)
    return x

ModuleDict

ModuleDict和ModuleList的作用类似,只是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(
  (act): ReLU()
  (linear): Linear(in_features=784, out_features=256, bias=True)
  (output): Linear(in_features=256, out_features=10, bias=True)
)

小结

这次的task3打卡我写的内容主要是针对我第五章中学到的些基础,我觉得重要的第一节内容,在群里看了大佬发言和纠错后,我用命令行来查看了Cuda支持的最高版本,在后续对第六章最后的实战训练完成后会在更新一次task3。

nvidia-smi

在这里插入图片描述

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章