Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
77bfa3ad
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
1 年多 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
77bfa3ad
编写于
1月 05, 2021
作者:
C
ceci3
提交者:
GitHub
1月 05, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update nas quick start (#584)
上级
4107167f
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
56 addition
and
36 deletion
+56
-36
docs/zh_cn/quick_start/nas_tutorial.md
docs/zh_cn/quick_start/nas_tutorial.md
+56
-36
未找到文件。
docs/zh_cn/quick_start/nas_tutorial.md
浏览文件 @
77bfa3ad
...
@@ -25,7 +25,9 @@
...
@@ -25,7 +25,9 @@
请确认已正确安装Paddle,导入需要的依赖包。
请确认已正确安装Paddle,导入需要的依赖包。
```
python
```
python
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
import
paddle.static
as
static
import
paddleslim
as
slim
import
paddleslim
as
slim
import
numpy
as
np
import
numpy
as
np
```
```
...
@@ -38,60 +40,78 @@ sanas = slim.nas.SANAS(configs=[('MobileNetV2Space')], server_addr=("", 8337), s
...
@@ -38,60 +40,78 @@ sanas = slim.nas.SANAS(configs=[('MobileNetV2Space')], server_addr=("", 8337), s
## 3. 构建网络
## 3. 构建网络
根据传入的网络结构构造训练program和测试program。
根据传入的网络结构构造训练program和测试program。
```
python
```
python
paddle
.
enable_static
()
def
build_program
(
archs
):
def
build_program
(
archs
):
train_program
=
fluid
.
Program
()
train_program
=
static
.
Program
()
startup_program
=
fluid
.
Program
()
startup_program
=
static
.
Program
()
with
fluid
.
program_guard
(
train_program
,
startup_program
):
with
static
.
program_guard
(
train_program
,
startup_program
):
data
=
fluid
.
data
(
name
=
'data'
,
shape
=
[
None
,
3
,
32
,
32
],
dtype
=
'float32'
)
data
=
static
.
data
(
name
=
'data'
,
shape
=
[
None
,
3
,
32
,
32
],
dtype
=
'float32'
)
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
None
,
1
],
dtype
=
'int64'
)
label
=
static
.
data
(
name
=
'label'
,
shape
=
[
None
,
1
],
dtype
=
'int64'
)
gt
=
paddle
.
reshape
(
label
,
[
-
1
,
1
])
output
=
archs
(
data
)
output
=
archs
(
data
)
output
=
fluid
.
layers
.
fc
(
input
=
output
,
size
=
10
)
output
=
static
.
nn
.
fc
(
output
,
size
=
10
)
softmax_out
=
fluid
.
layers
.
softmax
(
input
=
output
,
use_cudnn
=
False
)
softmax_out
=
F
.
softmax
(
output
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
softmax_out
,
label
=
label
)
cost
=
F
.
cross_entropy
(
softmax_out
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
cost
)
avg_cost
=
paddle
.
mean
(
cost
)
acc_top1
=
fluid
.
layers
.
accuracy
(
input
=
softmax_out
,
label
=
label
,
k
=
1
)
acc_top1
=
paddle
.
metric
.
accuracy
(
input
=
softmax_out
,
label
=
gt
,
k
=
1
)
acc_top5
=
fluid
.
layers
.
accuracy
(
input
=
softmax_out
,
label
=
label
,
k
=
5
)
acc_top5
=
paddle
.
metric
.
accuracy
(
input
=
softmax_out
,
label
=
gt
,
k
=
5
)
test_program
=
fluid
.
default_main_program
().
clone
(
for_test
=
True
)
test_program
=
static
.
default_main_program
().
clone
(
for_test
=
True
)
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.1
)
optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
0.1
)
optimizer
.
minimize
(
avg_cost
)
optimizer
.
minimize
(
avg_cost
)
place
=
fluid
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
exe
=
static
.
Executor
(
place
)
exe
.
run
(
startup_program
)
exe
.
run
(
startup_program
)
return
exe
,
train_program
,
test_program
,
(
data
,
label
),
avg_cost
,
acc_top1
,
acc_top5
return
exe
,
train_program
,
test_program
,
(
data
,
label
),
avg_cost
,
acc_top1
,
acc_top5
```
```
## 4. 定义输入数据函数
## 4. 定义输入数据函数
使用的数据集为cifar10,paddle框架中
`paddle.dataset.cifar`
包括了cifar数据集的下载和读取,代码如下:
为了快速执行该示例,我们使用的数据集为CIFAR10,Paddle框架的
`paddle.vision.datasets.Cifar10`
包定义了CIFAR10数据的下载和读取。 代码如下:
```
python
```
python
def
input_data
(
inputs
):
import
paddle.vision.transforms
as
T
train_reader
=
paddle
.
fluid
.
io
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
cifar
.
train10
(
cycle
=
False
),
buf_size
=
1024
),
batch_size
=
256
)
train_feeder
=
fluid
.
DataFeeder
(
inputs
,
fluid
.
CPUPlace
())
def
input_data
(
image
,
label
):
eval_reader
=
paddle
.
fluid
.
io
.
batch
(
paddle
.
dataset
.
cifar
.
test10
(
cycle
=
False
),
batch_size
=
256
)
transform
=
T
.
Compose
([
T
.
Normalize
([
127.5
],
[
127.5
])])
eval_feeder
=
fluid
.
DataFeeder
(
inputs
,
fluid
.
CPUPlace
())
train_dataset
=
paddle
.
vision
.
datasets
.
Cifar10
(
mode
=
"train"
,
transform
=
transform
)
return
train_reader
,
train_feeder
,
eval_reader
,
eval_feeder
train_loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
places
=
paddle
.
CPUPlace
(),
feed_list
=
[
image
,
label
],
drop_last
=
True
,
batch_size
=
64
,
return_list
=
False
,
shuffle
=
True
)
eval_dataset
=
paddle
.
vision
.
datasets
.
Cifar10
(
mode
=
"test"
,
transform
=
transform
)
eval_loader
=
paddle
.
io
.
DataLoader
(
eval_dataset
,
places
=
paddle
.
CPUPlace
(),
feed_list
=
[
image
,
label
],
drop_last
=
False
,
batch_size
=
64
,
return_list
=
False
,
shuffle
=
False
)
return
train_loader
,
eval_loader
```
```
## 5. 定义训练函数
## 5. 定义训练函数
根据训练program和训练数据进行训练。
根据训练program和训练数据进行训练。
```
python
```
python
def
start_train
(
program
,
data_
reader
,
data_fee
der
):
def
start_train
(
program
,
data_
loa
der
):
outputs
=
[
avg_cost
.
name
,
acc_top1
.
name
,
acc_top5
.
name
]
outputs
=
[
avg_cost
.
name
,
acc_top1
.
name
,
acc_top5
.
name
]
for
data
in
data_
re
ader
():
for
data
in
data_
lo
ader
():
batch_reward
=
exe
.
run
(
program
,
feed
=
data
_feeder
.
feed
(
data
)
,
fetch_list
=
outputs
)
batch_reward
=
exe
.
run
(
program
,
feed
=
data
,
fetch_list
=
outputs
)
print
(
"TRAIN: loss: {}, acc1: {}, acc5:{}"
.
format
(
batch_reward
[
0
],
batch_reward
[
1
],
batch_reward
[
2
]))
print
(
"TRAIN: loss: {}, acc1: {}, acc5:{}"
.
format
(
batch_reward
[
0
],
batch_reward
[
1
],
batch_reward
[
2
]))
```
```
## 6. 定义评估函数
## 6. 定义评估函数
根据评估program和评估数据进行评估。
根据评估program和评估数据进行评估。
```
python
```
python
def
start_eval
(
program
,
data_
reader
,
data_fee
der
):
def
start_eval
(
program
,
data_
loa
der
):
reward
=
[]
reward
=
[]
outputs
=
[
avg_cost
.
name
,
acc_top1
.
name
,
acc_top5
.
name
]
outputs
=
[
avg_cost
.
name
,
acc_top1
.
name
,
acc_top5
.
name
]
for
data
in
data_
re
ader
():
for
data
in
data_
lo
ader
():
batch_reward
=
exe
.
run
(
program
,
feed
=
data
_feeder
.
feed
(
data
)
,
fetch_list
=
outputs
)
batch_reward
=
exe
.
run
(
program
,
feed
=
data
,
fetch_list
=
outputs
)
reward_avg
=
np
.
mean
(
np
.
array
(
batch_reward
),
axis
=
1
)
reward_avg
=
np
.
mean
(
np
.
array
(
batch_reward
),
axis
=
1
)
reward
.
append
(
reward_avg
)
reward
.
append
(
reward_avg
)
print
(
"TEST: loss: {}, acc1: {}, acc5:{}"
.
format
(
batch_reward
[
0
],
batch_reward
[
1
],
batch_reward
[
2
]))
print
(
"TEST: loss: {}, acc1: {}, acc5:{}"
.
format
(
batch_reward
[
0
],
batch_reward
[
1
],
batch_reward
[
2
]))
...
@@ -112,23 +132,23 @@ archs = sanas.next_archs()[0]
...
@@ -112,23 +132,23 @@ archs = sanas.next_archs()[0]
### 7.2 构造program
### 7.2 构造program
调用步骤3中的函数,根据4.1中的模型结构构造相应的program。
调用步骤3中的函数,根据4.1中的模型结构构造相应的program。
```
python
```
python
exe
,
train_program
,
eval_program
,
inputs
,
avg_cost
,
acc_top1
,
acc_top5
=
build_program
(
archs
)
exe
,
train_program
,
eval_program
,
(
image
,
label
)
,
avg_cost
,
acc_top1
,
acc_top5
=
build_program
(
archs
)
```
```
### 7.3 定义输入数据
### 7.3 定义输入数据
```
python
```
python
train_
reader
,
train_feeder
,
eval_reader
,
eval_feeder
=
input_data
(
inputs
)
train_
loader
,
eval_loader
=
input_data
(
image
,
label
)
```
```
### 7.4 训练模型
### 7.4 训练模型
根据上面得到的训练program和评估数据启动训练。
根据上面得到的训练program和评估数据启动训练。
```
python
```
python
start_train
(
train_program
,
train_
reader
,
train_fee
der
)
start_train
(
train_program
,
train_
loa
der
)
```
```
### 7.5 评估模型
### 7.5 评估模型
根据上面得到的评估program和评估数据启动评估。
根据上面得到的评估program和评估数据启动评估。
```
python
```
python
finally_reward
=
start_eval
(
eval_program
,
eval_
reader
,
eval_fee
der
)
finally_reward
=
start_eval
(
eval_program
,
eval_
loa
der
)
```
```
### 7.6 回传当前模型的得分
### 7.6 回传当前模型的得分
```
```
...
@@ -141,16 +161,16 @@ sanas.reward(float(finally_reward[1]))
...
@@ -141,16 +161,16 @@ sanas.reward(float(finally_reward[1]))
for
step
in
range
(
3
):
for
step
in
range
(
3
):
archs
=
sanas
.
next_archs
()[
0
]
archs
=
sanas
.
next_archs
()[
0
]
exe
,
train_program
,
eval_program
,
inputs
,
avg_cost
,
acc_top1
,
acc_top5
=
build_program
(
archs
)
exe
,
train_program
,
eval_program
,
inputs
,
avg_cost
,
acc_top1
,
acc_top5
=
build_program
(
archs
)
train_
reader
,
train_feeder
,
eval_reader
,
eval_fee
der
=
input_data
(
inputs
)
train_
loader
,
eval_loa
der
=
input_data
(
inputs
)
current_flops
=
slim
.
analysis
.
flops
(
train_program
)
current_flops
=
slim
.
analysis
.
flops
(
train_program
)
if
current_flops
>
321208544
:
if
current_flops
>
321208544
:
continue
continue
for
epoch
in
range
(
7
):
for
epoch
in
range
(
7
):
start_train
(
train_program
,
train_
reader
,
train_fee
der
)
start_train
(
train_program
,
train_
loa
der
)
finally_reward
=
start_eval
(
eval_program
,
eval_
reader
,
eval_fee
der
)
finally_reward
=
start_eval
(
eval_program
,
eval_
loa
der
)
sanas
.
reward
(
float
(
finally_reward
[
1
]))
sanas
.
reward
(
float
(
finally_reward
[
1
]))
```
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录