当前位置:网站首页>用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))
这样,我们就完成了定义自己的网络。
边栏推荐
- Gradle sync failed: Uninitialized object exists on backward branch 142
- Multi-Modal Face Anti-Spoofing Based on Central Difference Networks学习笔记
- cocos2d-x-3.2 image graying effect
- Build vulhub vulnerability shooting range on kali
- 场效应管 | N-mos内部结构详解
- WeChat applet source code acquisition and decompilation method
- MySQL面试题大全(陆续更新)
- ERROR Error: No module factory availabl at Object.PROJECT_CONFIG_JSON_NOT_VALID_OR_NOT_EXIST ‘Error
- 网页截图与反向代理
- VS通过ODBC连接MYSQL(二)
猜你喜欢
js中的全局作用域与函数作用域
Android软件安全与逆向分析阅读笔记
动态规划(一)| 斐波那契数列和归递
为数学而歌之伯努利家族
After unicloud is released, the applet prompts that the connection to the local debugging service failed. Please check whether the client and the host are under the same local area network.
unicloud 云开发记录
安装Multisim出现 No software will be installed or removed解决方法
自定dialog 布局没有居中解决方案
The browser looks for events bound or listened to by js
This in js points to the prototype object
随机推荐
微信小程序源码获取与反编译方式
禅道安装及使用教程
人脸识别AdaFace学习笔记
Talking about the understanding of CAP in distributed mode
function in js
np.fliplr与np.flipud
活体检测PatchNet学习笔记
Understanding of js arrays
cocos2d-x-3.2创建项目方法
quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
[uiautomation] Get WeChat friend list (stored in txt)
自定dialog 布局没有居中解决方案
OpenCV中的图像数据格式CV_8U定义
MYSQL事务与锁问题处理
flutter 混合开发 module 依赖
Podspec automatic upgrade script
js中的对象与函数的理解
sql add default constraint
[Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]
js中的break与continue退出