当前位置: 首页 > news >正文

兰溪市网站建设公司班级优化大师

兰溪市网站建设公司,班级优化大师,做啥英文网站赚钱,做吃的教程网站ModuleList 与 ModuleDict1、ModuleList2、ModuleDict3、总结1、ModuleList 1)ModuleList 接收一个子模块的列表作为输入,然后也可以类似 List 那样进行 append 和 extend 操作: net nn.ModuleList([nn.Linear(784, 256), nn.ReLU()]) net.append(nn.…

ModuleList 与 ModuleDict

  • 1、ModuleList
  • 2、ModuleDict
  • 3、总结


1、ModuleList

1)ModuleList 接收一个子模块的列表作为输入,然后也可以类似 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)
# 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)
# )

\quad
2)nn.Sequentialnn.ModuleList 二者的区别:

  • nn.ModuleList 仅仅是一个储存各种模块的列表,这些模块之间没有联系(所以不用保证相邻层的输入输出维度匹配), 而 nn.Sequential 内的模块需要按照顺序排列,要保证相邻层的输入输出大小相匹配
  • nn.ModuleList 没有实现 forward 功能需要自己实现,所以上面执行 net(torch.zeros(1, 784)) 会报 NotImplementedError;而nn.Sequential 内部 forward 功能已经实现。

ModuleList 的出现只是让网络定义前向传播时更加灵活,见下面官网的例子:

class MyModule(nn.Module):def __init__(self):super(MyModule, self).__init__()self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])def forward(self, x):# ModuleList can act as an iterable, or be indexed using intsfor i, l in enumerate(self.linears):x = self.linears[i // 2](x) + l(x)return x

\quad
3)另外,nn.ModuleList 不同于一般的 Python 的 list,加入到 nn.ModuleList 里面的所有模块的参数会被自动添加到整个网络中,下面看一个例子对比一下。

import torch
import torch.nn as nnclass Module_ModuleList(nn.Module):def __init__(self):super(Module_ModuleList, self).__init__()self.linears = nn.ModuleList([nn.Linear(10, 10)])class Module_List(nn.Module):def __init__(self):super(Module_List, self).__init__()self.linears = [nn.Linear(10, 10)]net1 = Module_ModuleList()
net2 = Module_List()print(net1)
for p in net1.parameters():print(p.size())print('*'*20)
print(net2)
for p in net2.parameters():print(p)

输出

Module_ModuleList((linears): ModuleList((0): Linear(in_features=10, out_features=10, bias=True))
)
torch.Size([10, 10])
torch.Size([10])
********************
Module_List()

2、ModuleDict

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)
# net(torch.zeros(1, 784)) # 会报NotImplementedError# 输出:
# 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)
# )

(1)和 nn.ModuleList 一样,nn.ModuleDict 实例仅仅是存放了一些模块的字典,并没有定义 forward函数 需要自己定义。
(2)同样,nn.ModuleDict 也与 Python 的 Dict 有所不同,nn.ModuleDict 里的所有模块的参数会被自动添加到整个网络中。


3、总结

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

相关文章:

  • 现在学做网站赚钱吗东莞市优速网络科技有限公司
  • 宁津做网站公司宣传推广图片
  • 陕西的建设厅官方网站数据分析报告
  • 企业网站建设的定位互联网
  • 注册域名之后如何做网站优化清理大师
  • wordpress+在线播放推广seo网站
  • 丽水网站建设明恩玉杰网站开发框架
  • 如何设计网站中的上传功能搜索引擎技术基础
  • 余江区建设局网站百度搜索引擎优化的方法
  • 做网站用c 还是java万网域名注册教程
  • 青岛做网站那家好专业的网站优化公司排名
  • 网站如何做淘宝推广seo服务 收费
  • 学完js了可以做哪些网站营业推广的形式包括
  • 网站会员系统怎么做模版seo是指什么职位
  • 上海集团网站制作新闻 近期大事件
  • 商城网站验收标准seo关键词排名优化怎样收费
  • 睢宁做网站公司珠海百度关键字优化
  • 临安市住房和建设局网站伊春seo
  • 天津百度做网站多少钱游戏代理平台哪个好
  • b2b模式的网站google网站
  • 做优化网站哪个公司好十大营销策略
  • 软件商店app苏州网站关键词优化推广
  • wordpress添加日历首页优化公司
  • 日本可以自己做网站吗查询网站服务器
  • 做网站维护的人叫啥友情链接交换工具
  • 云南网站定制真正永久免费的建站系统有哪些
  • 温州做网站技术员沧州做网络推广的平台
  • wordpress media上海排名优化seobwyseo
  • 深圳网站建设科技有限公司注册一个网站
  • 网站设计改版seo关键词优化推广价格