Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
docs
提交
71496334
D
docs
项目概览
MindSpore
/
docs
通知
4
Star
2
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看板
提交
71496334
编写于
4月 27, 2020
作者:
X
Xiaoda
提交者:
Gitee
4月 27, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update tutorials/source_zh_cn/advanced_use/mixed_precision.md.
上级
29e26f19
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
29 addition
and
3 deletion
+29
-3
tutorials/source_zh_cn/advanced_use/mixed_precision.md
tutorials/source_zh_cn/advanced_use/mixed_precision.md
+29
-3
未找到文件。
tutorials/source_zh_cn/advanced_use/mixed_precision.md
浏览文件 @
71496334
...
...
@@ -45,9 +45,20 @@ MindSpore混合精度典型的计算流程如下图所示:
代码样例如下:
```
python
import
numpy
as
np
import
mindspore.nn
as
nn
import
mindspore.common.dtype
as
mstype
from
mindspore
import
Tensor
,
context
from
mindspore.ops
import
operations
as
P
from
mindspore.nn
import
WithLossCell
from
mindspore.nn
import
Momentum
from
mindspore.nn.loss
import
MSELoss
# The interface of Auto_mixed precision
from
mindspore.train
import
amp
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
)
context
.
set_context
(
device_target
=
"Ascend"
)
# Define network
class
LeNet5
(
nn
.
Cell
):
def
__init__
(
self
):
...
...
@@ -58,7 +69,7 @@ class LeNet5(nn.Cell):
self
.
fc2
=
nn
.
Dense
(
120
,
84
)
self
.
fc3
=
nn
.
Dense
(
84
,
10
)
self
.
relu
=
nn
.
ReLU
()
self
.
max_pool2d
=
nn
.
MaxPool2d
(
kernel_size
=
2
)
self
.
max_pool2d
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
flatten
=
P
.
Flatten
()
def
construct
(
self
,
x
):
...
...
@@ -103,6 +114,19 @@ MindSpore还支持手动混合精度。假定在网络中只有一个Dense Layer
代码样例如下:
```
python
import
numpy
as
np
import
mindspore.nn
as
nn
import
mindspore.common.dtype
as
mstype
from
mindspore
import
Tensor
,
context
from
mindspore.ops
import
operations
as
P
from
mindspore.nn
import
WithLossCell
,
TrainOneStepWithLossScaleCell
from
mindspore.nn
import
Momentum
from
mindspore.nn.loss
import
MSELoss
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
)
context
.
set_context
(
device_target
=
"Ascend"
)
# Define network
class
LeNet5
(
nn
.
Cell
):
def
__init__
(
self
):
...
...
@@ -111,9 +135,9 @@ class LeNet5(nn.Cell):
self
.
conv2
=
nn
.
Conv2d
(
6
,
16
,
5
,
pad_mode
=
'valid'
)
self
.
fc1
=
nn
.
Dense
(
16
*
5
*
5
,
120
)
self
.
fc2
=
nn
.
Dense
(
120
,
84
)
self
.
fc3
=
nn
.
Dense
(
84
,
10
)
.
to_float
(
mstype
.
float32
)
self
.
fc3
=
nn
.
Dense
(
84
,
10
)
self
.
relu
=
nn
.
ReLU
()
self
.
max_pool2d
=
nn
.
MaxPool2d
(
kernel_size
=
2
)
self
.
max_pool2d
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
flatten
=
P
.
Flatten
()
def
construct
(
self
,
x
):
...
...
@@ -128,6 +152,7 @@ class LeNet5(nn.Cell):
# Initialize network and set mixing precision
net
=
LeNet5
()
net
.
to_float
(
mstype
.
float16
)
net
.
fc3
.
to_float
(
mstype
.
float32
)
# Define training data, label and sens
predict
=
Tensor
(
np
.
ones
([
1
,
1
,
32
,
32
]).
astype
(
np
.
float32
)
*
0.01
)
...
...
@@ -140,6 +165,7 @@ loss = MSELoss()
optimizer
=
Momentum
(
params
=
net
.
trainable_params
(),
learning_rate
=
0.1
,
momentum
=
0.9
)
net_with_loss
=
WithLossCell
(
net
,
loss
)
train_network
=
TrainOneStepWithLossScaleCell
(
net_with_loss
,
optimizer
)
train_network
.
set_train
()
# Run training
output
=
train_network
(
predict
,
label
,
scaling_sens
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录