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
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'
)
tracking_kpis
=
[
train_cost_kpi
,
test_cost_kpi
]
tracking_kpis
=
[
train_cost_kpi
,
test_cost_kpi
]
def
parse_log
(
log
):
for
line
in
log
.
split
(
'
\n
'
):
fs
=
line
.
strip
().
split
(
'
\t
'
)
print
(
fs
)
if
len
(
fs
)
==
3
and
fs
[
0
]
==
'kpis'
:
print
(
"-----%s"
%
fs
)
kpi_name
=
fs
[
1
]
kpi_value
=
float
(
fs
[
2
])
yield
kpi_name
,
kpi_value
if
len
(
fs
)
==
3
and
fs
[
0
]
==
'kpis'
:
print
(
"-----%s"
%
fs
)
kpi_name
=
fs
[
1
]
kpi_value
=
float
(
fs
[
2
])
yield
kpi_name
,
kpi_value
def
log_to_ce
(
log
):
kpi_tracker
=
{}
for
kpi
in
tracking_kpis
:
kpi_tracker
[
kpi
.
name
]
=
kpi
kpi_tracker
[
kpi
.
name
]
=
kpi
for
(
kpi_name
,
kpi_value
)
in
parse_log
(
log
):
print
(
kpi_name
,
kpi_value
)
kpi_tracker
[
kpi_name
].
add_record
(
kpi_value
)
kpi_tracker
[
kpi_name
].
persist
()
print
(
kpi_name
,
kpi_value
)
kpi_tracker
[
kpi_name
].
add_record
(
kpi_value
)
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
import
paddle
import
paddle.fluid
as
fluid
def
parse_args
():
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
(
'--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."
)
parser
.
add_argument
(
'--enable_ce'
,
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
()
return
args
# For training test cost
def
train_test
(
executor
,
program
,
reader
,
feeder
,
fetch_list
):
accumulated
=
1
*
[
0
]
...
...
@@ -62,14 +72,18 @@ def main():
batch_size
=
20
if
args
.
enable_ce
:
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
uci_housing
.
train
(),
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
uci_housing
.
test
(),
batch_size
=
batch_size
)
else
:
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
uci_housing
.
train
(),
buf_size
=
500
),
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
uci_housing
.
train
(),
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
uci_housing
.
test
(),
batch_size
=
batch_size
)
else
:
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
uci_housing
.
train
(),
buf_size
=
500
),
batch_size
=
batch_size
)
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
)
# feature vector of length 13
...
...
@@ -78,11 +92,11 @@ def main():
main_program
=
fluid
.
default_main_program
()
startup_program
=
fluid
.
default_startup_program
()
if
args
.
enable_ce
:
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
)
cost
=
fluid
.
layers
.
square_error_cost
(
input
=
y_predict
,
label
=
y
)
avg_loss
=
fluid
.
layers
.
mean
(
cost
)
...
...
@@ -140,12 +154,13 @@ def main():
sys
.
exit
(
"got NaN loss, training failed."
)
if
params_dirname
is
not
None
:
# 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
:
print
(
"kpis
\t
train_cost
\t
%f"
%
avg_loss_value
[
0
])
print
(
"kpis
\t
test_cost
\t
%f"
%
test_metics
[
0
])
print
(
"kpis
\t
train_cost
\t
%f"
%
avg_loss_value
[
0
])
print
(
"kpis
\t
test_cost
\t
%f"
%
test_metics
[
0
])
infer_exe
=
fluid
.
Executor
(
place
)
inference_scope
=
fluid
.
core
.
Scope
()
...
...
@@ -182,5 +197,5 @@ def main():
if
__name__
==
'__main__'
:
args
=
parse_args
()
args
=
parse_args
()
main
()
02.recognize_digits/_ce.py
浏览文件 @
23416e81
...
...
@@ -10,28 +10,30 @@ from kpi import AccKpi
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_acc_kpi
=
AccKpi
(
'test_acc'
,
0.02
,
0
,
actived
=
True
,
desc
=
'test acc'
)
tracking_kpis
=
[
train_cost_kpi
,
test_cost_kpi
,
test_acc_kpi
]
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
]
def
parse_log
(
log
):
for
line
in
log
.
split
(
'
\n
'
):
fs
=
line
.
strip
().
split
(
'
\t
'
)
print
(
fs
)
if
len
(
fs
)
==
3
and
fs
[
0
]
==
'kpis'
:
kpi_name
=
fs
[
1
]
kpi_value
=
float
(
fs
[
2
])
yield
kpi_name
,
kpi_value
if
len
(
fs
)
==
3
and
fs
[
0
]
==
'kpis'
:
kpi_name
=
fs
[
1
]
kpi_value
=
float
(
fs
[
2
])
yield
kpi_name
,
kpi_value
def
log_to_ce
(
log
):
kpi_tracker
=
{}
for
kpi
in
tracking_kpis
:
kpi_tracker
[
kpi
.
name
]
=
kpi
kpi_tracker
[
kpi
.
name
]
=
kpi
for
(
kpi_name
,
kpi_value
)
in
parse_log
(
log
):
print
(
kpi_name
,
kpi_value
)
kpi_tracker
[
kpi_name
].
add_record
(
kpi_value
)
kpi_tracker
[
kpi_name
].
persist
()
print
(
kpi_name
,
kpi_value
)
kpi_tracker
[
kpi_name
].
add_record
(
kpi_value
)
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
import
paddle
import
paddle.fluid
as
fluid
def
parse_args
():
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
(
'--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
()
parser
.
add_argument
(
'--enable_ce'
,
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
=
5
,
help
=
"number of epochs."
)
args
=
parser
.
parse_args
()
return
args
def
loss_net
(
hidden
,
label
):
prediction
=
fluid
.
layers
.
fc
(
input
=
hidden
,
size
=
10
,
act
=
'softmax'
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
...
...
@@ -73,18 +83,21 @@ def train(nn_type,
params_filename
=
None
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
startup_program
=
fluid
.
default_startup_program
()
main_program
=
fluid
.
default_main_program
()
if
args
.
enable_ce
:
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
train
(),
batch_size
=
BATCH_SIZE
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
train_reader
=
paddle
.
batch
(
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
main_program
.
random_seed
=
90
else
:
else
:
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
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
...
...
@@ -122,7 +135,7 @@ def train(nn_type,
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
img
,
label
],
place
=
place
)
exe
.
run
(
startup_program
)
epochs
=
[
epoch_id
for
epoch_id
in
range
(
PASS_NUM
)]
...
...
@@ -154,17 +167,17 @@ def train(nn_type,
exe
,
model_filename
=
model_filename
,
params_filename
=
params_filename
)
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_acc
\t
%s"
%
acc_val
)
# find the best pass
best
=
sorted
(
lists
,
key
=
lambda
list
:
float
(
list
[
1
]))[
0
]
print
(
'Best pass is %s, testing Avgcost is %s'
%
(
best
[
0
],
best
[
1
]))
print
(
'The classification accuracy is %.2f%%'
%
(
float
(
best
[
2
])
*
100
))
def
infer
(
use_cuda
,
save_dirname
=
None
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录