当前位置:网站首页>用pytorch里的children方法自定义网络
用pytorch里的children方法自定义网络
2022-07-31 05:16:00 【王大队长】
假设我们要自定义一个网络,这个网络由resnet-18的所有除了最后一层和倒数第二层,最后一层用我们的自定义层。这时我们可以用到children方法。
children()返回网络模型里的组成元素,且children()返回的是最外层的元素
举个例子:
m = nn.Sequential(nn.Linear(2,2),
nn.ReLU(),
nn.Sequential(nn.Sigmoid(), nn.ReLU()))m.children()返回的是(一个list):
[Linear(in_features=2, out_features=2), ReLU(), Sequential(
(0): Sigmoid()
(1): ReLU()
)]此时我们对children方法应该有了一些了解,那么回到我们刚才的问题:
pretrained_net = torchvision.models.resnet18(pretrained=True)
net = nn.Sequential(*list(pretrained_net.children())[:-2])通过上面两行代码就可以获得resnet-18的除最后两层的所有层。
然后我们通过add_module方法添加自己想要的层:
num_classes = 21
net.add_module('final_conv', nn.Conv2d(512, num_classes, kernel_size=1))
net.add_module('transpose_conv', nn.ConvTranspose2d(num_classes, num_classes,
kernel_size=64, padding=16, stride=32))这样,我们就完成了定义自己的网络。
边栏推荐
猜你喜欢

QT VS中双击ui文件无法打开的问题

np.fliplr与np.flipud

数据库 | SQL增删改查基础语法
![[Cloud native] Open source data analysis SPL easily copes with T+0](/img/89/4a96358956782ef9dacf0b700b54c3.png)
[Cloud native] Open source data analysis SPL easily copes with T+0

Android软件安全与逆向分析阅读笔记

Gradle sync failed: Uninitialized object exists on backward branch 142

Tencent Cloud GPU Desktop Server Driver Installation
![[Cloud native] Simple introduction and use of microservice Nacos](/img/06/b0594208d5b0cbf3ae8edd80ec12c4.png)
[Cloud native] Simple introduction and use of microservice Nacos

Chinese garbled solution in UTF-8 environment in Powershell

unicloud 云开发记录
随机推荐
活体检测PatchNet学习笔记
podspec 校验依赖出错问题 pod lib lint ,需要指定源
Markdown 帮助文档
360 hardening file path not exists.
OpenCV中的图像数据格式CV_8U定义
Principle analysis of famous website msdn.itellyou.cn
Understanding SSRF, this article is enough
Artifact SSMwar exploded Error deploying artifact.See server log for details
[Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]
"limit" query in Oracle database
微信小程序源码获取与反编译方式
VS2017 connects to MYSQL
mysql常用命令
break and continue exit in js
Android软件安全与逆向分析阅读笔记
A simple bash to powershell case
朴素贝叶斯文本分类(代码实现)
cocos2d-x-3.2 image graying effect
VS2017连接MYSQL
2021美赛C题M奖思路