当前位置:网站首页>Pytorch实现ResNet
Pytorch实现ResNet
2022-07-31 05:16:00 【王大队长】
目标:用pytorch实现下图所示的网络

代码:
import torch
from torch import nn
import torch.nn.functional as F
class ResBlock(nn.Module): #残差块的实现也是继承nn.module后实现一个类,同样的要实现__init__()方法和forward方法
def __init__(self, n_chans):
super().__init__()
self.conv = nn.Conv2d(n_chans, n_chans, kernel_size=3, padding=1, bias=False)
self.batch_norm = nn.BatchNorm2d(n_chans)
torch.nn.init.kaiming_normal_(self.conv.weight, nonlinearity='relu') #参数初始化
torch.nn.init.constant_(self.batch_norm.weight, 0.5)
torch.nn.init.zeros_(self.batch_norm.bias)
def forward(self,x):
out = self.conv(x)
out = self.batch_norm(out)
out = F.relu(out)
return out + x
class NetResDepp(nn.Module):
def __init__(self, n_chans1=32, num_blocks=100):
super().__init__()
self.n_chans1 = n_chans1
self.num_blocks = num_blocks
self.conv = nn.Conv2d(3, n_chans1, kernel_size=3, padding=1)
self.resblocks = nn.Sequential(*(num_blocks * [*ResBlock(n_chans=n_chans1)])) # 注意这里的100个Resblock是通过先对ResBlock解包放到列表里,再用100乘这个列表就实现了将列表复制100倍,再解包就实现了100个ResBlock
self.fc1 = nn.Linear(8 * 8 * n_chans1, 32)
self.fc2 = nn.Linear(32,2)
def forward(self, x):
out = F.relu(self.conv(x))
out = F.max_pool2d(out, 2)
out = self.resblocks(out)
out = F.max_pool2d(out, 2)
out = out.view(-1, 8 * 8 * self.n_chans1)
out = self.fc1(out)
out = self.fc2(out)
return out
参考资料:
pytorch深度学习实战(伊莱史蒂文斯)
边栏推荐
- Flutter mixed development module dependencies
- sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
- quick-3.5 lua调用c++
- 腾讯云GPU桌面服务器驱动安装
- 网页截图与反向代理
- [Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version
- The browser looks for events bound or listened to by js
- Chinese garbled solution in UTF-8 environment in Powershell
- 2021 Mianjing - Embrace Change
- quick-3.6源码修改纪录
猜你喜欢

UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装

Access database query

为数学而歌之伯努利家族

自定dialog 布局没有居中解决方案

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

qt:cannot open C:\Users\某某某\AppData\Local\Temp\main.obj.15576.16.jom for write

腾讯云GPU桌面服务器驱动安装

The server time zone value ‘й‘ is unrecognized or represents more than one time zone

朴素贝叶斯文本分类(代码实现)

VS通过ODBC连接MYSQL(一)
随机推荐
VTK环境配置
SQLite 查询表中每天插入的数量
C语言 | 获取字符串里逗号间隔的内容
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
jenkins +miniprogram-ci 一键上传微信小程序
Notes on creating a new virtual machine in Hyper-V
VS通过ODBC连接MYSQL(二)
break and continue exit in js
The browser looks for events bound or listened to by js
5 methods of MySQL paging query
this points to the problem
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
Principle analysis of famous website msdn.itellyou.cn
The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
powershell统计文件夹大小
Global scope and function scope in js
unicloud 云开发记录
动态规划(一)| 斐波那契数列和归递
Understanding of objects and functions in js
如何修改数据库密码