之前我们使用nn.Sequential()都是直接写死的,就如下所示:

# Example of using Sequential
model = nn.Sequential(
nn.Conv2d(,,),
nn.ReLU(),
nn.Conv2d(,,),
nn.ReLU()
) # Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(,,)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(,,)),
('relu2', nn.ReLU())
]))

那如果我们想要根据条件一点点添加进去,那就可以使用其的add_module方法

torch.nn.Module.add_module

add_module(name, module)

添加子模块到当前模块中

该添加子模块能够使用给定的名字name来访问

参数:

  • name (string):子模块的名字。该添加子模块能够使用给定的名字name来从该模块中被访问
  • module (Module) :添加到该模块中的子模块

例子:

class Encoder(nn.Module):  #输入图片的大小isize、噪声的维度nz=、输入图片的通道nc=、ndf=、
def __init__(self,isize,nz,nc,ndf,ngpu,n_exter_layers=,add_final_conv=True):
super(Encoder,self).__init__()
self.ngpu=ngpu
# 必须为16倍数
assert isize % ==,"isize has to be a multiple of 16" main=nn.Sequential()
# 图片的高宽缩小一倍
main.add_module('initial-conv-{0}-{1}'.format(nc,ndf),nn.Conv2d(nc,ndf,,,,bias=False))
main.add_module('initial-relu-{0}'.format(ndf),nn.LeakyReLU(0.2,inplace=True))
csize,cndf=isize/,ndf for t in range(n_exter_layers): #在这里面特征宽高不变,通道数也不变
main.add_module('extra-layers-{0}-{1}-conv'.format(t,cndf),nn.Conv2d(cndf,cndf,,,,bias=False))
main.add_module('extra-layers-{0}-{1}-batchnorm'.format(t,cndf),nn.BatchNorm2d(cndf))
main.add_module('extra-layers-{0}-{1}-relu'.format(t,cndf),nn.LeakyReLU(0.2,inplace=True)) # 在特征高宽仍大于4时,就添加缩小一倍高宽,通道增加一倍的卷积块
while csize>:
in_feat = cndf out_feat = cndf * main.add_module('pyramid-{0}-{1}-conv'.format(in_feat, out_feat),nn.Conv2d(in_feat, out_feat, , , , bias=False)) main.add_module('pyramid-{0}-batchnorm'.format(out_feat),nn.BatchNorm2d(out_feat)) main.add_module('pyramid-{0}-relu'.format(out_feat),nn.LeakyReLU(0.2, inplace=True)) cndf = cndf * csize = csize / # 最后一层卷积,将4*4变为1*,得到nz = 100的噪声
if add_final_conv: main.add_module('final-{0}-{1}-conv'.format(cndf, ),nn.Conv2d(cndf, nz, , , , bias=False))
self.main=main def forward(self,input):
if self.ngpu>:
output=nn.parallel.data_parallel(self.main,input,range(self.ngpu)) #在多个gpu上运行模型,并行计算
else:
output=self.main(input) return output #如果输入的大小是3××,最后的输出是100××.

pytorch nn.Sequential()动态添加方法的更多相关文章

  1. ios的runtime为什么可以动态添加方法

    一句话概括 每个instance都有一个isa,这个isa,里面含有所有的方法列表,ios提供库函数增加,修改,即实现了动态添加方法

  2. 使用runtime给类动态添加方法并调用 - class_addMethod

    上手开发 iOS 一段时间后,我发现并不能只着眼于完成需求,利用闲暇之余多研究其他的开发技巧,才能在有限时间内提升自己水平.当然,“其他开发技巧”这个命题对于任何一个开发领域都感觉不找边际,而对于我来 ...

  3. iOS运行时使用(动态添加方法)

    1 举例  我们实现一个Person类 然后Person 其实是没得对象方法eat:的 下面调用person的eat方法 程序是会奔溃的 那么需要借助运行时动态的添加方法 Person *p = [[ ...

  4. 快速上手Runtime(四)之动态添加方法

    如果一个类方法非常多,加载类到内存的时候也比较耗费资源,可以使用动态给某个类,添加方法解决.做到优化内存,节省资源的效果. // // Person.m // ResolveInstanceMetho ...

  5. ios开发runtime学习三:动态添加方法(实际应用少,面试)

    #import "ViewController.h" #import "Person.h" /* 1: Runtime(动态添加方法):OC都是懒加载机制,只要 ...

  6. python 面向对象六 动态添加方法 __slots__限制动态添加方法

    一.动态添加属性 >>> class Student(object): pass >>> st = Student() >>> st.name = ...

  7. 给python类动态添加方法(method)

    群里有人问如何做到 def foo(): pass class Bar(object): pass Bar.set_instance_method(foo) b = Bar() b.foo() 这个其 ...

  8. python 类对象和实例对象动态添加方法

    class Person(): def __init__(self, name): self.name = name def print_name(self): print(self.name) p ...

  9. Runtime 方法替换 和 动态添加实例方法 结合使用

    前言: 方法替换,可以替换任意外部类的方法,而动态添加方法只能实现在被添加类创建的对象里,但是将方法替换和动态添加方法结合使用,可以实现,对任意外部类动态添加需要的方法,这个方法可以是类方法也可以是实 ...

随机推荐

  1. java基础(13)---集合框架

    一.集合框架 Java的集合类是一些非常实用的工具类,主要用于存储和装载数据 (包括对象),因此,Java的集合类也被成为容器.在Java中,所有的集合类都位于java.util包下,这些集合类主要是 ...

  2. golang log 使用

    原文:https://www.jianshu.com/p/d634316a9487 --------------------------------------------- 在我们开发程序后,如果有 ...

  3. used to do 与be used to doing /n.

    1.used to do:表示过去的习惯性动作,过去如此,现在不再这样了.常译作“过去常常”.(过去时+动词不定式) He used to play basketball when he was yo ...

  4. Linux https认证原理

    HTTPS在传输的过程中会涉及到三个密钥:服务器端的公钥和私钥,用来进行非对称加密客户端生成的随机密钥,用来进行对称加密一个HTTPS请求实际上包含了两次HTTP传输,可以细分为8步.1.客户端向服务 ...

  5. QGraphicsView,QGraphicsScene,QGraphicsItem

    参考:Qt4 开发实践第八章 图形视图QGraphicsView #ifndef DRIVEDGRAPH_H #define DRIVEDGRAPH_H #include <QObject> ...

  6. ipkg-nas

    http://pkg.entware.net/binaries/x86-64/ https://forum.synology.com/enu/viewtopic.php?t=95346 http:// ...

  7. CODE FESTIVAL 2016 qual A题解

    传送门 不知道为什么\(AGC\)系列的题里突然多了这些--那就做吧-- \(A\) 什么玩意儿-- upd:因为没看到最后要加换行居然没有\(1A\)好气哦-- const int N=15; ch ...

  8. WSL2(Ubuntu)安装Postgres

    原文链接:https://www.xu.ci/2019/12/wsl2ubuntupostgres.html 原文作者:博客园--曲高终和寡 *******************如果你看到这一行,说 ...

  9. UDF-C_UDMI【转载】

    UDF定义变量的输出 使用宏: C_UDMI( c, thread, index)       自变量类型:cell_t c       Thread *thread       int index ...

  10. 实验五 遇到的问题:openssl: error while loading shared libraries: libssl.so.1.1

    遇到的问题 命令行:linux@ubuntu64-vm:~/exp/exp5$ openssl enc -aes-128-cbc -in test_aes.txt -out out.txt -pass ...