Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
d2l-zh
提交
9af9f0b3
D
d2l-zh
项目概览
OpenDocCN
/
d2l-zh
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
d2l-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9af9f0b3
编写于
7月 18, 2018
作者:
A
Aston Zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update resnet in cifar
上级
ab28f55a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
22 addition
and
23 deletion
+22
-23
chapter_computer-vision/kaggle-gluon-cifar10.md
chapter_computer-vision/kaggle-gluon-cifar10.md
+22
-23
未找到文件。
chapter_computer-vision/kaggle-gluon-cifar10.md
浏览文件 @
9af9f0b3
...
...
@@ -184,30 +184,29 @@ test_data = gdata.DataLoader(test_ds.transform_first(transform_test),
## 定义模型
我们
这里使用了
ResNet-18模型,并使用混合式编程来提升执行效率。
我们
在这里定义
ResNet-18模型,并使用混合式编程来提升执行效率。
```
{.python .input n=6}
class Residual(nn.HybridBlock):
def __init__(self,
channels, same_shape=True
, **kwargs):
def __init__(self,
num_channels, use_1x1conv=False, strides=1
, **kwargs):
super(Residual, self).__init__(**kwargs)
self.same_shape = same_shape
with self.name_scope():
strides = 1 if same_shape else 2
self.conv1 = nn.Conv2D(channels, kernel_size=3, padding=1,
self.conv1 = nn.Conv2D(num_channels, kernel_size=3, padding=1,
strides=strides)
self.conv2 = nn.Conv2D(num_channels, kernel_size=3, padding=1)
if use_1x1conv:
self.conv3 = nn.Conv2D(num_channels, kernel_size=1,
strides=strides)
self.bn1 = nn.BatchNorm()
self.conv2 = nn.Conv2D(channels, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm()
if not same_shape:
self.conv3 = nn.Conv2D(channels, kernel_size=1,
strides=strides)
else:
self.conv3 = None
self.bn1 = nn.BatchNorm()
self.bn2 = nn.BatchNorm()
def hybrid_forward(self, F,
x
):
out = F.relu(self.bn1(self.conv1(x
)))
out = self.bn2(self.conv2(out
))
if
not self.same_shape
:
x = self.conv3(x
)
return F.relu(
out + x
)
def hybrid_forward(self, F,
X
):
Y = F.relu(self.bn1(self.conv1(X
)))
Y = self.bn2(self.conv2(Y
))
if
self.conv3
:
X = self.conv3(X
)
return F.relu(
Y + X
)
class ResNet(nn.HybridBlock):
...
...
@@ -223,15 +222,15 @@ class ResNet(nn.HybridBlock):
net.add(nn.Activation(activation='relu'))
# 模块 2。
for _ in range(3):
net.add(Residual(channels=32))
net.add(Residual(
num_
channels=32))
# 模块 3。
net.add(Residual(
channels=64, same_shape=False
))
net.add(Residual(
num_channels=64, use_1x1conv=True, strides=2
))
for _ in range(2):
net.add(Residual(channels=64))
net.add(Residual(
num_
channels=64))
# 模块 4。
net.add(Residual(
channels=128, same_shape=False
))
net.add(Residual(
num_channels=128, use_1x1conv=True, strides=2
))
for _ in range(2):
net.add(Residual(channels=128))
net.add(Residual(
num_
channels=128))
# 模块 5。
net.add(nn.AvgPool2D(pool_size=8))
net.add(nn.Flatten())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录