博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【深度学习笔记】用torch.nn.ModuleList搭建神经网络
阅读量:2134 次
发布时间:2019-04-30

本文共 1343 字,大约阅读时间需要 4 分钟。

一、class torch.nn.ModuleList(modules=None)[source]

  • 将submodules保存在一个 list 中。
  • ModuleList可以像一般的Python list一样被索引。而且ModuleList中包含的modules已经被正确的注册,对所有的module method可见。
  • 可以向python的列表一样使用append函数

二、参数说明:

  • modules (list, optional) —— 将要被添加到MuduleList中的 modules 列表
  • 类型:列表list

三、例子

import torch.nn as nnclass 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 ints        for i, l in enumerate(self.linears):            x = self.linears[i // 2](x) + l(x)        return x
  • 输出
MyModule(  (linears): ModuleList(    (0): Linear(in_features=10, out_features=10, bias=True)    (1): Linear(in_features=10, out_features=10, bias=True)    (2): Linear(in_features=10, out_features=10, bias=True)    (3): Linear(in_features=10, out_features=10, bias=True)    (4): Linear(in_features=10, out_features=10, bias=True)    (5): Linear(in_features=10, out_features=10, bias=True)    (6): Linear(in_features=10, out_features=10, bias=True)    (7): Linear(in_features=10, out_features=10, bias=True)    (8): Linear(in_features=10, out_features=10, bias=True)    (9): Linear(in_features=10, out_features=10, bias=True)  ))

四、动态修改网络

  • 中间(涉及列表的访问方式)、末尾都行。
  • 还可以插入nn.Sequential对象。

转载地址:http://fbugf.baihongyu.com/

你可能感兴趣的文章
笔试题(一)—— java基础
查看>>
Redis学习笔记(三)—— 使用redis客户端连接windows和linux下的redis并解决无法连接redis的问题
查看>>
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>
Intellij IDEA使用(二)—— 在Intellij IDEA中配置JDK(SDK)
查看>>
Intellij IDEA使用(三)——在Intellij IDEA中配置Tomcat服务器
查看>>
Intellij IDEA使用(四)—— 使用Intellij IDEA创建静态的web(HTML)项目
查看>>
Intellij IDEA使用(五)—— Intellij IDEA在使用中的一些其他常用功能或常用配置收集
查看>>
Intellij IDEA使用(六)—— 使用Intellij IDEA创建Java项目并配置jar包
查看>>
Eclipse使用(十)—— 使用Eclipse创建简单的Maven Java项目
查看>>
Eclipse使用(十一)—— 使用Eclipse创建简单的Maven JavaWeb项目
查看>>
Intellij IDEA使用(十三)—— 在Intellij IDEA中配置Maven
查看>>
面试题 —— 关于main方法的十个面试题
查看>>
集成测试(一)—— 使用PHP页面请求Spring项目的Java接口数据
查看>>
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
Redis学习笔记(四)—— redis的常用命令和五大数据类型的简单使用
查看>>
Win10+VS2015编译libcurl
查看>>
Windows下使用jsoncpp
查看>>
Ubuntu下测试使用Nginx+uWsgi+Django
查看>>
Windows下编译x264
查看>>