Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
book
提交
d68786eb
B
book
项目概览
MindSpore
/
book
通知
3
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d68786eb
编写于
7月 02, 2020
作者:
W
wukesong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify alexnet
上级
69fedea0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
3 deletion
+51
-3
chapter04/alexnet/generator_lr.py
chapter04/alexnet/generator_lr.py
+44
-0
chapter04/alexnet/main.py
chapter04/alexnet/main.py
+7
-3
未找到文件。
chapter04/alexnet/generator_lr.py
0 → 100755
浏览文件 @
d68786eb
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""learning rate generator"""
import
numpy
as
np
def
get_lr
(
current_step
,
lr_max
,
total_epochs
,
steps_per_epoch
):
"""
generate learning rate array
Args:
current_step(int): current steps of the training
lr_max(float): max learning rate
total_epochs(int): total epoch of training
steps_per_epoch(int): steps of one epoch
Returns:
np.array, learning rate array
"""
lr_each_step
=
[]
total_steps
=
steps_per_epoch
*
total_epochs
decay_epoch_index
=
[
0.8
*
total_steps
]
for
i
in
range
(
total_steps
):
if
i
<
decay_epoch_index
[
0
]:
lr
=
lr_max
else
:
lr
=
lr_max
*
0.1
lr_each_step
.
append
(
lr
)
lr_each_step
=
np
.
array
(
lr_each_step
).
astype
(
np
.
float32
)
learning_rate
=
lr_each_step
[
current_step
:]
return
learning_rate
chapter04/alexnet/main.py
浏览文件 @
d68786eb
...
...
@@ -17,14 +17,16 @@ AlexNet example tutorial
Usage:
python alexnet.py
with --device_target=GPU: After 20 epoch training, the accuracy is up to 80%
with --device_target=Ascend: After
10 epoch training, the accuracy is up to 81
%
with --device_target=Ascend: After
30 epoch training, the accuracy is up to 88
%
"""
import
argparse
from
config
import
alexnet_cfg
as
cfg
from
alexnet
import
AlexNet
from
generator_lr
import
get_lr
import
mindspore.dataset
as
ds
import
mindspore.nn
as
nn
from
mindspore
import
Tensor
from
mindspore
import
context
from
mindspore.train.serialization
import
load_checkpoint
,
load_param_into_net
from
mindspore.train.callback
import
ModelCheckpoint
,
CheckpointConfig
,
LossMonitor
...
...
@@ -75,7 +77,7 @@ if __name__ == "__main__":
parser
.
add_argument
(
'--data_path'
,
type
=
str
,
default
=
"./"
,
help
=
'path where the dataset is saved'
)
parser
.
add_argument
(
'--ckpt_path'
,
type
=
str
,
default
=
"./ckpt"
,
help
=
'if mode is test, must provide
\
path where the trained ckpt file'
)
parser
.
add_argument
(
'--dataset_sink_mode'
,
type
=
bool
,
default
=
Fals
e
,
help
=
'dataset_sink_mode is False or True'
)
parser
.
add_argument
(
'--dataset_sink_mode'
,
type
=
bool
,
default
=
Tru
e
,
help
=
'dataset_sink_mode is False or True'
)
args
=
parser
.
parse_args
()
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
args
.
device_target
)
...
...
@@ -83,7 +85,9 @@ if __name__ == "__main__":
network
=
AlexNet
(
cfg
.
num_classes
)
loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
repeat_size
=
cfg
.
epoch_size
opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
cfg
.
learning_rate
,
cfg
.
momentum
)
# when batch_size=32, steps is 1562
lr
=
Tensor
(
get_lr
(
0
,
cfg
.
learning_rate
,
cfg
.
epoch_size
,
1562
))
opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
lr
,
cfg
.
momentum
)
model
=
Model
(
network
,
loss
,
opt
,
metrics
=
{
"Accuracy"
:
Accuracy
()})
# test
if
args
.
mode
==
'train'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录