Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
docs
提交
e130247c
D
docs
项目概览
MindSpore
/
docs
通知
5
Star
3
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e130247c
编写于
8月 20, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
8月 20, 2020
浏览文件
操作
浏览文件
下载
差异文件
!687 delete lenet and create_dataset api description
Merge pull request !687 from jinyaohui/master
上级
3ed41c7e
aa5c4c59
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
4 addition
and
104 deletion
+4
-104
tutorials/source_zh_cn/advanced_use/gradient_accumulation.md
tutorials/source_zh_cn/advanced_use/gradient_accumulation.md
+4
-104
未找到文件。
tutorials/source_zh_cn/advanced_use/gradient_accumulation.md
浏览文件 @
e130247c
...
...
@@ -28,7 +28,7 @@
最终目的是为了达到跟直接用N
*
Mini-batch数据训练几乎同样的效果。
> 本教程用于GPU、Ascend 910 AI处理器, 你可以在这里下载
完整的
样例代码:<https://gitee.com/mindspore/docs/tree/master/tutorials/tutorial_code/gradient_accumulation>
> 本教程用于GPU、Ascend 910 AI处理器, 你可以在这里下载
主要的训练
样例代码:<https://gitee.com/mindspore/docs/tree/master/tutorials/tutorial_code/gradient_accumulation>
## 创建梯度累积模型
...
...
@@ -57,111 +57,11 @@ from model_zoo.official.cv.lenet.src.lenet import LeNet5
### 加载数据集
利用MindSpore的dataset提供的
`MnistDataset`
接口加载MNIST数据集。
```
python
def
create_dataset
(
data_path
,
batch_size
=
32
,
repeat_size
=
1
,
num_parallel_workers
=
1
):
"""
create dataset for train or test
"""
# define dataset
mnist_ds
=
ds
.
MnistDataset
(
data_path
)
resize_height
,
resize_width
=
32
,
32
rescale
=
1.0
/
255.0
shift
=
0.0
rescale_nml
=
1
/
0.3081
shift_nml
=
-
1
*
0.1307
/
0.3081
# define map operations
resize_op
=
CV
.
Resize
((
resize_height
,
resize_width
),
interpolation
=
Inter
.
LINEAR
)
# Bilinear mode
rescale_nml_op
=
CV
.
Rescale
(
rescale_nml
,
shift_nml
)
rescale_op
=
CV
.
Rescale
(
rescale
,
shift
)
hwc2chw_op
=
CV
.
HWC2CHW
()
type_cast_op
=
C
.
TypeCast
(
mstype
.
int32
)
# apply map operations on images
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"label"
,
operations
=
type_cast_op
,
num_parallel_workers
=
num_parallel_workers
)
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"image"
,
operations
=
resize_op
,
num_parallel_workers
=
num_parallel_workers
)
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"image"
,
operations
=
rescale_op
,
num_parallel_workers
=
num_parallel_workers
)
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"image"
,
operations
=
rescale_nml_op
,
num_parallel_workers
=
num_parallel_workers
)
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"image"
,
operations
=
hwc2chw_op
,
num_parallel_workers
=
num_parallel_workers
)
# apply DatasetOps
buffer_size
=
10000
mnist_ds
=
mnist_ds
.
shuffle
(
buffer_size
=
buffer_size
)
# 10000 as in LeNet train script
mnist_ds
=
mnist_ds
.
batch
(
batch_size
,
drop_remainder
=
True
)
mnist_ds
=
mnist_ds
.
repeat
(
repeat_size
)
return
mnist_ds
```
利用MindSpore的dataset提供的
`MnistDataset`
接口加载MNIST数据集,此部分代码由model_zoo中lenet目录下的
[
dataset.py
](
<
https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/lenet/src/dataset.py
>
)
导入。
### 定义网络
这里以LeNet网络为例进行介绍,当然也可以使用其它的网络,如ResNet-50、BERT等。
```
python
def
conv
(
in_channels
,
out_channels
,
kernel_size
,
stride
=
1
,
padding
=
0
):
"""weight initial for conv layer"""
weight
=
weight_variable
()
return
nn
.
Conv2d
(
in_channels
,
out_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
padding
=
padding
,
weight_init
=
weight
,
has_bias
=
False
,
pad_mode
=
"valid"
)
def
fc_with_initialize
(
input_channels
,
out_channels
):
"""weight initial for fc layer"""
weight
=
weight_variable
()
bias
=
weight_variable
()
return
nn
.
Dense
(
input_channels
,
out_channels
,
weight
,
bias
)
def
weight_variable
():
"""weight initial"""
return
TruncatedNormal
(
0.02
)
class
LeNet5
(
nn
.
Cell
):
"""
Lenet network
Args:
num_class (int): Number classes. Default: 10.
Returns:
Tensor, output tensor
Examples:
>>> LeNet(num_class=10)
"""
def
__init__
(
self
,
num_class
=
10
,
channel
=
1
):
super
(
LeNet5
,
self
).
__init__
()
self
.
num_class
=
num_class
self
.
conv1
=
conv
(
channel
,
6
,
5
)
self
.
conv2
=
conv
(
6
,
16
,
5
)
self
.
fc1
=
fc_with_initialize
(
16
*
5
*
5
,
120
)
self
.
fc2
=
fc_with_initialize
(
120
,
84
)
self
.
fc3
=
fc_with_initialize
(
84
,
self
.
num_class
)
self
.
relu
=
nn
.
ReLU
()
self
.
max_pool2d
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
flatten
=
nn
.
Flatten
()
def
construct
(
self
,
x
):
x
=
self
.
conv1
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
max_pool2d
(
x
)
x
=
self
.
conv2
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
max_pool2d
(
x
)
x
=
self
.
flatten
(
x
)
x
=
self
.
fc1
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
fc2
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
fc3
(
x
)
return
x
```
这里以LeNet网络为例进行介绍,当然也可以使用其它的网络,如ResNet-50、BERT等, 此部分代码由model_zoo中lenet目录下的
[
lenet.py
](
<
https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/lenet/src/lenet.py
>
)
导入。
### 定义训练模型
将训练流程拆分为正向反向训练、参数更新和累积梯度清理三个部分:
...
...
@@ -350,7 +250,7 @@ if __name__ == "__main__":
**验证模型**
通过model_zoo
下lenet网络
的
[
eval.py
](
<
https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/lenet/train.py
>
)
,使用保存的CheckPoint文件,加载验证数据集,进行验证。
通过model_zoo
中lenet目录下
的
[
eval.py
](
<
https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/lenet/train.py
>
)
,使用保存的CheckPoint文件,加载验证数据集,进行验证。
```
shell
$
python eval.py
--data_path
=
./MNIST_Data
--ckpt_path
=
./gradient_accumulation.ckpt
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录