Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
e104e706
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e104e706
编写于
9月 18, 2018
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update Unittests
上级
e704aa2c
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
170 addition
and
51 deletion
+170
-51
python/paddle/fluid/tests/book/high-level-api/fit_a_line/test_fit_a_line.py
...d/tests/book/high-level-api/fit_a_line/test_fit_a_line.py
+13
-3
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_resnet.py
.../image_classification/test_image_classification_resnet.py
+15
-4
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_vgg.py
...api/image_classification/test_image_classification_vgg.py
+15
-4
python/paddle/fluid/tests/book/high-level-api/label_semantic_roles/test_label_semantic_roles_newapi.py
.../label_semantic_roles/test_label_semantic_roles_newapi.py
+14
-4
python/paddle/fluid/tests/book/high-level-api/machine_translation/test_machine_translation.py
...level-api/machine_translation/test_machine_translation.py
+14
-3
python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_conv.py
...-level-api/recognize_digits/test_recognize_digits_conv.py
+16
-8
python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_mlp.py
...h-level-api/recognize_digits/test_recognize_digits_mlp.py
+15
-7
python/paddle/fluid/tests/book/high-level-api/recommender_system/test_recommender_system_newapi.py
...-api/recommender_system/test_recommender_system_newapi.py
+13
-3
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_conv.py
...pi/understand_sentiment/test_understand_sentiment_conv.py
+14
-4
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_dynamic_rnn.py
...rstand_sentiment/test_understand_sentiment_dynamic_rnn.py
+14
-4
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_stacked_lstm.py
...stand_sentiment/test_understand_sentiment_stacked_lstm.py
+14
-4
python/paddle/fluid/tests/book/high-level-api/word2vec/test_word2vec_new_api.py
...sts/book/high-level-api/word2vec/test_word2vec_new_api.py
+13
-3
未找到文件。
python/paddle/fluid/tests/book/high-level-api/fit_a_line/test_fit_a_line.py
浏览文件 @
e104e706
...
@@ -16,6 +16,16 @@ from __future__ import print_function
...
@@ -16,6 +16,16 @@ from __future__ import print_function
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
sys
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
contextlib
import
contextlib
import
numpy
import
numpy
import
unittest
import
unittest
...
@@ -57,11 +67,11 @@ def optimizer_func():
...
@@ -57,11 +67,11 @@ def optimizer_func():
def
train
(
use_cuda
,
train_program
,
params_dirname
,
inference_model_dirname
):
def
train
(
use_cuda
,
train_program
,
params_dirname
,
inference_model_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
train_program
,
place
=
place
,
optimizer_func
=
optimizer_func
)
train_func
=
train_program
,
place
=
place
,
optimizer_func
=
optimizer_func
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndStepEvent
):
if
isinstance
(
event
,
EndStepEvent
):
if
event
.
step
==
10
:
if
event
.
step
==
10
:
test_metrics
=
trainer
.
test
(
test_metrics
=
trainer
.
test
(
reader
=
test_reader
,
feed_order
=
[
'x'
,
'y'
])
reader
=
test_reader
,
feed_order
=
[
'x'
,
'y'
])
...
@@ -91,7 +101,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
...
@@ -91,7 +101,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
return
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
infer_func
=
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
batch_size
=
10
batch_size
=
10
...
...
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_resnet.py
浏览文件 @
e104e706
...
@@ -14,11 +14,22 @@
...
@@ -14,11 +14,22 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
sys
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
import
numpy
import
numpy
import
six
import
os
import
os
import
cifar10_small_test_set
import
cifar10_small_test_set
...
@@ -106,7 +117,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
...
@@ -106,7 +117,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
BATCH_SIZE
,
drop_last
=
False
)
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
BATCH_SIZE
,
drop_last
=
False
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndStepEvent
):
if
isinstance
(
event
,
EndStepEvent
):
avg_cost
,
accuracy
=
trainer
.
test
(
avg_cost
,
accuracy
=
trainer
.
test
(
reader
=
test_reader
,
feed_order
=
[
'pixel'
,
'label'
])
reader
=
test_reader
,
feed_order
=
[
'pixel'
,
'label'
])
...
@@ -118,7 +129,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
...
@@ -118,7 +129,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
return
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
train_program
,
train_func
=
train_program
,
optimizer_func
=
optimizer_func
,
optimizer_func
=
optimizer_func
,
place
=
place
,
place
=
place
,
...
@@ -133,7 +144,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
...
@@ -133,7 +144,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
def
infer
(
use_cuda
,
inference_program
,
parallel
,
params_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
parallel
,
params_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
inference_program
,
infer_func
=
inference_program
,
param_path
=
params_dirname
,
param_path
=
params_dirname
,
place
=
place
,
place
=
place
,
...
...
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_vgg.py
浏览文件 @
e104e706
...
@@ -14,11 +14,22 @@
...
@@ -14,11 +14,22 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
sys
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
import
numpy
import
numpy
import
six
import
os
import
os
import
cifar10_small_test_set
import
cifar10_small_test_set
...
@@ -83,7 +94,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
...
@@ -83,7 +94,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
BATCH_SIZE
,
drop_last
=
False
)
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
BATCH_SIZE
,
drop_last
=
False
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndStepEvent
):
if
isinstance
(
event
,
EndStepEvent
):
avg_cost
,
accuracy
=
trainer
.
test
(
avg_cost
,
accuracy
=
trainer
.
test
(
reader
=
test_reader
,
feed_order
=
[
'pixel'
,
'label'
])
reader
=
test_reader
,
feed_order
=
[
'pixel'
,
'label'
])
...
@@ -95,7 +106,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
...
@@ -95,7 +106,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
return
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
train_program
,
train_func
=
train_program
,
place
=
place
,
place
=
place
,
optimizer_func
=
optimizer_func
,
optimizer_func
=
optimizer_func
,
...
@@ -110,7 +121,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
...
@@ -110,7 +121,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
def
infer
(
use_cuda
,
inference_program
,
parallel
,
params_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
parallel
,
params_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
inference_program
,
infer_func
=
inference_program
,
param_path
=
params_dirname
,
param_path
=
params_dirname
,
place
=
place
,
place
=
place
,
...
...
python/paddle/fluid/tests/book/high-level-api/label_semantic_roles/test_label_semantic_roles_newapi.py
浏览文件 @
e104e706
...
@@ -16,6 +16,16 @@ from __future__ import print_function
...
@@ -16,6 +16,16 @@ from __future__ import print_function
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
sys
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
numpy
as
np
import
numpy
as
np
WORD_DICT
,
VERB_DICT
,
LABEL_DICT
=
paddle
.
dataset
.
conll05
.
get_dict
()
WORD_DICT
,
VERB_DICT
,
LABEL_DICT
=
paddle
.
dataset
.
conll05
.
get_dict
()
...
@@ -149,7 +159,7 @@ def optimize_func():
...
@@ -149,7 +159,7 @@ def optimize_func():
def
train
(
use_cuda
,
train_program
,
params_dirname
):
def
train
(
use_cuda
,
train_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
train_program
,
place
=
place
,
optimizer_func
=
optimize_func
)
train_func
=
train_program
,
place
=
place
,
optimizer_func
=
optimize_func
)
feed_order
=
[
feed_order
=
[
...
@@ -164,7 +174,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -164,7 +174,7 @@ def train(use_cuda, train_program, params_dirname):
# place)
# place)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndEpochEvent
):
if
isinstance
(
event
,
EndEpochEvent
):
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
conll05
.
test
(),
batch_size
=
BATCH_SIZE
)
paddle
.
dataset
.
conll05
.
test
(),
batch_size
=
BATCH_SIZE
)
avg_cost_set
=
trainer
.
test
(
avg_cost_set
=
trainer
.
test
(
...
@@ -184,7 +194,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -184,7 +194,7 @@ def train(use_cuda, train_program, params_dirname):
if
math
.
isnan
(
float
(
avg_cost
)):
if
math
.
isnan
(
float
(
avg_cost
)):
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
elif
isinstance
(
event
,
fluid
.
EndStepEvent
):
elif
isinstance
(
event
,
EndStepEvent
):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
...
@@ -204,7 +214,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -204,7 +214,7 @@ def train(use_cuda, train_program, params_dirname):
def
infer
(
use_cuda
,
inference_program
,
params_dirname
):
def
infer
(
use_cuda
,
inference_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
# Setup input by creating LoDTensor to represent sequence of words.
# Setup input by creating LoDTensor to represent sequence of words.
...
...
python/paddle/fluid/tests/book/high-level-api/machine_translation/test_machine_translation.py
浏览文件 @
e104e706
...
@@ -13,17 +13,28 @@
...
@@ -13,17 +13,28 @@
# limitations under the License.
# limitations under the License.
from
__future__
import
print_function
from
__future__
import
print_function
import
contextlib
import
contextlib
import
sys
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
paddle.fluid.framework
as
framework
import
paddle.fluid.framework
as
framework
import
paddle.fluid.layers
as
pd
import
paddle.fluid.layers
as
pd
from
paddle.fluid.executor
import
Executor
from
paddle.fluid.executor
import
Executor
from
functools
import
partial
from
functools
import
partial
import
unittest
import
unittest
import
os
dict_size
=
30000
dict_size
=
30000
source_dict_dim
=
target_dict_dim
=
dict_size
source_dict_dim
=
target_dict_dim
=
dict_size
...
@@ -198,12 +209,12 @@ def train(use_cuda, is_sparse, is_local=True):
...
@@ -198,12 +209,12 @@ def train(use_cuda, is_sparse, is_local=True):
]
]
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndStepEvent
):
if
isinstance
(
event
,
EndStepEvent
):
print
(
'pass_id='
+
str
(
event
.
epoch
)
+
' batch='
+
str
(
event
.
step
))
print
(
'pass_id='
+
str
(
event
.
epoch
)
+
' batch='
+
str
(
event
.
step
))
if
event
.
step
==
10
:
if
event
.
step
==
10
:
trainer
.
stop
()
trainer
.
stop
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
partial
(
train_program
,
is_sparse
),
train_func
=
partial
(
train_program
,
is_sparse
),
place
=
place
,
place
=
place
,
optimizer_func
=
optimizer_func
)
optimizer_func
=
optimizer_func
)
...
...
python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_conv.py
浏览文件 @
e104e706
...
@@ -14,14 +14,22 @@
...
@@ -14,14 +14,22 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
argparse
import
sys
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
import
paddle
import
paddle
import
six
import
sys
import
numpy
import
numpy
import
unittest
import
math
import
math
import
sys
import
sys
import
os
import
os
...
@@ -68,14 +76,14 @@ def optimizer_func():
...
@@ -68,14 +76,14 @@ def optimizer_func():
def
train
(
use_cuda
,
train_program
,
parallel
,
params_dirname
):
def
train
(
use_cuda
,
train_program
,
parallel
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
train_program
,
train_func
=
train_program
,
place
=
place
,
place
=
place
,
optimizer_func
=
optimizer_func
,
optimizer_func
=
optimizer_func
,
parallel
=
parallel
)
parallel
=
parallel
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndEpochEvent
):
if
isinstance
(
event
,
EndEpochEvent
):
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
avg_cost
,
acc
=
trainer
.
test
(
avg_cost
,
acc
=
trainer
.
test
(
...
@@ -91,7 +99,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
...
@@ -91,7 +99,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
event
.
epoch
+
1
,
avg_cost
,
acc
))
event
.
epoch
+
1
,
avg_cost
,
acc
))
if
math
.
isnan
(
avg_cost
):
if
math
.
isnan
(
avg_cost
):
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
elif
isinstance
(
event
,
fluid
.
EndStepEvent
):
elif
isinstance
(
event
,
EndStepEvent
):
print
(
print
(
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
event
.
step
,
event
.
epoch
,
...
@@ -112,7 +120,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
...
@@ -112,7 +120,7 @@ def train(use_cuda, train_program, parallel, params_dirname):
def
infer
(
use_cuda
,
inference_program
,
parallel
,
params_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
parallel
,
params_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
inference_program
,
infer_func
=
inference_program
,
param_path
=
params_dirname
,
param_path
=
params_dirname
,
place
=
place
,
place
=
place
,
...
...
python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_mlp.py
浏览文件 @
e104e706
...
@@ -14,14 +14,22 @@
...
@@ -14,14 +14,22 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
argparse
import
sys
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
import
paddle
import
paddle
import
six
import
sys
import
numpy
import
numpy
import
unittest
import
math
import
math
import
sys
import
sys
import
os
import
os
...
@@ -55,14 +63,14 @@ def optimizer_func():
...
@@ -55,14 +63,14 @@ def optimizer_func():
def
train
(
use_cuda
,
train_program
,
params_dirname
,
parallel
):
def
train
(
use_cuda
,
train_program
,
params_dirname
,
parallel
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
train_program
,
train_func
=
train_program
,
place
=
place
,
place
=
place
,
optimizer_func
=
optimizer_func
,
optimizer_func
=
optimizer_func
,
parallel
=
parallel
)
parallel
=
parallel
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndEpochEvent
):
if
isinstance
(
event
,
EndEpochEvent
):
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
BATCH_SIZE
)
avg_cost
,
acc
=
trainer
.
test
(
avg_cost
,
acc
=
trainer
.
test
(
...
@@ -94,7 +102,7 @@ def train(use_cuda, train_program, params_dirname, parallel):
...
@@ -94,7 +102,7 @@ def train(use_cuda, train_program, params_dirname, parallel):
def
infer
(
use_cuda
,
inference_program
,
parallel
,
params_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
parallel
,
params_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
inference_program
,
infer_func
=
inference_program
,
param_path
=
params_dirname
,
param_path
=
params_dirname
,
place
=
place
,
place
=
place
,
...
...
python/paddle/fluid/tests/book/high-level-api/recommender_system/test_recommender_system_newapi.py
浏览文件 @
e104e706
...
@@ -19,6 +19,16 @@ import sys
...
@@ -19,6 +19,16 @@ import sys
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
sys
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
paddle.fluid.layers
as
layers
import
paddle.fluid.layers
as
layers
import
paddle.fluid.nets
as
nets
import
paddle.fluid.nets
as
nets
...
@@ -164,7 +174,7 @@ def optimizer_func():
...
@@ -164,7 +174,7 @@ def optimizer_func():
def
train
(
use_cuda
,
train_program
,
params_dirname
):
def
train
(
use_cuda
,
train_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
train_program
,
place
=
place
,
optimizer_func
=
optimizer_func
)
train_func
=
train_program
,
place
=
place
,
optimizer_func
=
optimizer_func
)
feed_order
=
[
feed_order
=
[
...
@@ -173,7 +183,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -173,7 +183,7 @@ def train(use_cuda, train_program, params_dirname):
]
]
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndStepEvent
):
if
isinstance
(
event
,
EndStepEvent
):
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
movielens
.
test
(),
batch_size
=
BATCH_SIZE
)
paddle
.
dataset
.
movielens
.
test
(),
batch_size
=
BATCH_SIZE
)
avg_cost_set
=
trainer
.
test
(
avg_cost_set
=
trainer
.
test
(
...
@@ -208,7 +218,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -208,7 +218,7 @@ def train(use_cuda, train_program, params_dirname):
def
infer
(
use_cuda
,
inference_program
,
params_dirname
):
def
infer
(
use_cuda
,
inference_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
# Use the first data from paddle.dataset.movielens.test() as input.
# Use the first data from paddle.dataset.movielens.test() as input.
...
...
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_conv.py
浏览文件 @
e104e706
...
@@ -16,6 +16,16 @@ from __future__ import print_function
...
@@ -16,6 +16,16 @@ from __future__ import print_function
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
sys
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
from
functools
import
partial
from
functools
import
partial
import
numpy
as
np
import
numpy
as
np
...
@@ -72,13 +82,13 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -72,13 +82,13 @@ def train(use_cuda, train_program, params_dirname):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
partial
(
train_program
,
word_dict
),
train_func
=
partial
(
train_program
,
word_dict
),
place
=
place
,
place
=
place
,
optimizer_func
=
optimizer_func
)
optimizer_func
=
optimizer_func
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndEpochEvent
):
if
isinstance
(
event
,
EndEpochEvent
):
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
BATCH_SIZE
)
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
BATCH_SIZE
)
avg_cost
,
acc
=
trainer
.
test
(
avg_cost
,
acc
=
trainer
.
test
(
...
@@ -96,7 +106,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -96,7 +106,7 @@ def train(use_cuda, train_program, params_dirname):
event
.
epoch
+
1
,
avg_cost
,
acc
))
event
.
epoch
+
1
,
avg_cost
,
acc
))
if
math
.
isnan
(
avg_cost
):
if
math
.
isnan
(
avg_cost
):
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
elif
isinstance
(
event
,
fluid
.
EndStepEvent
):
elif
isinstance
(
event
,
EndStepEvent
):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
...
@@ -119,7 +129,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
...
@@ -119,7 +129,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
partial
(
inference_program
,
word_dict
),
infer_func
=
partial
(
inference_program
,
word_dict
),
param_path
=
params_dirname
,
param_path
=
params_dirname
,
place
=
place
)
place
=
place
)
...
...
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_dynamic_rnn.py
浏览文件 @
e104e706
...
@@ -16,6 +16,16 @@ from __future__ import print_function
...
@@ -16,6 +16,16 @@ from __future__ import print_function
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
sys
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
from
functools
import
partial
from
functools
import
partial
import
numpy
as
np
import
numpy
as
np
...
@@ -87,13 +97,13 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -87,13 +97,13 @@ def train(use_cuda, train_program, params_dirname):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
partial
(
train_program
,
word_dict
),
train_func
=
partial
(
train_program
,
word_dict
),
place
=
place
,
place
=
place
,
optimizer_func
=
optimizer_func
)
optimizer_func
=
optimizer_func
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndEpochEvent
):
if
isinstance
(
event
,
EndEpochEvent
):
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
BATCH_SIZE
)
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
BATCH_SIZE
)
avg_cost
,
acc
=
trainer
.
test
(
avg_cost
,
acc
=
trainer
.
test
(
...
@@ -111,7 +121,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -111,7 +121,7 @@ def train(use_cuda, train_program, params_dirname):
event
.
epoch
+
1
,
avg_cost
,
acc
))
event
.
epoch
+
1
,
avg_cost
,
acc
))
if
math
.
isnan
(
avg_cost
):
if
math
.
isnan
(
avg_cost
):
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
elif
isinstance
(
event
,
fluid
.
EndStepEvent
):
elif
isinstance
(
event
,
EndStepEvent
):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
...
@@ -134,7 +144,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
...
@@ -134,7 +144,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
partial
(
inference_program
,
word_dict
),
infer_func
=
partial
(
inference_program
,
word_dict
),
param_path
=
params_dirname
,
param_path
=
params_dirname
,
place
=
place
)
place
=
place
)
...
...
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_stacked_lstm.py
浏览文件 @
e104e706
...
@@ -16,6 +16,16 @@ from __future__ import print_function
...
@@ -16,6 +16,16 @@ from __future__ import print_function
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
sys
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
from
functools
import
partial
from
functools
import
partial
import
numpy
as
np
import
numpy
as
np
...
@@ -79,13 +89,13 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -79,13 +89,13 @@ def train(use_cuda, train_program, params_dirname):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
partial
(
train_program
,
word_dict
),
train_func
=
partial
(
train_program
,
word_dict
),
place
=
place
,
place
=
place
,
optimizer_func
=
optimizer_func
)
optimizer_func
=
optimizer_func
)
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndEpochEvent
):
if
isinstance
(
event
,
EndEpochEvent
):
test_reader
=
paddle
.
batch
(
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
paddle
.
dataset
.
imdb
.
test
(
word_dict
),
batch_size
=
BATCH_SIZE
,
batch_size
=
BATCH_SIZE
,
...
@@ -105,7 +115,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -105,7 +115,7 @@ def train(use_cuda, train_program, params_dirname):
event
.
epoch
+
1
,
avg_cost
,
acc
))
event
.
epoch
+
1
,
avg_cost
,
acc
))
if
math
.
isnan
(
avg_cost
):
if
math
.
isnan
(
avg_cost
):
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
elif
isinstance
(
event
,
fluid
.
EndStepEvent
):
elif
isinstance
(
event
,
EndStepEvent
):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
...
@@ -129,7 +139,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
...
@@ -129,7 +139,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
partial
(
inference_program
,
word_dict
),
infer_func
=
partial
(
inference_program
,
word_dict
),
param_path
=
params_dirname
,
param_path
=
params_dirname
,
place
=
place
)
place
=
place
)
...
...
python/paddle/fluid/tests/book/high-level-api/word2vec/test_word2vec_new_api.py
浏览文件 @
e104e706
...
@@ -16,6 +16,16 @@ from __future__ import print_function
...
@@ -16,6 +16,16 @@ from __future__ import print_function
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
sys
try
:
from
paddle.fluid.contrib.trainer
import
*
from
paddle.fluid.contrib.inferencer
import
*
except
ImportError
:
print
(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib"
,
file
=
sys
.
stderr
)
from
paddle.fluid.trainer
import
*
from
paddle.fluid.inferencer
import
*
import
numpy
as
np
import
numpy
as
np
import
math
import
math
import
sys
import
sys
...
@@ -95,7 +105,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -95,7 +105,7 @@ def train(use_cuda, train_program, params_dirname):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
EndStepEvent
):
if
isinstance
(
event
,
EndStepEvent
):
outs
=
trainer
.
test
(
outs
=
trainer
.
test
(
reader
=
test_reader
,
reader
=
test_reader
,
feed_order
=
[
'firstw'
,
'secondw'
,
'thirdw'
,
'forthw'
,
'nextw'
])
feed_order
=
[
'firstw'
,
'secondw'
,
'thirdw'
,
'forthw'
,
'nextw'
])
...
@@ -109,7 +119,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -109,7 +119,7 @@ def train(use_cuda, train_program, params_dirname):
if
math
.
isnan
(
avg_cost
):
if
math
.
isnan
(
avg_cost
):
sys
.
exit
(
"got NaN loss, training failed."
)
sys
.
exit
(
"got NaN loss, training failed."
)
trainer
=
fluid
.
Trainer
(
trainer
=
Trainer
(
train_func
=
train_program
,
optimizer_func
=
optimizer_func
,
place
=
place
)
train_func
=
train_program
,
optimizer_func
=
optimizer_func
,
place
=
place
)
trainer
.
train
(
trainer
.
train
(
...
@@ -121,7 +131,7 @@ def train(use_cuda, train_program, params_dirname):
...
@@ -121,7 +131,7 @@ def train(use_cuda, train_program, params_dirname):
def
infer
(
use_cuda
,
inference_program
,
params_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inferencer
=
Inferencer
(
infer_func
=
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
infer_func
=
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
# Setup inputs by creating 4 LoDTensors representing 4 words. Here each word
# Setup inputs by creating 4 LoDTensors representing 4 words. Here each word
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录