Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
fb43c6b4
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
fb43c6b4
编写于
5月 25, 2018
作者:
S
Siddharth Goyal
提交者:
GitHub
5月 25, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix attribute name in new API (#10947)
上级
c79ec9f0
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
90 addition
and
83 deletion
+90
-83
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
+9
-9
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_resnet.py
.../image_classification/test_image_classification_resnet.py
+9
-7
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_vgg.py
...api/image_classification/test_image_classification_vgg.py
+9
-7
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
+8
-8
python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_conv.py
...-level-api/recognize_digits/test_recognize_digits_conv.py
+7
-7
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
+7
-7
python/paddle/fluid/tests/book/high-level-api/recommender_system/test_recommender_system_newapi.py
...-api/recommender_system/test_recommender_system_newapi.py
+10
-7
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_conv.py
...pi/understand_sentiment/test_understand_sentiment_conv.py
+8
-8
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_dynamic_rnn.py
...rstand_sentiment/test_understand_sentiment_dynamic_rnn.py
+8
-8
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_stacked_lstm.py
...stand_sentiment/test_understand_sentiment_stacked_lstm.py
+8
-8
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
+7
-7
未找到文件。
python/paddle/fluid/tests/book/high-level-api/fit_a_line/test_fit_a_line.py
浏览文件 @
fb43c6b4
...
...
@@ -48,7 +48,7 @@ def linear():
return
avg_loss
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
...
...
@@ -68,8 +68,8 @@ def train(use_cuda, train_program, save_dirname):
['15.343549569447836']
...
'''
if
save
_dirname
is
not
None
:
trainer
.
save_params
(
save
_dirname
)
if
params
_dirname
is
not
None
:
trainer
.
save_params
(
params
_dirname
)
trainer
.
stop
()
trainer
.
train
(
...
...
@@ -80,13 +80,13 @@ def train(use_cuda, train_program, save_dirname):
# infer
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
if
save
_dirname
is
None
:
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
if
params
_dirname
is
None
:
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save
_dirname
,
place
=
place
)
infer_func
=
inference_program
,
param_path
=
params
_dirname
,
place
=
place
)
batch_size
=
10
tensor_x
=
numpy
.
random
.
uniform
(
0
,
10
,
[
batch_size
,
13
]).
astype
(
"float32"
)
...
...
@@ -100,10 +100,10 @@ def main(use_cuda):
return
# Directory for saving the trained model
save
_dirname
=
"fit_a_line.inference.model"
params
_dirname
=
"fit_a_line.inference.model"
train
(
use_cuda
,
linear
,
save
_dirname
)
infer
(
use_cuda
,
inference_program
,
save
_dirname
)
train
(
use_cuda
,
linear
,
params
_dirname
)
infer
(
use_cuda
,
inference_program
,
params
_dirname
)
class
TestFitALine
(
unittest
.
TestCase
):
...
...
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_resnet.py
浏览文件 @
fb43c6b4
...
...
@@ -85,7 +85,7 @@ def train_network():
return
[
avg_cost
,
accuracy
]
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
BATCH_SIZE
=
128
EPOCH_NUM
=
1
...
...
@@ -105,8 +105,8 @@ def train(use_cuda, train_program, save_dirname):
print
(
'Loss {0:2.2}, Acc {1:2.2}'
.
format
(
avg_cost
,
accuracy
))
if
accuracy
>
0.01
:
# Low threshold for speeding up CI
if
save
_dirname
is
not
None
:
trainer
.
save_params
(
save
_dirname
)
if
params
_dirname
is
not
None
:
trainer
.
save_params
(
params
_dirname
)
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
...
...
@@ -122,10 +122,10 @@ def train(use_cuda, train_program, save_dirname):
feed_order
=
[
'pixel'
,
'label'
])
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save
_dirname
,
place
=
place
)
infer_func
=
inference_program
,
param_path
=
params
_dirname
,
place
=
place
)
# The input's dimension of conv should be 4-D or 5-D.
# Use normilized image pixels as input data, which should be in the range
...
...
@@ -142,12 +142,14 @@ def main(use_cuda):
save_path
=
"image_classification_resnet.inference.model"
train
(
use_cuda
=
use_cuda
,
train_program
=
train_network
,
save_dirname
=
save_path
)
use_cuda
=
use_cuda
,
train_program
=
train_network
,
params_dirname
=
save_path
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
inference_network
,
save
_dirname
=
save_path
)
params
_dirname
=
save_path
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_vgg.py
浏览文件 @
fb43c6b4
...
...
@@ -64,7 +64,7 @@ def train_network():
return
[
avg_cost
,
accuracy
]
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
BATCH_SIZE
=
128
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
...
...
@@ -82,8 +82,8 @@ def train(use_cuda, train_program, save_dirname):
print
(
'Loss {0:2.2}, Acc {1:2.2}'
.
format
(
avg_cost
,
accuracy
))
if
accuracy
>
0.01
:
# Low threshold for speeding up CI
if
save
_dirname
is
not
None
:
trainer
.
save_params
(
save
_dirname
)
if
params
_dirname
is
not
None
:
trainer
.
save_params
(
params
_dirname
)
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
...
...
@@ -99,10 +99,10 @@ def train(use_cuda, train_program, save_dirname):
feed_order
=
[
'pixel'
,
'label'
])
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save
_dirname
,
place
=
place
)
infer_func
=
inference_program
,
param_path
=
params
_dirname
,
place
=
place
)
# The input's dimension of conv should be 4-D or 5-D.
# Use normilized image pixels as input data, which should be in the range
...
...
@@ -119,12 +119,14 @@ def main(use_cuda):
save_path
=
"image_classification_vgg.inference.model"
train
(
use_cuda
=
use_cuda
,
train_program
=
train_network
,
save_dirname
=
save_path
)
use_cuda
=
use_cuda
,
train_program
=
train_network
,
params_dirname
=
save_path
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
inference_network
,
save
_dirname
=
save_path
)
params
_dirname
=
save_path
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/label_semantic_roles/test_label_semantic_roles_newapi.py
浏览文件 @
fb43c6b4
...
...
@@ -141,7 +141,7 @@ def train_program():
return
[
avg_cost
]
def
train
(
use_cuda
,
train_program
,
save_path
):
def
train
(
use_cuda
,
train_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.01
)
...
...
@@ -172,7 +172,7 @@ def train(use_cuda, train_program, save_path):
print
(
"avg_cost: %s"
%
avg_cost
)
if
float
(
avg_cost
)
<
100.0
:
# Large value to increase CI speed
trainer
.
save_params
(
save_path
)
trainer
.
save_params
(
params_dirname
)
else
:
print
(
'BatchID {0}, Test Loss {1:0.2}'
.
format
(
event
.
epoch
+
1
,
float
(
avg_cost
)))
...
...
@@ -183,7 +183,7 @@ def train(use_cuda, train_program, save_path):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
map
(
np
.
array
,
event
.
metrics
)))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
trainer
.
save_params
(
save_path
)
trainer
.
save_params
(
params_dirname
)
trainer
.
stop
()
train_reader
=
paddle
.
batch
(
...
...
@@ -197,10 +197,10 @@ def train(use_cuda, train_program, save_path):
feed_order
=
feed_order
)
def
infer
(
use_cuda
,
inference_program
,
save_path
):
def
infer
(
use_cuda
,
inference_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inference_program
,
param_path
=
save_path
,
place
=
place
)
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
# Setup inputs by creating LoDTensors to represent sequences of words.
# Here each word is the basic element of these LoDTensors and the shape of
...
...
@@ -251,9 +251,9 @@ def infer(use_cuda, inference_program, save_path):
def
main
(
use_cuda
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"label_semantic_roles.inference.model"
train
(
use_cuda
,
train_program
,
save_path
)
infer
(
use_cuda
,
inference_program
,
save_path
)
params_dirname
=
"label_semantic_roles.inference.model"
train
(
use_cuda
,
train_program
,
params_dirname
)
infer
(
use_cuda
,
inference_program
,
params_dirname
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_conv.py
浏览文件 @
fb43c6b4
...
...
@@ -57,7 +57,7 @@ def train_program():
return
[
avg_cost
,
acc
]
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.001
)
...
...
@@ -78,7 +78,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"acc : %s"
%
acc
)
if
acc
>
0.2
:
# Smaller value to increase CI speed
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
else
:
print
(
'BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'
.
format
(
event
.
epoch
+
1
,
avg_cost
,
acc
))
...
...
@@ -100,11 +100,11 @@ def train(use_cuda, train_program, save_dirname):
feed_order
=
[
'img'
,
'label'
])
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save
_dirname
,
place
=
place
)
infer_func
=
inference_program
,
param_path
=
params
_dirname
,
place
=
place
)
batch_size
=
1
tensor_img
=
numpy
.
random
.
uniform
(
-
1.0
,
1.0
,
...
...
@@ -116,17 +116,17 @@ def infer(use_cuda, inference_program, save_dirname=None):
def
main
(
use_cuda
):
save
_dirname
=
"recognize_digits_conv.inference.model"
params
_dirname
=
"recognize_digits_conv.inference.model"
# call train() with is_local argument to run distributed train
train
(
use_cuda
=
use_cuda
,
train_program
=
train_program
,
save_dirname
=
save
_dirname
)
params_dirname
=
params
_dirname
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
inference_program
,
save_dirname
=
save
_dirname
)
params_dirname
=
params
_dirname
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_mlp.py
浏览文件 @
fb43c6b4
...
...
@@ -44,7 +44,7 @@ def train_program():
return
[
avg_cost
,
acc
]
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.001
)
...
...
@@ -62,7 +62,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"acc : %s"
%
acc
)
if
acc
>
0.2
:
# Smaller value to increase CI speed
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
else
:
print
(
'BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'
.
format
(
event
.
epoch
+
1
,
avg_cost
,
acc
))
...
...
@@ -81,11 +81,11 @@ def train(use_cuda, train_program, save_dirname):
feed_order
=
[
'img'
,
'label'
])
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save
_dirname
,
place
=
place
)
infer_func
=
inference_program
,
param_path
=
params
_dirname
,
place
=
place
)
batch_size
=
1
tensor_img
=
numpy
.
random
.
uniform
(
-
1.0
,
1.0
,
...
...
@@ -97,17 +97,17 @@ def infer(use_cuda, inference_program, save_dirname=None):
def
main
(
use_cuda
):
save
_dirname
=
"recognize_digits_mlp.inference.model"
params
_dirname
=
"recognize_digits_mlp.inference.model"
# call train() with is_local argument to run distributed train
train
(
use_cuda
=
use_cuda
,
train_program
=
train_program
,
save_dirname
=
save
_dirname
)
params_dirname
=
params
_dirname
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
inference_program
,
save_dirname
=
save
_dirname
)
params_dirname
=
params
_dirname
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/recommender_system/test_recommender_system_newapi.py
浏览文件 @
fb43c6b4
...
...
@@ -155,7 +155,7 @@ def train_program():
return
[
avg_cost
,
scale_infer
]
def
train
(
use_cuda
,
train_program
,
save_path
):
def
train
(
use_cuda
,
train_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.2
)
...
...
@@ -180,7 +180,7 @@ def train(use_cuda, train_program, save_path):
print
(
"avg_cost: %s"
%
avg_cost
)
if
float
(
avg_cost
)
<
4
:
# Smaller value to increase CI speed
trainer
.
save_params
(
save_path
)
trainer
.
save_params
(
params_dirname
)
trainer
.
stop
()
else
:
print
(
'BatchID {0}, Test Loss {1:0.2}'
.
format
(
event
.
epoch
+
1
,
...
...
@@ -200,10 +200,10 @@ def train(use_cuda, train_program, save_path):
feed_order
=
feed_order
)
def
infer
(
use_cuda
,
inference_program
,
save_path
):
def
infer
(
use_cuda
,
inference_program
,
params_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inference_program
,
param_path
=
save_path
,
place
=
place
)
inference_program
,
param_path
=
params_dirname
,
place
=
place
)
# Use the first data from paddle.dataset.movielens.test() as input.
# Use create_lod_tensor(data, lod, place) API to generate LoD Tensor,
...
...
@@ -240,12 +240,15 @@ def infer(use_cuda, inference_program, save_path):
def
main
(
use_cuda
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"recommender_system.inference.model"
train
(
use_cuda
=
use_cuda
,
train_program
=
train_program
,
save_path
=
save_path
)
params_dirname
=
"recommender_system.inference.model"
train
(
use_cuda
=
use_cuda
,
train_program
=
train_program
,
params_dirname
=
params_dirname
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
inference_program
,
save_path
=
save_path
)
params_dirname
=
params_dirname
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_conv.py
浏览文件 @
fb43c6b4
...
...
@@ -64,7 +64,7 @@ def train_program(word_dict):
return
[
avg_cost
,
accuracy
]
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
0.002
)
...
...
@@ -85,7 +85,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"acc : %s"
%
acc
)
if
acc
>
0.2
:
# Smaller value to increase CI speed
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
trainer
.
stop
()
else
:
...
...
@@ -97,7 +97,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
map
(
np
.
array
,
event
.
metrics
)))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
trainer
.
stop
()
train_reader
=
paddle
.
batch
(
...
...
@@ -112,13 +112,13 @@ def train(use_cuda, train_program, save_dirname):
feed_order
=
[
'words'
,
'label'
])
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
partial
(
inference_program
,
word_dict
),
param_path
=
save
_dirname
,
param_path
=
params
_dirname
,
place
=
place
)
# Setup input by creating LoDTensor to represent sequence of words.
...
...
@@ -143,9 +143,9 @@ def infer(use_cuda, inference_program, save_dirname=None):
def
main
(
use_cuda
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"understand_sentiment_conv.inference.model"
train
(
use_cuda
,
train_program
,
save_path
)
infer
(
use_cuda
,
inference_program
,
save_path
)
params_dirname
=
"understand_sentiment_conv.inference.model"
train
(
use_cuda
,
train_program
,
params_dirname
)
infer
(
use_cuda
,
inference_program
,
params_dirname
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_dynamic_rnn.py
浏览文件 @
fb43c6b4
...
...
@@ -79,7 +79,7 @@ def train_program(word_dict):
return
[
avg_cost
,
accuracy
]
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
0.002
)
...
...
@@ -100,7 +100,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"acc : %s"
%
acc
)
if
acc
>
0.2
:
# Smaller value to increase CI speed
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
trainer
.
stop
()
else
:
...
...
@@ -112,7 +112,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
map
(
np
.
array
,
event
.
metrics
)))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
trainer
.
stop
()
train_reader
=
paddle
.
batch
(
...
...
@@ -127,13 +127,13 @@ def train(use_cuda, train_program, save_dirname):
feed_order
=
[
'words'
,
'label'
])
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
partial
(
inference_program
,
word_dict
),
param_path
=
save
_dirname
,
param_path
=
params
_dirname
,
place
=
place
)
# Setup input by creating LoDTensor to represent sequence of words.
...
...
@@ -158,9 +158,9 @@ def infer(use_cuda, inference_program, save_dirname=None):
def
main
(
use_cuda
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"understand_sentiment_conv.inference.model"
train
(
use_cuda
,
train_program
,
save_path
)
infer
(
use_cuda
,
inference_program
,
save_path
)
params_dirname
=
"understand_sentiment_conv.inference.model"
train
(
use_cuda
,
train_program
,
params_dirname
)
infer
(
use_cuda
,
inference_program
,
params_dirname
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/understand_sentiment/test_understand_sentiment_stacked_lstm.py
浏览文件 @
fb43c6b4
...
...
@@ -71,7 +71,7 @@ def train_program(word_dict):
return
[
avg_cost
,
accuracy
]
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
optimizer
=
fluid
.
optimizer
.
Adagrad
(
learning_rate
=
0.002
)
...
...
@@ -92,7 +92,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"acc : %s"
%
acc
)
if
acc
>
0.2
:
# Smaller value to increase CI speed
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
trainer
.
stop
()
else
:
...
...
@@ -104,7 +104,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
map
(
np
.
array
,
event
.
metrics
)))
if
event
.
step
==
1
:
# Run 2 iterations to speed CI
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
trainer
.
stop
()
train_reader
=
paddle
.
batch
(
...
...
@@ -119,13 +119,13 @@ def train(use_cuda, train_program, save_dirname):
feed_order
=
[
'words'
,
'label'
])
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
word_dict
=
paddle
.
dataset
.
imdb
.
word_dict
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
partial
(
inference_program
,
word_dict
),
param_path
=
save
_dirname
,
param_path
=
params
_dirname
,
place
=
place
)
# Setup input by creating LoDTensor to represent sequence of words.
...
...
@@ -150,9 +150,9 @@ def infer(use_cuda, inference_program, save_dirname=None):
def
main
(
use_cuda
):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"understand_sentiment_stacked_lstm.inference.model"
train
(
use_cuda
,
train_program
,
save_path
)
infer
(
use_cuda
,
inference_program
,
save_path
)
params_dirname
=
"understand_sentiment_stacked_lstm.inference.model"
train
(
use_cuda
,
train_program
,
params_dirname
)
infer
(
use_cuda
,
inference_program
,
params_dirname
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/word2vec/test_word2vec_new_api.py
浏览文件 @
fb43c6b4
...
...
@@ -80,7 +80,7 @@ def train_program(is_sparse):
return
avg_cost
def
train
(
use_cuda
,
train_program
,
save
_dirname
):
def
train
(
use_cuda
,
train_program
,
params
_dirname
):
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
imikolov
.
train
(
word_dict
,
N
),
BATCH_SIZE
)
test_reader
=
paddle
.
batch
(
...
...
@@ -97,7 +97,7 @@ def train(use_cuda, train_program, save_dirname):
print
(
"loss= "
,
avg_cost
)
if
avg_cost
<
10.0
:
trainer
.
save_params
(
save
_dirname
)
trainer
.
save_params
(
params
_dirname
)
trainer
.
stop
()
if
math
.
isnan
(
avg_cost
):
...
...
@@ -115,10 +115,10 @@ def train(use_cuda, train_program, save_dirname):
feed_order
=
[
'firstw'
,
'secondw'
,
'thirdw'
,
'forthw'
,
'nextw'
])
def
infer
(
use_cuda
,
inference_program
,
save
_dirname
=
None
):
def
infer
(
use_cuda
,
inference_program
,
params
_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save
_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
# is simply an index to look up for the corresponding word vector and hence
...
...
@@ -153,17 +153,17 @@ def main(use_cuda, is_sparse):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"word2vec.inference.model"
params_dirname
=
"word2vec.inference.model"
train
(
use_cuda
=
use_cuda
,
train_program
=
partial
(
train_program
,
is_sparse
),
save_dirname
=
save_path
)
params_dirname
=
params_dirname
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
partial
(
inference_program
,
is_sparse
),
save_dirname
=
save_path
)
params_dirname
=
params_dirname
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录