Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
23416e81
B
book
项目概览
PaddlePaddle
/
book
通知
16
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
23416e81
编写于
5月 22, 2019
作者:
R
root
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify ce file code style of 01.fit_a_line and 02.recognize_digits
上级
e9abf856
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
93 addition
and
60 deletion
+93
-60
01.fit_a_line/_ce.py
01.fit_a_line/_ce.py
+17
-14
01.fit_a_line/image/prediction_gt.png
01.fit_a_line/image/prediction_gt.png
+0
-0
01.fit_a_line/train.py
01.fit_a_line/train.py
+33
-18
02.recognize_digits/_ce.py
02.recognize_digits/_ce.py
+15
-13
02.recognize_digits/train.py
02.recognize_digits/train.py
+28
-15
未找到文件。
01.fit_a_line/_ce.py
浏览文件 @
23416e81
...
@@ -9,28 +9,31 @@ from kpi import CostKpi
...
@@ -9,28 +9,31 @@ from kpi import CostKpi
train_cost_kpi
=
CostKpi
(
'train_cost'
,
0.02
,
0
,
actived
=
True
,
desc
=
'train cost'
)
train_cost_kpi
=
CostKpi
(
'train_cost'
,
0.02
,
0
,
actived
=
True
,
desc
=
'train cost'
)
test_cost_kpi
=
CostKpi
(
'test_cost'
,
0.02
,
0
,
actived
=
True
,
desc
=
'test cost'
)
test_cost_kpi
=
CostKpi
(
'test_cost'
,
0.02
,
0
,
actived
=
True
,
desc
=
'test cost'
)
tracking_kpis
=
[
train_cost_kpi
,
test_cost_kpi
]
tracking_kpis
=
[
train_cost_kpi
,
test_cost_kpi
]
def
parse_log
(
log
):
def
parse_log
(
log
):
for
line
in
log
.
split
(
'
\n
'
):
for
line
in
log
.
split
(
'
\n
'
):
fs
=
line
.
strip
().
split
(
'
\t
'
)
fs
=
line
.
strip
().
split
(
'
\t
'
)
print
(
fs
)
print
(
fs
)
if
len
(
fs
)
==
3
and
fs
[
0
]
==
'kpis'
:
if
len
(
fs
)
==
3
and
fs
[
0
]
==
'kpis'
:
print
(
"-----%s"
%
fs
)
print
(
"-----%s"
%
fs
)
kpi_name
=
fs
[
1
]
kpi_name
=
fs
[
1
]
kpi_value
=
float
(
fs
[
2
])
kpi_value
=
float
(
fs
[
2
])
yield
kpi_name
,
kpi_value
yield
kpi_name
,
kpi_value
def
log_to_ce
(
log
):
def
log_to_ce
(
log
):
kpi_tracker
=
{}
kpi_tracker
=
{}
for
kpi
in
tracking_kpis
:
for
kpi
in
tracking_kpis
:
kpi_tracker
[
kpi
.
name
]
=
kpi
kpi_tracker
[
kpi
.
name
]
=
kpi
for
(
kpi_name
,
kpi_value
)
in
parse_log
(
log
):
for
(
kpi_name
,
kpi_value
)
in
parse_log
(
log
):
print
(
kpi_name
,
kpi_value
)
print
(
kpi_name
,
kpi_value
)
kpi_tracker
[
kpi_name
].
add_record
(
kpi_value
)
kpi_tracker
[
kpi_name
].
add_record
(
kpi_value
)
kpi_tracker
[
kpi_name
].
persist
()
kpi_tracker
[
kpi_name
].
persist
()
if
__name__
==
'__main__'
:
log
=
sys
.
stdin
.
read
()
log_to_ce
(
log
)
if
__name__
==
'__main__'
:
log
=
sys
.
stdin
.
read
()
log_to_ce
(
log
)
01.fit_a_line/image/prediction_gt.png
查看替换文件 @
e9abf856
浏览文件 @
23416e81
31.4 KB
|
W:
|
H:
31.4 KB
|
W:
|
H:
2-up
Swipe
Onion skin
01.fit_a_line/train.py
浏览文件 @
23416e81
...
@@ -23,14 +23,24 @@ import numpy
...
@@ -23,14 +23,24 @@ import numpy
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
def
parse_args
():
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
"fit_a_line"
)
parser
=
argparse
.
ArgumentParser
(
"fit_a_line"
)
parser
.
add_argument
(
'--enable_ce'
,
action
=
'store_true'
,
help
=
"If set, run the task with continuous evaluation logs."
)
parser
.
add_argument
(
parser
.
add_argument
(
'--use_gpu'
,
type
=
bool
,
default
=
False
,
help
=
"Whether to use GPU or not."
)
'--enable_ce'
,
parser
.
add_argument
(
'--num_epochs'
,
type
=
int
,
default
=
100
,
help
=
"number of epochs."
)
action
=
'store_true'
,
help
=
"If set, run the task with continuous evaluation logs."
)
parser
.
add_argument
(
'--use_gpu'
,
type
=
bool
,
default
=
False
,
help
=
"Whether to use GPU or not."
)
parser
.
add_argument
(
'--num_epochs'
,
type
=
int
,
default
=
100
,
help
=
"number of epochs."
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
return
args
return
args
# For training test cost
# For training test cost
def
train_test
(
executor
,
program
,
reader
,
feeder
,
fetch_list
):
def
train_test
(
executor
,
program
,
reader
,
feeder
,
fetch_list
):
accumulated
=
1
*
[
0
]
accumulated
=
1
*
[
0
]
...
@@ -62,14 +72,18 @@ def main():
...
@@ -62,14 +72,18 @@ def main():
batch_size
=
20
batch_size
=
20
if
args
.
enable_ce
:
if
args
.
enable_ce
:
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
uci_housing
.
train
(),
batch_size
=
batch_size
)
train_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
uci_housing
.
test
(),
batch_size
=
batch_size
)
paddle
.
dataset
.
uci_housing
.
train
(),
batch_size
=
batch_size
)
else
:
test_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
uci_housing
.
test
(),
batch_size
=
batch_size
)
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
uci_housing
.
train
(),
buf_size
=
500
),
else
:
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
uci_housing
.
train
(),
buf_size
=
500
),
batch_size
=
batch_size
)
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
uci_housing
.
test
(),
buf_size
=
500
),
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
uci_housing
.
test
(),
buf_size
=
500
),
batch_size
=
batch_size
)
batch_size
=
batch_size
)
# feature vector of length 13
# feature vector of length 13
...
@@ -78,11 +92,11 @@ def main():
...
@@ -78,11 +92,11 @@ def main():
main_program
=
fluid
.
default_main_program
()
main_program
=
fluid
.
default_main_program
()
startup_program
=
fluid
.
default_startup_program
()
startup_program
=
fluid
.
default_startup_program
()
if
args
.
enable_ce
:
if
args
.
enable_ce
:
main_program
.
random_seed
=
90
main_program
.
random_seed
=
90
startup_program
.
random_seed
=
90
startup_program
.
random_seed
=
90
y_predict
=
fluid
.
layers
.
fc
(
input
=
x
,
size
=
1
,
act
=
None
)
y_predict
=
fluid
.
layers
.
fc
(
input
=
x
,
size
=
1
,
act
=
None
)
cost
=
fluid
.
layers
.
square_error_cost
(
input
=
y_predict
,
label
=
y
)
cost
=
fluid
.
layers
.
square_error_cost
(
input
=
y_predict
,
label
=
y
)
avg_loss
=
fluid
.
layers
.
mean
(
cost
)
avg_loss
=
fluid
.
layers
.
mean
(
cost
)
...
@@ -140,12 +154,13 @@ def main():
...
@@ -140,12 +154,13 @@ def main():
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
if
params_dirname
is
not
None
:
if
params_dirname
is
not
None
:
# We can save the trained parameters for the inferences later
# We can save the trained parameters for the inferences later
fluid
.
io
.
save_inference_model
(
params_dirname
,
[
'x'
],
[
y_predict
],
exe
)
fluid
.
io
.
save_inference_model
(
params_dirname
,
[
'x'
],
[
y_predict
],
exe
)
if
args
.
enable_ce
and
pass_id
==
args
.
num_epochs
-
1
:
if
args
.
enable_ce
and
pass_id
==
args
.
num_epochs
-
1
:
print
(
"kpis
\t
train_cost
\t
%f"
%
avg_loss_value
[
0
])
print
(
"kpis
\t
train_cost
\t
%f"
%
avg_loss_value
[
0
])
print
(
"kpis
\t
test_cost
\t
%f"
%
test_metics
[
0
])
print
(
"kpis
\t
test_cost
\t
%f"
%
test_metics
[
0
])
infer_exe
=
fluid
.
Executor
(
place
)
infer_exe
=
fluid
.
Executor
(
place
)
inference_scope
=
fluid
.
core
.
Scope
()
inference_scope
=
fluid
.
core
.
Scope
()
...
@@ -182,5 +197,5 @@ def main():
...
@@ -182,5 +197,5 @@ def main():
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
args
=
parse_args
()
args
=
parse_args
()
main
()
main
()
02.recognize_digits/_ce.py
浏览文件 @
23416e81
...
@@ -10,28 +10,30 @@ from kpi import AccKpi
...
@@ -10,28 +10,30 @@ from kpi import AccKpi
train_cost_kpi
=
CostKpi
(
'train_cost'
,
0.02
,
0
,
actived
=
True
,
desc
=
'train cost'
)
train_cost_kpi
=
CostKpi
(
'train_cost'
,
0.02
,
0
,
actived
=
True
,
desc
=
'train cost'
)
test_cost_kpi
=
CostKpi
(
'test_cost'
,
0.02
,
0
,
actived
=
True
,
desc
=
'test cost'
)
test_cost_kpi
=
CostKpi
(
'test_cost'
,
0.02
,
0
,
actived
=
True
,
desc
=
'test cost'
)
test_acc_kpi
=
AccKpi
(
'test_acc'
,
0.02
,
0
,
actived
=
True
,
desc
=
'test acc'
)
test_acc_kpi
=
AccKpi
(
'test_acc'
,
0.02
,
0
,
actived
=
True
,
desc
=
'test acc'
)
tracking_kpis
=
[
train_cost_kpi
,
test_cost_kpi
,
test_acc_kpi
]
tracking_kpis
=
[
train_cost_kpi
,
test_cost_kpi
,
test_acc_kpi
]
def
parse_log
(
log
):
def
parse_log
(
log
):
for
line
in
log
.
split
(
'
\n
'
):
for
line
in
log
.
split
(
'
\n
'
):
fs
=
line
.
strip
().
split
(
'
\t
'
)
fs
=
line
.
strip
().
split
(
'
\t
'
)
print
(
fs
)
print
(
fs
)
if
len
(
fs
)
==
3
and
fs
[
0
]
==
'kpis'
:
if
len
(
fs
)
==
3
and
fs
[
0
]
==
'kpis'
:
kpi_name
=
fs
[
1
]
kpi_name
=
fs
[
1
]
kpi_value
=
float
(
fs
[
2
])
kpi_value
=
float
(
fs
[
2
])
yield
kpi_name
,
kpi_value
yield
kpi_name
,
kpi_value
def
log_to_ce
(
log
):
def
log_to_ce
(
log
):
kpi_tracker
=
{}
kpi_tracker
=
{}
for
kpi
in
tracking_kpis
:
for
kpi
in
tracking_kpis
:
kpi_tracker
[
kpi
.
name
]
=
kpi
kpi_tracker
[
kpi
.
name
]
=
kpi
for
(
kpi_name
,
kpi_value
)
in
parse_log
(
log
):
for
(
kpi_name
,
kpi_value
)
in
parse_log
(
log
):
print
(
kpi_name
,
kpi_value
)
print
(
kpi_name
,
kpi_value
)
kpi_tracker
[
kpi_name
].
add_record
(
kpi_value
)
kpi_tracker
[
kpi_name
].
add_record
(
kpi_value
)
kpi_tracker
[
kpi_name
].
persist
()
kpi_tracker
[
kpi_name
].
persist
()
if
__name__
==
'__main__'
:
log
=
sys
.
stdin
.
read
()
log_to_ce
(
log
)
if
__name__
==
'__main__'
:
log
=
sys
.
stdin
.
read
()
log_to_ce
(
log
)
02.recognize_digits/train.py
浏览文件 @
23416e81
...
@@ -21,14 +21,24 @@ import numpy
...
@@ -21,14 +21,24 @@ import numpy
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
def
parse_args
():
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
"mnist"
)
parser
=
argparse
.
ArgumentParser
(
"mnist"
)
parser
.
add_argument
(
'--enable_ce'
,
action
=
'store_true'
,
help
=
"If set, run the task with continuous evaluation logs."
)
parser
.
add_argument
(
parser
.
add_argument
(
'--use_gpu'
,
type
=
bool
,
default
=
False
,
help
=
"Whether to use GPU or not."
)
'--enable_ce'
,
parser
.
add_argument
(
'--num_epochs'
,
type
=
int
,
default
=
5
,
help
=
"number of epochs."
)
action
=
'store_true'
,
args
=
parser
.
parse_args
()
help
=
"If set, run the task with continuous evaluation logs."
)
parser
.
add_argument
(
'--use_gpu'
,
type
=
bool
,
default
=
False
,
help
=
"Whether to use GPU or not."
)
parser
.
add_argument
(
'--num_epochs'
,
type
=
int
,
default
=
5
,
help
=
"number of epochs."
)
args
=
parser
.
parse_args
()
return
args
return
args
def
loss_net
(
hidden
,
label
):
def
loss_net
(
hidden
,
label
):
prediction
=
fluid
.
layers
.
fc
(
input
=
hidden
,
size
=
10
,
act
=
'softmax'
)
prediction
=
fluid
.
layers
.
fc
(
input
=
hidden
,
size
=
10
,
act
=
'softmax'
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
...
@@ -73,18 +83,21 @@ def train(nn_type,
...
@@ -73,18 +83,21 @@ def train(nn_type,
params_filename
=
None
):
params_filename
=
None
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
return
startup_program
=
fluid
.
default_startup_program
()
startup_program
=
fluid
.
default_startup_program
()
main_program
=
fluid
.
default_main_program
()
main_program
=
fluid
.
default_main_program
()
if
args
.
enable_ce
:
if
args
.
enable_ce
:
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
train
(),
batch_size
=
BATCH_SIZE
)
train_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
paddle
.
dataset
.
mnist
.
train
(),
batch_size
=
BATCH_SIZE
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
startup_program
.
random_seed
=
90
startup_program
.
random_seed
=
90
main_program
.
random_seed
=
90
main_program
.
random_seed
=
90
else
:
else
:
train_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
mnist
.
train
(),
buf_size
=
500
),
batch_size
=
BATCH_SIZE
)
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
mnist
.
train
(),
buf_size
=
500
),
batch_size
=
BATCH_SIZE
)
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
...
@@ -122,7 +135,7 @@ def train(nn_type,
...
@@ -122,7 +135,7 @@ def train(nn_type,
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
exe
=
fluid
.
Executor
(
place
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
img
,
label
],
place
=
place
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
img
,
label
],
place
=
place
)
exe
.
run
(
startup_program
)
exe
.
run
(
startup_program
)
epochs
=
[
epoch_id
for
epoch_id
in
range
(
PASS_NUM
)]
epochs
=
[
epoch_id
for
epoch_id
in
range
(
PASS_NUM
)]
...
@@ -154,17 +167,17 @@ def train(nn_type,
...
@@ -154,17 +167,17 @@ def train(nn_type,
exe
,
exe
,
model_filename
=
model_filename
,
model_filename
=
model_filename
,
params_filename
=
params_filename
)
params_filename
=
params_filename
)
if
args
.
enable_ce
:
if
args
.
enable_ce
:
print
(
"kpis
\t
train_cost
\t
%f"
%
metrics
[
0
]
)
print
(
"kpis
\t
train_cost
\t
%f"
%
metrics
[
0
])
print
(
"kpis
\t
test_cost
\t
%s"
%
avg_loss_val
)
print
(
"kpis
\t
test_cost
\t
%s"
%
avg_loss_val
)
print
(
"kpis
\t
test_acc
\t
%s"
%
acc_val
)
print
(
"kpis
\t
test_acc
\t
%s"
%
acc_val
)
# find the best pass
# find the best pass
best
=
sorted
(
lists
,
key
=
lambda
list
:
float
(
list
[
1
]))[
0
]
best
=
sorted
(
lists
,
key
=
lambda
list
:
float
(
list
[
1
]))[
0
]
print
(
'Best pass is %s, testing Avgcost is %s'
%
(
best
[
0
],
best
[
1
]))
print
(
'Best pass is %s, testing Avgcost is %s'
%
(
best
[
0
],
best
[
1
]))
print
(
'The classification accuracy is %.2f%%'
%
(
float
(
best
[
2
])
*
100
))
print
(
'The classification accuracy is %.2f%%'
%
(
float
(
best
[
2
])
*
100
))
def
infer
(
use_cuda
,
def
infer
(
use_cuda
,
save_dirname
=
None
,
save_dirname
=
None
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录