Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
51d4afaa
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
51d4afaa
编写于
11月 04, 2017
作者:
Y
Yu Yang
提交者:
GitHub
11月 04, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename program->main_program, init_program->startup_program (#5360)
上级
e745bcfc
变更
24
隐藏空白更改
内联
并排
Showing
24 changed file
with
486 addition
and
418 deletion
+486
-418
python/paddle/v2/framework/framework.py
python/paddle/v2/framework/framework.py
+2
-2
python/paddle/v2/framework/io.py
python/paddle/v2/framework/io.py
+35
-29
python/paddle/v2/framework/layer_helper.py
python/paddle/v2/framework/layer_helper.py
+15
-15
python/paddle/v2/framework/layers.py
python/paddle/v2/framework/layers.py
+30
-29
python/paddle/v2/framework/net_drawer.py
python/paddle/v2/framework/net_drawer.py
+3
-3
python/paddle/v2/framework/nets.py
python/paddle/v2/framework/nets.py
+22
-22
python/paddle/v2/framework/optimizer.py
python/paddle/v2/framework/optimizer.py
+7
-5
python/paddle/v2/framework/tests/test_executor_and_mul.py
python/paddle/v2/framework/tests/test_executor_and_mul.py
+2
-2
python/paddle/v2/framework/tests/test_fit_a_line.py
python/paddle/v2/framework/tests/test_fit_a_line.py
+20
-16
python/paddle/v2/framework/tests/test_image_classification_layer.py
...dle/v2/framework/tests/test_image_classification_layer.py
+35
-31
python/paddle/v2/framework/tests/test_image_classification_train.py
...dle/v2/framework/tests/test_image_classification_train.py
+66
-50
python/paddle/v2/framework/tests/test_inference_model_io.py
python/paddle/v2/framework/tests/test_inference_model_io.py
+10
-10
python/paddle/v2/framework/tests/test_layers.py
python/paddle/v2/framework/tests/test_layers.py
+53
-36
python/paddle/v2/framework/tests/test_lod_rank_table.py
python/paddle/v2/framework/tests/test_lod_rank_table.py
+2
-2
python/paddle/v2/framework/tests/test_operator_desc.py
python/paddle/v2/framework/tests/test_operator_desc.py
+2
-2
python/paddle/v2/framework/tests/test_parameter.py
python/paddle/v2/framework/tests/test_parameter.py
+2
-2
python/paddle/v2/framework/tests/test_program.py
python/paddle/v2/framework/tests/test_program.py
+9
-9
python/paddle/v2/framework/tests/test_recognize_digits_conv.py
...n/paddle/v2/framework/tests/test_recognize_digits_conv.py
+25
-19
python/paddle/v2/framework/tests/test_recognize_digits_mlp.py
...on/paddle/v2/framework/tests/test_recognize_digits_mlp.py
+25
-18
python/paddle/v2/framework/tests/test_recommender_system.py
python/paddle/v2/framework/tests/test_recommender_system.py
+66
-64
python/paddle/v2/framework/tests/test_recurrent_op.py
python/paddle/v2/framework/tests/test_recurrent_op.py
+16
-14
python/paddle/v2/framework/tests/test_understand_sentiment_conv.py
...ddle/v2/framework/tests/test_understand_sentiment_conv.py
+3
-3
python/paddle/v2/framework/tests/test_variable.py
python/paddle/v2/framework/tests/test_variable.py
+2
-2
python/paddle/v2/framework/tests/test_word2vec.py
python/paddle/v2/framework/tests/test_word2vec.py
+34
-33
未找到文件。
python/paddle/v2/framework/framework.py
浏览文件 @
51d4afaa
...
@@ -550,5 +550,5 @@ class Parameter(Variable):
...
@@ -550,5 +550,5 @@ class Parameter(Variable):
# program is a global instance.
# program is a global instance.
g_program
=
Program
()
g_
main_
program
=
Program
()
g_
init
_program
=
Program
()
g_
startup
_program
=
Program
()
python/paddle/v2/framework/io.py
浏览文件 @
51d4afaa
import
os
import
os
import
cPickle
as
pickle
import
cPickle
as
pickle
from
paddle.v2.framework.framework
import
Program
,
Parameter
,
g_program
,
\
from
paddle.v2.framework.framework
import
Program
,
Parameter
,
g_
main_
program
,
\
Variable
Variable
__all__
=
[
__all__
=
[
...
@@ -29,13 +29,13 @@ def _clone_var_in_block_(block, var):
...
@@ -29,13 +29,13 @@ def _clone_var_in_block_(block, var):
persistable
=
True
)
persistable
=
True
)
def
save_vars
(
executor
,
dirname
,
program
=
None
,
vars
=
None
,
predicate
=
None
):
def
save_vars
(
executor
,
dirname
,
main_
program
=
None
,
vars
=
None
,
predicate
=
None
):
"""
"""
Save variables to directory by executor.
Save variables to directory by executor.
:param executor: executor that save variable
:param executor: executor that save variable
:param dirname: directory path
:param dirname: directory path
:param program: program. If vars is None, then filter all variables in this
:param
main_
program: program. If vars is None, then filter all variables in this
program which fit `predicate`. Default g_program.
program which fit `predicate`. Default g_program.
:param predicate: The Predicate describes a callable that returns a variable
:param predicate: The Predicate describes a callable that returns a variable
as a bool. If it returns true, the variables will be saved.
as a bool. If it returns true, the variables will be saved.
...
@@ -44,15 +44,15 @@ def save_vars(executor, dirname, program=None, vars=None, predicate=None):
...
@@ -44,15 +44,15 @@ def save_vars(executor, dirname, program=None, vars=None, predicate=None):
:return: None
:return: None
"""
"""
if
vars
is
None
:
if
vars
is
None
:
if
program
is
None
:
if
main_
program
is
None
:
program
=
g
_program
main_program
=
g_main
_program
if
not
isinstance
(
program
,
Program
):
if
not
isinstance
(
main_
program
,
Program
):
raise
TypeError
(
"program should be as Program type or None"
)
raise
TypeError
(
"program should be as Program type or None"
)
save_vars
(
save_vars
(
executor
,
executor
,
dirname
=
dirname
,
dirname
=
dirname
,
vars
=
filter
(
predicate
,
program
.
list_vars
()))
vars
=
filter
(
predicate
,
main_
program
.
list_vars
()))
else
:
else
:
save_program
=
Program
()
save_program
=
Program
()
save_block
=
save_program
.
global_block
()
save_block
=
save_program
.
global_block
()
...
@@ -66,37 +66,37 @@ def save_vars(executor, dirname, program=None, vars=None, predicate=None):
...
@@ -66,37 +66,37 @@ def save_vars(executor, dirname, program=None, vars=None, predicate=None):
executor
.
run
(
save_program
)
executor
.
run
(
save_program
)
def
save_params
(
executor
,
dirname
,
program
=
None
):
def
save_params
(
executor
,
dirname
,
main_
program
=
None
):
"""
"""
Save all parameters to directory with executor.
Save all parameters to directory with executor.
"""
"""
save_vars
(
save_vars
(
executor
,
executor
,
dirname
=
dirname
,
dirname
=
dirname
,
program
=
program
,
main_program
=
main_
program
,
vars
=
None
,
vars
=
None
,
predicate
=
is_parameter
)
predicate
=
is_parameter
)
def
save_persistables
(
executor
,
dirname
,
program
=
None
):
def
save_persistables
(
executor
,
dirname
,
main_
program
=
None
):
"""
"""
Save all persistables to directory with executor.
Save all persistables to directory with executor.
"""
"""
save_vars
(
save_vars
(
executor
,
executor
,
dirname
=
dirname
,
dirname
=
dirname
,
program
=
program
,
main_program
=
main_
program
,
vars
=
None
,
vars
=
None
,
predicate
=
is_persistable
)
predicate
=
is_persistable
)
def
load_vars
(
executor
,
dirname
,
program
=
None
,
vars
=
None
,
predicate
=
None
):
def
load_vars
(
executor
,
dirname
,
main_
program
=
None
,
vars
=
None
,
predicate
=
None
):
"""
"""
Load variables from directory by executor.
Load variables from directory by executor.
:param executor: executor that save variable
:param executor: executor that save variable
:param dirname: directory path
:param dirname: directory path
:param program: program. If vars is None, then filter all variables in this
:param
main_
program: program. If vars is None, then filter all variables in this
program which fit `predicate`. Default g_program.
program which fit `predicate`. Default g_program.
:param predicate: The Predicate describes a callable that returns a variable
:param predicate: The Predicate describes a callable that returns a variable
as a bool. If it returns true, the variables will be loaded.
as a bool. If it returns true, the variables will be loaded.
...
@@ -105,15 +105,15 @@ def load_vars(executor, dirname, program=None, vars=None, predicate=None):
...
@@ -105,15 +105,15 @@ def load_vars(executor, dirname, program=None, vars=None, predicate=None):
:return: None
:return: None
"""
"""
if
vars
is
None
:
if
vars
is
None
:
if
program
is
None
:
if
main_
program
is
None
:
program
=
g
_program
main_program
=
g_main
_program
if
not
isinstance
(
program
,
Program
):
if
not
isinstance
(
main_
program
,
Program
):
raise
TypeError
(
"program's type should be Program"
)
raise
TypeError
(
"program's type should be Program"
)
load_vars
(
load_vars
(
executor
,
executor
,
dirname
=
dirname
,
dirname
=
dirname
,
vars
=
filter
(
predicate
,
program
.
list_vars
()))
vars
=
filter
(
predicate
,
main_
program
.
list_vars
()))
else
:
else
:
load_prog
=
Program
()
load_prog
=
Program
()
load_block
=
load_prog
.
global_block
()
load_block
=
load_prog
.
global_block
()
...
@@ -129,27 +129,33 @@ def load_vars(executor, dirname, program=None, vars=None, predicate=None):
...
@@ -129,27 +129,33 @@ def load_vars(executor, dirname, program=None, vars=None, predicate=None):
executor
.
run
(
load_prog
)
executor
.
run
(
load_prog
)
def
load_params
(
executor
,
dirname
,
program
=
None
):
def
load_params
(
executor
,
dirname
,
main_
program
=
None
):
"""
"""
load all parameters from directory by executor.
load all parameters from directory by executor.
"""
"""
load_vars
(
load_vars
(
executor
,
dirname
=
dirname
,
program
=
program
,
predicate
=
is_parameter
)
executor
,
dirname
=
dirname
,
main_program
=
main_program
,
predicate
=
is_parameter
)
def
load_persistables
(
executor
,
dirname
,
program
=
None
):
def
load_persistables
(
executor
,
dirname
,
main_
program
=
None
):
"""
"""
load all persistables from directory by executor.
load all persistables from directory by executor.
"""
"""
load_vars
(
load_vars
(
executor
,
dirname
=
dirname
,
program
=
program
,
predicate
=
is_persistable
)
executor
,
dirname
=
dirname
,
main_program
=
main_program
,
predicate
=
is_persistable
)
def
save_inference_model
(
dirname
,
def
save_inference_model
(
dirname
,
feeded_var_names
,
feeded_var_names
,
target_vars
,
target_vars
,
executor
,
executor
,
program
=
None
):
main_
program
=
None
):
"""
"""
Build a model especially for inference,
Build a model especially for inference,
and save it to directory by the executor.
and save it to directory by the executor.
...
@@ -158,20 +164,20 @@ def save_inference_model(dirname,
...
@@ -158,20 +164,20 @@ def save_inference_model(dirname,
:param feeded_var_names: Names of variables that need to be feeded data during inference
:param feeded_var_names: Names of variables that need to be feeded data during inference
:param target_vars: Variables from which we can get inference results.
:param target_vars: Variables from which we can get inference results.
:param executor: executor that save inference model
:param executor: executor that save inference model
:param program: original program, which will be pruned to build the inference model.
:param
main_
program: original program, which will be pruned to build the inference model.
Default g_program.
Default g_program.
:return: None
:return: None
"""
"""
if
program
is
None
:
if
main_
program
is
None
:
program
=
g
_program
main_program
=
g_main
_program
if
not
isinstance
(
target_vars
,
list
):
if
not
isinstance
(
target_vars
,
list
):
target_vars
=
[
target_vars
]
target_vars
=
[
target_vars
]
if
not
os
.
path
.
isdir
(
dirname
):
if
not
os
.
path
.
isdir
(
dirname
):
os
.
makedirs
(
dirname
)
os
.
makedirs
(
dirname
)
pruned_program
=
program
.
prune
(
target_vars
)
pruned_program
=
main_
program
.
prune
(
target_vars
)
fetch_var_names
=
[
v
.
name
for
v
in
target_vars
]
fetch_var_names
=
[
v
.
name
for
v
in
target_vars
]
model_file_name
=
dirname
+
"/__model__"
model_file_name
=
dirname
+
"/__model__"
...
@@ -182,10 +188,10 @@ def save_inference_model(dirname,
...
@@ -182,10 +188,10 @@ def save_inference_model(dirname,
"fetch_var_names"
:
fetch_var_names
"fetch_var_names"
:
fetch_var_names
},
f
,
-
1
)
},
f
,
-
1
)
save_params
(
executor
,
dirname
,
program
)
save_params
(
executor
,
dirname
,
main_
program
)
def
load_persistables_if_exist
(
executor
,
dirname
,
program
=
None
):
def
load_persistables_if_exist
(
executor
,
dirname
,
main_
program
=
None
):
filenames
=
next
(
os
.
walk
(
dirname
))[
2
]
filenames
=
next
(
os
.
walk
(
dirname
))[
2
]
filenames
=
set
(
filenames
)
filenames
=
set
(
filenames
)
...
@@ -198,7 +204,7 @@ def load_persistables_if_exist(executor, dirname, program=None):
...
@@ -198,7 +204,7 @@ def load_persistables_if_exist(executor, dirname, program=None):
load_vars
(
load_vars
(
executor
,
executor
,
dirname
,
dirname
,
program
=
program
,
main_program
=
main_
program
,
vars
=
None
,
vars
=
None
,
predicate
=
_is_presistable_and_exist_
)
predicate
=
_is_presistable_and_exist_
)
...
...
python/paddle/v2/framework/layer_helper.py
浏览文件 @
51d4afaa
import
copy
import
copy
import
itertools
import
itertools
from
paddle.v2.framework.framework
import
Variable
,
g_program
,
\
from
paddle.v2.framework.framework
import
Variable
,
g_
main_
program
,
\
g_
init
_program
,
unique_name
,
Program
g_
startup
_program
,
unique_name
,
Program
from
paddle.v2.framework.initializer
import
ConstantInitializer
,
\
from
paddle.v2.framework.initializer
import
ConstantInitializer
,
\
UniformInitializer
UniformInitializer
...
@@ -20,23 +20,23 @@ class LayerHelper(object):
...
@@ -20,23 +20,23 @@ class LayerHelper(object):
return
self
.
kwargs
[
'name'
]
return
self
.
kwargs
[
'name'
]
@
property
@
property
def
program
(
self
):
def
main_
program
(
self
):
prog
=
self
.
kwargs
.
get
(
'program'
,
None
)
prog
=
self
.
kwargs
.
get
(
'
main_
program'
,
None
)
if
prog
is
None
:
if
prog
is
None
:
return
g_program
return
g_
main_
program
else
:
else
:
return
prog
return
prog
@
property
@
property
def
init
_program
(
self
):
def
startup
_program
(
self
):
prog
=
self
.
kwargs
.
get
(
'
init
_program'
,
None
)
prog
=
self
.
kwargs
.
get
(
'
startup
_program'
,
None
)
if
prog
is
None
:
if
prog
is
None
:
return
g_
init
_program
return
g_
startup
_program
else
:
else
:
return
prog
return
prog
def
append_op
(
self
,
*
args
,
**
kwargs
):
def
append_op
(
self
,
*
args
,
**
kwargs
):
return
self
.
program
.
current_block
().
append_op
(
*
args
,
**
kwargs
)
return
self
.
main_
program
.
current_block
().
append_op
(
*
args
,
**
kwargs
)
def
multiple_input
(
self
,
input_param_name
=
'input'
):
def
multiple_input
(
self
,
input_param_name
=
'input'
):
inputs
=
self
.
kwargs
.
get
(
input_param_name
,
[])
inputs
=
self
.
kwargs
.
get
(
input_param_name
,
[])
...
@@ -120,27 +120,27 @@ class LayerHelper(object):
...
@@ -120,27 +120,27 @@ class LayerHelper(object):
attr_copy
[
'initializer'
]
=
initializer
attr_copy
[
'initializer'
]
=
initializer
if
attr_copy
[
'name'
]
is
None
:
if
attr_copy
[
'name'
]
is
None
:
attr_copy
[
'name'
]
=
unique_name
(
"."
.
join
([
self
.
name
,
suffix
]))
attr_copy
[
'name'
]
=
unique_name
(
"."
.
join
([
self
.
name
,
suffix
]))
self
.
init
_program
.
global_block
().
create_parameter
(
self
.
startup
_program
.
global_block
().
create_parameter
(
dtype
=
dtype
,
shape
=
shape
,
**
attr_copy
)
dtype
=
dtype
,
shape
=
shape
,
**
attr_copy
)
return
self
.
program
.
global_block
().
create_parameter
(
return
self
.
main_
program
.
global_block
().
create_parameter
(
name
=
attr_copy
[
'name'
],
dtype
=
dtype
,
shape
=
shape
)
name
=
attr_copy
[
'name'
],
dtype
=
dtype
,
shape
=
shape
)
def
create_tmp_variable
(
self
,
dtype
):
def
create_tmp_variable
(
self
,
dtype
):
return
self
.
program
.
current_block
().
create_var
(
return
self
.
main_
program
.
current_block
().
create_var
(
name
=
unique_name
(
"."
.
join
([
self
.
name
,
'tmp'
])),
name
=
unique_name
(
"."
.
join
([
self
.
name
,
'tmp'
])),
dtype
=
dtype
,
dtype
=
dtype
,
persistable
=
False
)
persistable
=
False
)
def
create_variable
(
self
,
*
args
,
**
kwargs
):
def
create_variable
(
self
,
*
args
,
**
kwargs
):
return
self
.
program
.
current_block
().
create_var
(
*
args
,
**
kwargs
)
return
self
.
main_
program
.
current_block
().
create_var
(
*
args
,
**
kwargs
)
def
create_global_variable
(
self
,
persistable
=
False
,
*
args
,
**
kwargs
):
def
create_global_variable
(
self
,
persistable
=
False
,
*
args
,
**
kwargs
):
return
self
.
program
.
global_block
().
create_var
(
return
self
.
main_
program
.
global_block
().
create_var
(
*
args
,
persistable
=
persistable
,
**
kwargs
)
*
args
,
persistable
=
persistable
,
**
kwargs
)
def
set_variable_initializer
(
self
,
var
,
initializer
):
def
set_variable_initializer
(
self
,
var
,
initializer
):
assert
isinstance
(
var
,
Variable
)
assert
isinstance
(
var
,
Variable
)
self
.
init
_program
.
global_block
().
create_var
(
self
.
startup
_program
.
global_block
().
create_var
(
name
=
var
.
name
,
name
=
var
.
name
,
type
=
var
.
type
,
type
=
var
.
type
,
dtype
=
var
.
data_type
,
dtype
=
var
.
data_type
,
...
...
python/paddle/v2/framework/layers.py
浏览文件 @
51d4afaa
...
@@ -18,8 +18,8 @@ def fc(input,
...
@@ -18,8 +18,8 @@ def fc(input,
name
=
None
,
name
=
None
,
act
=
None
,
act
=
None
,
num_flatten_dims
=
1
,
num_flatten_dims
=
1
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
# create helper
# create helper
helper
=
LayerHelper
(
'fc'
,
**
locals
())
helper
=
LayerHelper
(
'fc'
,
**
locals
())
...
@@ -64,8 +64,8 @@ def embedding(input,
...
@@ -64,8 +64,8 @@ def embedding(input,
data_type
=
'float32'
,
data_type
=
'float32'
,
is_sparse
=
False
,
is_sparse
=
False
,
param_attr
=
None
,
param_attr
=
None
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
helper
=
LayerHelper
(
'embedding'
,
**
locals
())
helper
=
LayerHelper
(
'embedding'
,
**
locals
())
w
=
helper
.
create_parameter
(
w
=
helper
.
create_parameter
(
attr
=
helper
.
param_attr
,
shape
=
size
,
dtype
=
data_type
)
attr
=
helper
.
param_attr
,
shape
=
size
,
dtype
=
data_type
)
...
@@ -84,8 +84,8 @@ def data(name,
...
@@ -84,8 +84,8 @@ def data(name,
data_type
=
'float32'
,
data_type
=
'float32'
,
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
append_batch_size
=
True
,
append_batch_size
=
True
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
helper
=
LayerHelper
(
'data'
,
**
locals
())
helper
=
LayerHelper
(
'data'
,
**
locals
())
shape
=
list
(
shape
)
shape
=
list
(
shape
)
for
i
in
xrange
(
len
(
shape
)):
for
i
in
xrange
(
len
(
shape
)):
...
@@ -178,7 +178,7 @@ _create_op_func_('sigmoid')
...
@@ -178,7 +178,7 @@ _create_op_func_('sigmoid')
_create_op_func_
(
'scale'
)
_create_op_func_
(
'scale'
)
def
cast
(
x
,
data_type
,
program
=
None
):
def
cast
(
x
,
data_type
,
main_
program
=
None
):
helper
=
LayerHelper
(
'cast'
,
**
locals
())
helper
=
LayerHelper
(
'cast'
,
**
locals
())
out
=
helper
.
create_tmp_variable
(
dtype
=
data_type
)
out
=
helper
.
create_tmp_variable
(
dtype
=
data_type
)
helper
.
append_op
(
helper
.
append_op
(
...
@@ -190,7 +190,7 @@ def cast(x, data_type, program=None):
...
@@ -190,7 +190,7 @@ def cast(x, data_type, program=None):
return
out
return
out
def
concat
(
input
,
axis
,
program
=
None
,
init
_program
=
None
):
def
concat
(
input
,
axis
,
main_program
=
None
,
startup
_program
=
None
):
helper
=
LayerHelper
(
'concat'
,
**
locals
())
helper
=
LayerHelper
(
'concat'
,
**
locals
())
out
=
helper
.
create_tmp_variable
(
dtype
=
helper
.
input_dtype
())
out
=
helper
.
create_tmp_variable
(
dtype
=
helper
.
input_dtype
())
helper
.
append_op
(
helper
.
append_op
(
...
@@ -201,7 +201,7 @@ def concat(input, axis, program=None, init_program=None):
...
@@ -201,7 +201,7 @@ def concat(input, axis, program=None, init_program=None):
return
out
return
out
def
sums
(
input
,
program
=
None
,
init
_program
=
None
):
def
sums
(
input
,
main_program
=
None
,
startup
_program
=
None
):
helper
=
LayerHelper
(
'sum'
,
**
locals
())
helper
=
LayerHelper
(
'sum'
,
**
locals
())
out
=
helper
.
create_tmp_variable
(
dtype
=
helper
.
input_dtype
())
out
=
helper
.
create_tmp_variable
(
dtype
=
helper
.
input_dtype
())
helper
.
append_op
(
type
=
'sum'
,
inputs
=
{
'X'
:
input
},
outputs
=
{
'Out'
:
out
})
helper
.
append_op
(
type
=
'sum'
,
inputs
=
{
'X'
:
input
},
outputs
=
{
'Out'
:
out
})
...
@@ -281,8 +281,8 @@ def sequence_conv(input,
...
@@ -281,8 +281,8 @@ def sequence_conv(input,
padding
=
None
,
padding
=
None
,
bias_attr
=
None
,
bias_attr
=
None
,
param_attr
=
None
,
param_attr
=
None
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
# FIXME(dzh) : want to unify the argument of python layer
# FIXME(dzh) : want to unify the argument of python layer
# function. So we ignore some unecessary attributes.
# function. So we ignore some unecessary attributes.
# such as, padding_trainable, context_start.
# such as, padding_trainable, context_start.
...
@@ -321,8 +321,8 @@ def conv2d(input,
...
@@ -321,8 +321,8 @@ def conv2d(input,
padding
=
None
,
padding
=
None
,
bias_attr
=
None
,
bias_attr
=
None
,
param_attr
=
None
,
param_attr
=
None
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
helper
=
LayerHelper
(
'conv2d'
,
**
locals
())
helper
=
LayerHelper
(
'conv2d'
,
**
locals
())
dtype
=
helper
.
input_dtype
()
dtype
=
helper
.
input_dtype
()
...
@@ -388,8 +388,8 @@ def pool2d(input,
...
@@ -388,8 +388,8 @@ def pool2d(input,
pool_stride
=
[
1
,
1
],
pool_stride
=
[
1
,
1
],
pool_padding
=
[
0
,
0
],
pool_padding
=
[
0
,
0
],
global_pooling
=
False
,
global_pooling
=
False
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
if
pool_type
not
in
[
"max"
,
"avg"
]:
if
pool_type
not
in
[
"max"
,
"avg"
]:
raise
ValueError
(
raise
ValueError
(
"Unknown pool_type: '%s'. It can only be 'max' or 'avg'."
,
"Unknown pool_type: '%s'. It can only be 'max' or 'avg'."
,
...
@@ -428,8 +428,8 @@ def batch_norm(input,
...
@@ -428,8 +428,8 @@ def batch_norm(input,
param_attr
=
None
,
param_attr
=
None
,
bias_attr
=
None
,
bias_attr
=
None
,
data_layout
=
'NCHW'
,
data_layout
=
'NCHW'
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
helper
=
LayerHelper
(
'batch_norm'
,
**
locals
())
helper
=
LayerHelper
(
'batch_norm'
,
**
locals
())
dtype
=
helper
.
input_dtype
()
dtype
=
helper
.
input_dtype
()
...
@@ -505,16 +505,16 @@ class BlockGuard(object):
...
@@ -505,16 +505,16 @@ class BlockGuard(object):
keyword.
keyword.
"""
"""
def
__init__
(
self
,
program
):
def
__init__
(
self
,
main_
program
):
if
not
isinstance
(
program
,
Program
):
if
not
isinstance
(
main_
program
,
Program
):
raise
TypeError
(
"BlockGuard takes a program"
)
raise
TypeError
(
"BlockGuard takes a program"
)
self
.
program
=
program
self
.
main_program
=
main_
program
def
__enter__
(
self
):
def
__enter__
(
self
):
self
.
program
.
create_block
()
self
.
main_
program
.
create_block
()
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
program
.
rollback
()
self
.
main_
program
.
rollback
()
if
exc_type
is
not
None
:
if
exc_type
is
not
None
:
return
False
# re-raise exception
return
False
# re-raise exception
return
True
return
True
...
@@ -524,7 +524,7 @@ class StaticRNNGuard(BlockGuard):
...
@@ -524,7 +524,7 @@ class StaticRNNGuard(BlockGuard):
def
__init__
(
self
,
rnn
):
def
__init__
(
self
,
rnn
):
if
not
isinstance
(
rnn
,
StaticRNN
):
if
not
isinstance
(
rnn
,
StaticRNN
):
raise
TypeError
(
"StaticRNNGuard takes an StaticRNN"
)
raise
TypeError
(
"StaticRNNGuard takes an StaticRNN"
)
super
(
StaticRNNGuard
,
self
).
__init__
(
rnn
.
helper
.
program
)
super
(
StaticRNNGuard
,
self
).
__init__
(
rnn
.
helper
.
main_
program
)
self
.
rnn
=
rnn
self
.
rnn
=
rnn
def
__enter__
(
self
):
def
__enter__
(
self
):
...
@@ -560,8 +560,9 @@ class StaticRNN(object):
...
@@ -560,8 +560,9 @@ class StaticRNN(object):
IN_RNN_BLOCK
=
1
IN_RNN_BLOCK
=
1
AFTER_RNN_BLOCK
=
2
AFTER_RNN_BLOCK
=
2
def
__init__
(
self
,
name
=
None
,
program
=
None
):
def
__init__
(
self
,
name
=
None
,
main_program
=
None
):
self
.
helper
=
LayerHelper
(
"static_rnn"
,
name
=
name
,
program
=
program
)
self
.
helper
=
LayerHelper
(
"static_rnn"
,
name
=
name
,
main_program
=
main_program
)
self
.
memories
=
{}
# memory map, from pre_mem.name --> MemoryLink
self
.
memories
=
{}
# memory map, from pre_mem.name --> MemoryLink
self
.
inputs
=
[]
# input variable list in current block
self
.
inputs
=
[]
# input variable list in current block
self
.
outputs
=
[]
# output variable list in parent block
self
.
outputs
=
[]
# output variable list in parent block
...
@@ -653,7 +654,7 @@ class StaticRNN(object):
...
@@ -653,7 +654,7 @@ class StaticRNN(object):
self
.
memories
[
mem
.
name
].
mem
=
var
self
.
memories
[
mem
.
name
].
mem
=
var
def
parent_block
(
self
):
def
parent_block
(
self
):
prog
=
self
.
helper
.
program
prog
=
self
.
helper
.
main_
program
parent_idx
=
prog
.
current_block
().
parent_idx
parent_idx
=
prog
.
current_block
().
parent_idx
assert
parent_idx
>=
0
assert
parent_idx
>=
0
parent_block
=
prog
.
block
(
parent_idx
)
parent_block
=
prog
.
block
(
parent_idx
)
...
@@ -670,8 +671,8 @@ class StaticRNN(object):
...
@@ -670,8 +671,8 @@ class StaticRNN(object):
return
self
.
outputs
return
self
.
outputs
def
complete_rnn_op
(
self
):
def
complete_rnn_op
(
self
):
program
=
self
.
helper
.
program
main_program
=
self
.
helper
.
main_
program
rnn_block
=
program
.
current_block
()
rnn_block
=
main_
program
.
current_block
()
parent_block
=
self
.
parent_block
()
parent_block
=
self
.
parent_block
()
local_inputs
=
set
()
local_inputs
=
set
()
...
@@ -737,7 +738,7 @@ class StaticRNN(object):
...
@@ -737,7 +738,7 @@ class StaticRNN(object):
})
})
def
lod_rank_table
(
x
,
level
=
0
,
program
=
None
):
def
lod_rank_table
(
x
,
level
=
0
,
main_
program
=
None
):
helper
=
LayerHelper
(
"lod_rank_table"
,
**
locals
())
helper
=
LayerHelper
(
"lod_rank_table"
,
**
locals
())
table
=
helper
.
create_variable
(
table
=
helper
.
create_variable
(
type
=
core
.
VarDesc
.
VarType
.
LOD_RANK_TABLE
,
type
=
core
.
VarDesc
.
VarType
.
LOD_RANK_TABLE
,
...
...
python/paddle/v2/framework/net_drawer.py
浏览文件 @
51d4afaa
...
@@ -80,7 +80,7 @@ def parse_graph(program, graph, var_dict, **kwargs):
...
@@ -80,7 +80,7 @@ def parse_graph(program, graph, var_dict, **kwargs):
graph
.
edge
(
**
draw_edge
(
var_dict
,
op
,
e
,
arg
))
graph
.
edge
(
**
draw_edge
(
var_dict
,
op
,
e
,
arg
))
def
draw_graph
(
init_program
,
program
,
**
kwargs
):
def
draw_graph
(
startup_program
,
main_
program
,
**
kwargs
):
if
kwargs
.
has_key
(
"graph_attr"
):
if
kwargs
.
has_key
(
"graph_attr"
):
GRAPH_STYLE
.
update
(
kwargs
[
graph_attr
])
GRAPH_STYLE
.
update
(
kwargs
[
graph_attr
])
if
kwargs
.
has_key
(
"node_attr"
):
if
kwargs
.
has_key
(
"node_attr"
):
...
@@ -101,8 +101,8 @@ def draw_graph(init_program, program, **kwargs):
...
@@ -101,8 +101,8 @@ def draw_graph(init_program, program, **kwargs):
**
kwargs
)
**
kwargs
)
var_dict
=
{}
var_dict
=
{}
parse_graph
(
init
_program
,
g
,
var_dict
)
parse_graph
(
startup
_program
,
g
,
var_dict
)
parse_graph
(
program
,
g
,
var_dict
)
parse_graph
(
main_
program
,
g
,
var_dict
)
if
filename
!=
None
:
if
filename
!=
None
:
g
.
save
()
g
.
save
()
...
...
python/paddle/v2/framework/nets.py
浏览文件 @
51d4afaa
...
@@ -10,23 +10,23 @@ def simple_img_conv_pool(input,
...
@@ -10,23 +10,23 @@ def simple_img_conv_pool(input,
pool_stride
,
pool_stride
,
act
,
act
,
pool_type
=
'max'
,
pool_type
=
'max'
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
conv_out
=
layers
.
conv2d
(
conv_out
=
layers
.
conv2d
(
input
=
input
,
input
=
input
,
num_filters
=
num_filters
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
filter_size
=
filter_size
,
act
=
act
,
act
=
act
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
pool_out
=
layers
.
pool2d
(
pool_out
=
layers
.
pool2d
(
input
=
conv_out
,
input
=
conv_out
,
pool_size
=
pool_size
,
pool_size
=
pool_size
,
pool_type
=
pool_type
,
pool_type
=
pool_type
,
pool_stride
=
pool_stride
,
pool_stride
=
pool_stride
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
return
pool_out
return
pool_out
...
@@ -40,8 +40,8 @@ def img_conv_group(input,
...
@@ -40,8 +40,8 @@ def img_conv_group(input,
conv_batchnorm_drop_rate
=
None
,
conv_batchnorm_drop_rate
=
None
,
pool_stride
=
1
,
pool_stride
=
1
,
pool_type
=
None
,
pool_type
=
None
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
"""
"""
Image Convolution Group, Used for vgg net.
Image Convolution Group, Used for vgg net.
"""
"""
...
@@ -71,30 +71,30 @@ def img_conv_group(input,
...
@@ -71,30 +71,30 @@ def img_conv_group(input,
filter_size
=
conv_filter_size
[
i
],
filter_size
=
conv_filter_size
[
i
],
padding
=
conv_padding
[
i
],
padding
=
conv_padding
[
i
],
act
=
local_conv_act
,
act
=
local_conv_act
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
if
conv_with_batchnorm
[
i
]:
if
conv_with_batchnorm
[
i
]:
tmp
=
layers
.
batch_norm
(
tmp
=
layers
.
batch_norm
(
input
=
tmp
,
input
=
tmp
,
act
=
conv_act
,
act
=
conv_act
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
drop_rate
=
conv_batchnorm_drop_rate
[
i
]
drop_rate
=
conv_batchnorm_drop_rate
[
i
]
if
abs
(
drop_rate
)
>
1e-5
:
if
abs
(
drop_rate
)
>
1e-5
:
tmp
=
layers
.
dropout
(
tmp
=
layers
.
dropout
(
x
=
tmp
,
x
=
tmp
,
dropout_prob
=
drop_rate
,
dropout_prob
=
drop_rate
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
pool_out
=
layers
.
pool2d
(
pool_out
=
layers
.
pool2d
(
input
=
tmp
,
input
=
tmp
,
pool_size
=
pool_size
,
pool_size
=
pool_size
,
pool_type
=
pool_type
,
pool_type
=
pool_type
,
pool_stride
=
pool_stride
,
pool_stride
=
pool_stride
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
return
pool_out
return
pool_out
...
@@ -103,19 +103,19 @@ def sequence_conv_pool(input,
...
@@ -103,19 +103,19 @@ def sequence_conv_pool(input,
filter_size
,
filter_size
,
act
=
"sigmoid"
,
act
=
"sigmoid"
,
pool_type
=
"max"
,
pool_type
=
"max"
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
conv_out
=
layers
.
sequence_conv
(
conv_out
=
layers
.
sequence_conv
(
input
=
input
,
input
=
input
,
num_filters
=
num_filters
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
filter_size
=
filter_size
,
act
=
act
,
act
=
act
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
pool_out
=
layers
.
sequence_pool
(
pool_out
=
layers
.
sequence_pool
(
input
=
conv_out
,
input
=
conv_out
,
pool_type
=
pool_type
,
pool_type
=
pool_type
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
return
pool_out
return
pool_out
python/paddle/v2/framework/optimizer.py
浏览文件 @
51d4afaa
...
@@ -132,7 +132,7 @@ class Optimizer(object):
...
@@ -132,7 +132,7 @@ class Optimizer(object):
def
create_optimization_pass
(
self
,
def
create_optimization_pass
(
self
,
parameters_and_grads
,
parameters_and_grads
,
loss
,
loss
,
init
_program
=
None
):
startup
_program
=
None
):
"""Add optimization operators to update gradients to variables.
"""Add optimization operators to update gradients to variables.
Args:
Args:
...
@@ -144,7 +144,7 @@ class Optimizer(object):
...
@@ -144,7 +144,7 @@ class Optimizer(object):
optimization. This will include parameter update ops, global step
optimization. This will include parameter update ops, global step
update ops and any other custom ops required by subclasses to manage
update ops and any other custom ops required by subclasses to manage
their internal state.
their internal state.
:param
init
_program:
:param
startup
_program:
"""
"""
# This is a default implementation of create_optimization_pass that
# This is a default implementation of create_optimization_pass that
# can be shared by most optimizers. This implementation assumes that
# can be shared by most optimizers. This implementation assumes that
...
@@ -156,7 +156,9 @@ class Optimizer(object):
...
@@ -156,7 +156,9 @@ class Optimizer(object):
# Create any accumulators
# Create any accumulators
program
=
loss
.
block
.
program
program
=
loss
.
block
.
program
self
.
helper
=
LayerHelper
(
self
.
helper
=
LayerHelper
(
self
.
__class__
.
__name__
,
program
=
program
,
init_program
=
init_program
)
self
.
__class__
.
__name__
,
main_program
=
program
,
startup_program
=
startup_program
)
self
.
_create_accumulators
(
loss
.
block
,
self
.
_create_accumulators
(
loss
.
block
,
[
p
[
0
]
for
p
in
parameters_and_grads
])
[
p
[
0
]
for
p
in
parameters_and_grads
])
# Create any necessary tensors
# Create any necessary tensors
...
@@ -185,7 +187,7 @@ class Optimizer(object):
...
@@ -185,7 +187,7 @@ class Optimizer(object):
def
minimize
(
self
,
def
minimize
(
self
,
loss
,
loss
,
init
_program
=
None
,
startup
_program
=
None
,
parameter_list
=
None
,
parameter_list
=
None
,
no_grad_set
=
None
):
no_grad_set
=
None
):
"""Add operations to minimize `loss` by updating `parameter_list`.
"""Add operations to minimize `loss` by updating `parameter_list`.
...
@@ -198,7 +200,7 @@ class Optimizer(object):
...
@@ -198,7 +200,7 @@ class Optimizer(object):
# Add regularization if any
# Add regularization if any
params_grads
=
append_regularization_ops
(
params_grads
)
params_grads
=
append_regularization_ops
(
params_grads
)
optimize_ops
=
self
.
create_optimization_pass
(
params_grads
,
loss
,
optimize_ops
=
self
.
create_optimization_pass
(
params_grads
,
loss
,
init
_program
)
startup
_program
)
return
optimize_ops
return
optimize_ops
...
...
python/paddle/v2/framework/tests/test_executor_and_mul.py
浏览文件 @
51d4afaa
...
@@ -2,7 +2,7 @@ import unittest
...
@@ -2,7 +2,7 @@ import unittest
from
paddle.v2.framework.layers
import
mul
,
data
from
paddle.v2.framework.layers
import
mul
,
data
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.framework
import
g_program
from
paddle.v2.framework.framework
import
g_
main_
program
import
numpy
import
numpy
...
@@ -23,7 +23,7 @@ class TestExecutor(unittest.TestCase):
...
@@ -23,7 +23,7 @@ class TestExecutor(unittest.TestCase):
tensor_b
=
core
.
LoDTensor
()
tensor_b
=
core
.
LoDTensor
()
tensor_b
.
set
(
b_np
,
place
)
tensor_b
.
set
(
b_np
,
place
)
exe
=
Executor
(
place
)
exe
=
Executor
(
place
)
outs
=
exe
.
run
(
g_program
,
outs
=
exe
.
run
(
g_
main_
program
,
feed
=
{
'a'
:
tensor_a
,
feed
=
{
'a'
:
tensor_a
,
'b'
:
tensor_b
},
'b'
:
tensor_b
},
fetch_list
=
[
out
])
fetch_list
=
[
out
])
...
...
python/paddle/v2/framework/tests/test_fit_a_line.py
浏览文件 @
51d4afaa
...
@@ -3,40 +3,44 @@ import paddle.v2.framework.layers as layers
...
@@ -3,40 +3,44 @@ import paddle.v2.framework.layers as layers
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.optimizer
as
optimizer
import
paddle.v2.framework.optimizer
as
optimizer
from
paddle.v2.framework.framework
import
Program
,
g_program
from
paddle.v2.framework.framework
import
Program
,
g_
main_
program
from
paddle.v2.framework.io
import
save_persistables
,
load_persistables
from
paddle.v2.framework.io
import
save_persistables
,
load_persistables
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.executor
import
Executor
import
numpy
as
np
import
numpy
as
np
init
_program
=
Program
()
startup
_program
=
Program
()
program
=
Program
()
main_
program
=
Program
()
x
=
layers
.
data
(
x
=
layers
.
data
(
name
=
'x'
,
name
=
'x'
,
shape
=
[
13
],
shape
=
[
13
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
y_predict
=
layers
.
fc
(
input
=
x
,
y_predict
=
layers
.
fc
(
input
=
x
,
size
=
1
,
size
=
1
,
act
=
None
,
act
=
None
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
y
=
layers
.
data
(
y
=
layers
.
data
(
name
=
'y'
,
name
=
'y'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
cost
=
layers
.
square_error_cost
(
cost
=
layers
.
square_error_cost
(
input
=
y_predict
,
label
=
y
,
program
=
program
,
init_program
=
init_program
)
input
=
y_predict
,
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
,
init_program
=
init_program
)
label
=
y
,
main_program
=
main_program
,
startup_program
=
startup_program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
main_program
=
main_program
,
startup_program
=
startup_program
)
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.001
)
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.001
)
opts
=
sgd_optimizer
.
minimize
(
avg_cost
,
init
_program
)
opts
=
sgd_optimizer
.
minimize
(
avg_cost
,
startup
_program
)
BATCH_SIZE
=
20
BATCH_SIZE
=
20
...
@@ -48,12 +52,12 @@ train_reader = paddle.batch(
...
@@ -48,12 +52,12 @@ train_reader = paddle.batch(
place
=
core
.
CPUPlace
()
place
=
core
.
CPUPlace
()
exe
=
Executor
(
place
)
exe
=
Executor
(
place
)
exe
.
run
(
init
_program
,
feed
=
{},
fetch_list
=
[])
exe
.
run
(
startup
_program
,
feed
=
{},
fetch_list
=
[])
PASS_NUM
=
100
PASS_NUM
=
100
for
pass_id
in
range
(
PASS_NUM
):
for
pass_id
in
range
(
PASS_NUM
):
save_persistables
(
exe
,
"./fit_a_line.model/"
,
program
=
program
)
save_persistables
(
exe
,
"./fit_a_line.model/"
,
main_program
=
main_
program
)
load_persistables
(
exe
,
"./fit_a_line.model/"
,
program
=
program
)
load_persistables
(
exe
,
"./fit_a_line.model/"
,
main_program
=
main_
program
)
for
data
in
train_reader
():
for
data
in
train_reader
():
x_data
=
np
.
array
(
map
(
lambda
x
:
x
[
0
],
data
)).
astype
(
"float32"
)
x_data
=
np
.
array
(
map
(
lambda
x
:
x
[
0
],
data
)).
astype
(
"float32"
)
y_data
=
np
.
array
(
map
(
lambda
x
:
x
[
1
],
data
)).
astype
(
"float32"
)
y_data
=
np
.
array
(
map
(
lambda
x
:
x
[
1
],
data
)).
astype
(
"float32"
)
...
@@ -65,7 +69,7 @@ for pass_id in range(PASS_NUM):
...
@@ -65,7 +69,7 @@ for pass_id in range(PASS_NUM):
tensor_y
=
core
.
LoDTensor
()
tensor_y
=
core
.
LoDTensor
()
tensor_y
.
set
(
y_data
,
place
)
tensor_y
.
set
(
y_data
,
place
)
# print tensor_y.get_dims()
# print tensor_y.get_dims()
outs
=
exe
.
run
(
program
,
outs
=
exe
.
run
(
main_
program
,
feed
=
{
'x'
:
tensor_x
,
feed
=
{
'x'
:
tensor_x
,
'y'
:
tensor_y
},
'y'
:
tensor_y
},
fetch_list
=
[
avg_cost
])
fetch_list
=
[
avg_cost
])
...
...
python/paddle/v2/framework/tests/test_image_classification_layer.py
浏览文件 @
51d4afaa
...
@@ -9,8 +9,8 @@ def conv_block(input,
...
@@ -9,8 +9,8 @@ def conv_block(input,
num_filter
,
num_filter
,
groups
,
groups
,
dropouts
,
dropouts
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
return
nets
.
img_conv_group
(
return
nets
.
img_conv_group
(
input
=
input
,
input
=
input
,
pool_size
=
2
,
pool_size
=
2
,
...
@@ -21,77 +21,81 @@ def conv_block(input,
...
@@ -21,77 +21,81 @@ def conv_block(input,
conv_with_batchnorm
=
True
,
conv_with_batchnorm
=
True
,
conv_batchnorm_drop_rate
=
dropouts
,
conv_batchnorm_drop_rate
=
dropouts
,
pool_type
=
'max'
,
pool_type
=
'max'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
class
TestLayer
(
unittest
.
TestCase
):
class
TestLayer
(
unittest
.
TestCase
):
def
test_batch_norm_layer
(
self
):
def
test_batch_norm_layer
(
self
):
program
=
Program
()
main_
program
=
Program
()
init
_program
=
Program
()
startup
_program
=
Program
()
images
=
layers
.
data
(
images
=
layers
.
data
(
name
=
'pixel'
,
name
=
'pixel'
,
shape
=
[
3
,
48
,
48
],
shape
=
[
3
,
48
,
48
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
)
main_program
=
main_
program
)
layers
.
batch_norm
(
layers
.
batch_norm
(
input
=
images
,
program
=
program
,
init_program
=
init_program
)
input
=
images
,
main_program
=
main_program
,
startup_program
=
startup_program
)
# print str(program)
# print str(
main_
program)
def
test_dropout_layer
(
self
):
def
test_dropout_layer
(
self
):
program
=
Program
()
main_
program
=
Program
()
init
_program
=
Program
()
startup
_program
=
Program
()
images
=
layers
.
data
(
images
=
layers
.
data
(
name
=
'pixel'
,
name
=
'pixel'
,
shape
=
[
3
,
48
,
48
],
shape
=
[
3
,
48
,
48
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
)
main_program
=
main_
program
)
layers
.
dropout
(
layers
.
dropout
(
x
=
images
,
x
=
images
,
dropout_prob
=
0.5
,
dropout_prob
=
0.5
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
# print str(program)
# print str(
main_
program)
def
test_img_conv_group
(
self
):
def
test_img_conv_group
(
self
):
program
=
Program
()
main_
program
=
Program
()
init
_program
=
Program
()
startup
_program
=
Program
()
images
=
layers
.
data
(
images
=
layers
.
data
(
name
=
'pixel'
,
name
=
'pixel'
,
shape
=
[
3
,
48
,
48
],
shape
=
[
3
,
48
,
48
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_program
=
main_program
,
init_program
=
init_program
)
startup_program
=
startup_program
)
conv1
=
conv_block
(
images
,
64
,
2
,
[
0.3
,
0
],
program
,
init_program
)
conv1
=
conv_block
(
images
,
64
,
2
,
[
0.3
,
0
],
main_program
,
conv2
=
conv_block
(
conv1
,
256
,
3
,
[
0.4
,
0.4
,
0
],
program
,
init_program
)
startup_program
)
conv2
=
conv_block
(
conv1
,
256
,
3
,
[
0.4
,
0.4
,
0
],
main_program
,
startup_program
)
# print str(program)
# print str(
main_
program)
def
test_elementwise_add_with_act
(
self
):
def
test_elementwise_add_with_act
(
self
):
program
=
Program
()
main_
program
=
Program
()
init
_program
=
Program
()
startup
_program
=
Program
()
image1
=
layers
.
data
(
image1
=
layers
.
data
(
name
=
'pixel1'
,
name
=
'pixel1'
,
shape
=
[
3
,
48
,
48
],
shape
=
[
3
,
48
,
48
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
image2
=
layers
.
data
(
image2
=
layers
.
data
(
name
=
'pixel2'
,
name
=
'pixel2'
,
shape
=
[
3
,
48
,
48
],
shape
=
[
3
,
48
,
48
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
out
=
layers
.
elementwise_add
(
out
=
layers
.
elementwise_add
(
x
=
image1
,
x
=
image1
,
y
=
image2
,
y
=
image2
,
act
=
'relu'
,
act
=
'relu'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
# print(program)
# print(
main_
program)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
python/paddle/v2/framework/tests/test_image_classification_train.py
浏览文件 @
51d4afaa
...
@@ -5,19 +5,19 @@ import paddle.v2.framework.layers as layers
...
@@ -5,19 +5,19 @@ import paddle.v2.framework.layers as layers
import
paddle.v2.framework.nets
as
nets
import
paddle.v2.framework.nets
as
nets
import
paddle.v2.framework.optimizer
as
optimizer
import
paddle.v2.framework.optimizer
as
optimizer
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.framework
import
g_
init_program
,
g
_program
from
paddle.v2.framework.framework
import
g_
startup_program
,
g_main
_program
from
paddle.v2.framework.initializer
import
XavierInitializer
from
paddle.v2.framework.initializer
import
XavierInitializer
def
resnet_cifar10
(
input
,
depth
=
32
,
program
=
None
,
init
_program
=
None
):
def
resnet_cifar10
(
input
,
depth
=
32
,
main_program
=
None
,
startup
_program
=
None
):
def
conv_bn_layer
(
input
,
def
conv_bn_layer
(
input
,
ch_out
,
ch_out
,
filter_size
,
filter_size
,
stride
,
stride
,
padding
,
padding
,
act
=
'relu'
,
act
=
'relu'
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
tmp
=
layers
.
conv2d
(
tmp
=
layers
.
conv2d
(
input
=
input
,
input
=
input
,
filter_size
=
filter_size
,
filter_size
=
filter_size
,
...
@@ -26,10 +26,13 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
...
@@ -26,10 +26,13 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
padding
=
padding
,
padding
=
padding
,
act
=
None
,
act
=
None
,
bias_attr
=
False
,
bias_attr
=
False
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
return
layers
.
batch_norm
(
return
layers
.
batch_norm
(
input
=
tmp
,
act
=
act
,
program
=
program
,
init_program
=
init_program
)
input
=
tmp
,
act
=
act
,
main_program
=
main_program
,
startup_program
=
startup_program
)
def
shortcut
(
input
,
ch_in
,
ch_out
,
stride
,
program
,
init_program
):
def
shortcut
(
input
,
ch_in
,
ch_out
,
stride
,
program
,
init_program
):
if
ch_in
!=
ch_out
:
if
ch_in
!=
ch_out
:
...
@@ -42,16 +45,16 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
...
@@ -42,16 +45,16 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
ch_in
,
ch_in
,
ch_out
,
ch_out
,
stride
,
stride
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
):
startup_program
=
startup
_program
):
tmp
=
conv_bn_layer
(
tmp
=
conv_bn_layer
(
input
,
input
,
ch_out
,
ch_out
,
3
,
3
,
stride
,
stride
,
1
,
1
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
tmp
=
conv_bn_layer
(
tmp
=
conv_bn_layer
(
tmp
,
tmp
,
ch_out
,
ch_out
,
...
@@ -59,21 +62,22 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
...
@@ -59,21 +62,22 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
1
,
1
,
1
,
1
,
act
=
None
,
act
=
None
,
program
=
program
,
main_program
=
main_program
,
init_program
=
init_program
)
startup_program
=
startup_program
)
short
=
shortcut
(
input
,
ch_in
,
ch_out
,
stride
,
program
,
init_program
)
short
=
shortcut
(
input
,
ch_in
,
ch_out
,
stride
,
main_program
,
startup_program
)
return
layers
.
elementwise_add
(
return
layers
.
elementwise_add
(
x
=
tmp
,
x
=
tmp
,
y
=
short
,
y
=
short
,
act
=
'relu'
,
act
=
'relu'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
def
layer_warp
(
block_func
,
input
,
ch_in
,
ch_out
,
count
,
stride
,
program
,
def
layer_warp
(
block_func
,
input
,
ch_in
,
ch_out
,
count
,
stride
,
program
,
init
_program
):
startup
_program
):
tmp
=
block_func
(
input
,
ch_in
,
ch_out
,
stride
,
program
,
init
_program
)
tmp
=
block_func
(
input
,
ch_in
,
ch_out
,
stride
,
program
,
startup
_program
)
for
i
in
range
(
1
,
count
):
for
i
in
range
(
1
,
count
):
tmp
=
block_func
(
tmp
,
ch_out
,
ch_out
,
1
,
program
,
init
_program
)
tmp
=
block_func
(
tmp
,
ch_out
,
ch_out
,
1
,
program
,
startup
_program
)
return
tmp
return
tmp
assert
(
depth
-
2
)
%
6
==
0
assert
(
depth
-
2
)
%
6
==
0
...
@@ -84,8 +88,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
...
@@ -84,8 +88,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
filter_size
=
3
,
filter_size
=
3
,
stride
=
1
,
stride
=
1
,
padding
=
1
,
padding
=
1
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
res1
=
layer_warp
(
res1
=
layer_warp
(
basicblock
,
basicblock
,
conv1
,
conv1
,
...
@@ -93,8 +97,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
...
@@ -93,8 +97,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
16
,
16
,
n
,
n
,
1
,
1
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
res2
=
layer_warp
(
res2
=
layer_warp
(
basicblock
,
basicblock
,
res1
,
res1
,
...
@@ -102,8 +106,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
...
@@ -102,8 +106,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
32
,
32
,
n
,
n
,
2
,
2
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
res3
=
layer_warp
(
res3
=
layer_warp
(
basicblock
,
basicblock
,
res2
,
res2
,
...
@@ -111,25 +115,25 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
...
@@ -111,25 +115,25 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
64
,
64
,
n
,
n
,
2
,
2
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
pool
=
layers
.
pool2d
(
pool
=
layers
.
pool2d
(
input
=
res3
,
input
=
res3
,
pool_size
=
8
,
pool_size
=
8
,
pool_type
=
'avg'
,
pool_type
=
'avg'
,
pool_stride
=
1
,
pool_stride
=
1
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
return
pool
return
pool
def
vgg16_bn_drop
(
input
,
program
=
None
,
init
_program
=
None
):
def
vgg16_bn_drop
(
input
,
main_program
=
None
,
startup
_program
=
None
):
def
conv_block
(
input
,
def
conv_block
(
input
,
num_filter
,
num_filter
,
groups
,
groups
,
dropouts
,
dropouts
,
program
=
None
,
main_
program
=
None
,
init
_program
=
None
):
startup
_program
=
None
):
return
nets
.
img_conv_group
(
return
nets
.
img_conv_group
(
input
=
input
,
input
=
input
,
pool_size
=
2
,
pool_size
=
2
,
...
@@ -140,38 +144,50 @@ def vgg16_bn_drop(input, program=None, init_program=None):
...
@@ -140,38 +144,50 @@ def vgg16_bn_drop(input, program=None, init_program=None):
conv_with_batchnorm
=
True
,
conv_with_batchnorm
=
True
,
conv_batchnorm_drop_rate
=
dropouts
,
conv_batchnorm_drop_rate
=
dropouts
,
pool_type
=
'max'
,
pool_type
=
'max'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
conv1
=
conv_block
(
input
,
64
,
2
,
[
0.3
,
0
],
program
,
init_program
)
conv1
=
conv_block
(
input
,
64
,
2
,
[
0.3
,
0
],
main_program
,
startup_program
)
conv2
=
conv_block
(
conv1
,
128
,
2
,
[
0.4
,
0
],
program
,
init_program
)
conv2
=
conv_block
(
conv1
,
128
,
2
,
[
0.4
,
0
],
main_program
,
startup_program
)
conv3
=
conv_block
(
conv2
,
256
,
3
,
[
0.4
,
0.4
,
0
],
program
,
init_program
)
conv3
=
conv_block
(
conv2
,
256
,
3
,
[
0.4
,
0.4
,
0
],
main_program
,
conv4
=
conv_block
(
conv3
,
512
,
3
,
[
0.4
,
0.4
,
0
],
program
,
init_program
)
startup_program
)
conv5
=
conv_block
(
conv4
,
512
,
3
,
[
0.4
,
0.4
,
0
],
program
,
init_program
)
conv4
=
conv_block
(
conv3
,
512
,
3
,
[
0.4
,
0.4
,
0
],
main_program
,
startup_program
)
conv5
=
conv_block
(
conv4
,
512
,
3
,
[
0.4
,
0.4
,
0
],
main_program
,
startup_program
)
drop
=
layers
.
dropout
(
drop
=
layers
.
dropout
(
x
=
conv5
,
dropout_prob
=
0.5
,
program
=
program
,
init_program
=
init_program
)
x
=
conv5
,
dropout_prob
=
0.5
,
main_program
=
main_program
,
startup_program
=
startup_program
)
fc1
=
layers
.
fc
(
input
=
drop
,
fc1
=
layers
.
fc
(
input
=
drop
,
size
=
512
,
size
=
512
,
act
=
None
,
act
=
None
,
param_attr
=
{
"initializer"
:
XavierInitializer
()},
param_attr
=
{
"initializer"
:
XavierInitializer
()},
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
reshape1
=
layers
.
reshape
(
reshape1
=
layers
.
reshape
(
x
=
fc1
,
x
=
fc1
,
shape
=
list
(
fc1
.
shape
+
(
1
,
1
)),
shape
=
list
(
fc1
.
shape
+
(
1
,
1
)),
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
bn
=
layers
.
batch_norm
(
bn
=
layers
.
batch_norm
(
input
=
reshape1
,
act
=
'relu'
,
program
=
program
,
init_program
=
init_program
)
input
=
reshape1
,
act
=
'relu'
,
main_program
=
main_program
,
startup_program
=
startup_program
)
drop2
=
layers
.
dropout
(
drop2
=
layers
.
dropout
(
x
=
bn
,
dropout_prob
=
0.5
,
program
=
program
,
init_program
=
init_program
)
x
=
bn
,
dropout_prob
=
0.5
,
main_program
=
main_program
,
startup_program
=
startup_program
)
fc2
=
layers
.
fc
(
input
=
drop2
,
fc2
=
layers
.
fc
(
input
=
drop2
,
size
=
512
,
size
=
512
,
act
=
None
,
act
=
None
,
param_attr
=
{
"initializer"
:
XavierInitializer
()},
param_attr
=
{
"initializer"
:
XavierInitializer
()},
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
return
fc2
return
fc2
...
@@ -209,7 +225,7 @@ train_reader = paddle.batch(
...
@@ -209,7 +225,7 @@ train_reader = paddle.batch(
place
=
core
.
CPUPlace
()
place
=
core
.
CPUPlace
()
exe
=
Executor
(
place
)
exe
=
Executor
(
place
)
exe
.
run
(
g_
init
_program
,
feed
=
{},
fetch_list
=
[])
exe
.
run
(
g_
startup
_program
,
feed
=
{},
fetch_list
=
[])
for
pass_id
in
range
(
PASS_NUM
):
for
pass_id
in
range
(
PASS_NUM
):
batch_id
=
0
batch_id
=
0
...
@@ -227,7 +243,7 @@ for pass_id in range(PASS_NUM):
...
@@ -227,7 +243,7 @@ for pass_id in range(PASS_NUM):
tensor_img
.
set
(
img_data
,
place
)
tensor_img
.
set
(
img_data
,
place
)
tensor_y
.
set
(
y_data
,
place
)
tensor_y
.
set
(
y_data
,
place
)
outs
=
exe
.
run
(
g_program
,
outs
=
exe
.
run
(
g_
main_
program
,
feed
=
{
"pixel"
:
tensor_img
,
feed
=
{
"pixel"
:
tensor_img
,
"label"
:
tensor_y
},
"label"
:
tensor_y
},
fetch_list
=
[
avg_cost
,
accuracy
])
fetch_list
=
[
avg_cost
,
accuracy
])
...
...
python/paddle/v2/framework/tests/test_inference_model_io.py
浏览文件 @
51d4afaa
...
@@ -3,7 +3,7 @@ import paddle.v2.framework.layers as layers
...
@@ -3,7 +3,7 @@ import paddle.v2.framework.layers as layers
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.optimizer
as
optimizer
import
paddle.v2.framework.optimizer
as
optimizer
from
paddle.v2.framework.framework
import
Program
,
g_program
from
paddle.v2.framework.framework
import
Program
,
g_
main_
program
from
paddle.v2.framework.io
import
save_inference_model
,
load_inference_model
from
paddle.v2.framework.io
import
save_inference_model
,
load_inference_model
import
paddle.v2.framework.executor
as
executor
import
paddle.v2.framework.executor
as
executor
import
unittest
import
unittest
...
@@ -20,28 +20,28 @@ class TestBook(unittest.TestCase):
...
@@ -20,28 +20,28 @@ class TestBook(unittest.TestCase):
name
=
'x'
,
name
=
'x'
,
shape
=
[
2
],
shape
=
[
2
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_
program
=
program
,
init
_program
=
init_program
)
startup
_program
=
init_program
)
y
=
layers
.
data
(
y
=
layers
.
data
(
name
=
'y'
,
name
=
'y'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_
program
=
program
,
init
_program
=
init_program
)
startup
_program
=
init_program
)
y_predict
=
layers
.
fc
(
input
=
x
,
y_predict
=
layers
.
fc
(
input
=
x
,
size
=
1
,
size
=
1
,
act
=
None
,
act
=
None
,
program
=
program
,
main_
program
=
program
,
init
_program
=
init_program
)
startup
_program
=
init_program
)
cost
=
layers
.
square_error_cost
(
cost
=
layers
.
square_error_cost
(
input
=
y_predict
,
input
=
y_predict
,
label
=
y
,
label
=
y
,
program
=
program
,
main_
program
=
program
,
init
_program
=
init_program
)
startup
_program
=
init_program
)
avg_cost
=
layers
.
mean
(
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
,
init
_program
=
init_program
)
x
=
cost
,
main_program
=
program
,
startup
_program
=
init_program
)
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.001
)
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.001
)
opts
=
sgd_optimizer
.
minimize
(
avg_cost
,
init_program
)
opts
=
sgd_optimizer
.
minimize
(
avg_cost
,
init_program
)
...
...
python/paddle/v2/framework/tests/test_layers.py
浏览文件 @
51d4afaa
import
paddle.v2.framework.layers
as
layers
import
paddle.v2.framework.layers
as
layers
import
paddle.v2.framework.nets
as
nets
import
paddle.v2.framework.nets
as
nets
from
paddle.v2.framework.framework
import
Program
,
g_program
from
paddle.v2.framework.framework
import
Program
,
g_
main_
program
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
unittest
import
unittest
...
@@ -9,15 +9,15 @@ class TestBook(unittest.TestCase):
...
@@ -9,15 +9,15 @@ class TestBook(unittest.TestCase):
def
test_fit_a_line
(
self
):
def
test_fit_a_line
(
self
):
program
=
Program
()
program
=
Program
()
x
=
layers
.
data
(
x
=
layers
.
data
(
name
=
'x'
,
shape
=
[
13
],
data_type
=
'float32'
,
program
=
program
)
name
=
'x'
,
shape
=
[
13
],
data_type
=
'float32'
,
main_
program
=
program
)
y_predict
=
layers
.
fc
(
input
=
x
,
size
=
1
,
act
=
None
,
program
=
program
)
y_predict
=
layers
.
fc
(
input
=
x
,
size
=
1
,
act
=
None
,
main_
program
=
program
)
y
=
layers
.
data
(
y
=
layers
.
data
(
name
=
'y'
,
shape
=
[
1
],
data_type
=
'float32'
,
program
=
program
)
name
=
'y'
,
shape
=
[
1
],
data_type
=
'float32'
,
main_
program
=
program
)
cost
=
layers
.
square_error_cost
(
cost
=
layers
.
square_error_cost
(
input
=
y_predict
,
label
=
y
,
program
=
program
)
input
=
y_predict
,
label
=
y
,
main_
program
=
program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
main_
program
=
program
)
self
.
assertIsNotNone
(
avg_cost
)
self
.
assertIsNotNone
(
avg_cost
)
program
.
append_backward
(
avg_cost
)
program
.
append_backward
(
avg_cost
)
print
str
(
program
)
print
str
(
program
)
...
@@ -27,26 +27,42 @@ class TestBook(unittest.TestCase):
...
@@ -27,26 +27,42 @@ class TestBook(unittest.TestCase):
# Change g_program, so the rest layers use `g_program`
# Change g_program, so the rest layers use `g_program`
images
=
layers
.
data
(
images
=
layers
.
data
(
name
=
'pixel'
,
shape
=
[
784
],
data_type
=
'float32'
,
program
=
program
)
name
=
'pixel'
,
shape
=
[
784
],
data_type
=
'float32'
,
main_program
=
program
)
label
=
layers
.
data
(
label
=
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
data_type
=
'int32'
,
program
=
program
)
name
=
'label'
,
shape
=
[
1
],
data_type
=
'int32'
,
main_program
=
program
)
hidden1
=
layers
.
fc
(
input
=
images
,
size
=
128
,
act
=
'relu'
,
program
=
program
)
hidden1
=
layers
.
fc
(
input
=
images
,
hidden2
=
layers
.
fc
(
input
=
hidden1
,
size
=
64
,
act
=
'relu'
,
program
=
program
)
size
=
128
,
act
=
'relu'
,
main_program
=
program
)
hidden2
=
layers
.
fc
(
input
=
hidden1
,
size
=
64
,
act
=
'relu'
,
main_program
=
program
)
predict
=
layers
.
fc
(
input
=
hidden2
,
predict
=
layers
.
fc
(
input
=
hidden2
,
size
=
10
,
size
=
10
,
act
=
'softmax'
,
act
=
'softmax'
,
program
=
program
)
main_program
=
program
)
cost
=
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
,
program
=
program
)
cost
=
layers
.
cross_entropy
(
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
)
input
=
predict
,
label
=
label
,
main_program
=
program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
main_program
=
program
)
self
.
assertIsNotNone
(
avg_cost
)
self
.
assertIsNotNone
(
avg_cost
)
print
str
(
program
)
print
str
(
program
)
def
test_simple_conv2d
(
self
):
def
test_simple_conv2d
(
self
):
program
=
Program
()
program
=
Program
()
images
=
layers
.
data
(
images
=
layers
.
data
(
name
=
'pixel'
,
shape
=
[
3
,
48
,
48
],
data_type
=
'int32'
,
program
=
program
)
name
=
'pixel'
,
shape
=
[
3
,
48
,
48
],
data_type
=
'int32'
,
main_program
=
program
)
layers
.
conv2d
(
layers
.
conv2d
(
input
=
images
,
num_filters
=
3
,
filter_size
=
[
4
,
4
],
program
=
program
)
input
=
images
,
num_filters
=
3
,
filter_size
=
[
4
,
4
],
main_program
=
program
)
print
str
(
program
)
print
str
(
program
)
...
@@ -57,9 +73,9 @@ class TestBook(unittest.TestCase):
...
@@ -57,9 +73,9 @@ class TestBook(unittest.TestCase):
name
=
'pixel'
,
name
=
'pixel'
,
shape
=
[
1
,
28
,
28
],
shape
=
[
1
,
28
,
28
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
)
main_
program
=
program
)
label
=
layers
.
data
(
label
=
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
data_type
=
'int32'
,
program
=
program
)
name
=
'label'
,
shape
=
[
1
],
data_type
=
'int32'
,
main_
program
=
program
)
conv_pool_1
=
nets
.
simple_img_conv_pool
(
conv_pool_1
=
nets
.
simple_img_conv_pool
(
input
=
images
,
input
=
images
,
filter_size
=
5
,
filter_size
=
5
,
...
@@ -67,7 +83,7 @@ class TestBook(unittest.TestCase):
...
@@ -67,7 +83,7 @@ class TestBook(unittest.TestCase):
pool_size
=
2
,
pool_size
=
2
,
pool_stride
=
2
,
pool_stride
=
2
,
act
=
"relu"
,
act
=
"relu"
,
program
=
program
)
main_
program
=
program
)
conv_pool_2
=
nets
.
simple_img_conv_pool
(
conv_pool_2
=
nets
.
simple_img_conv_pool
(
input
=
conv_pool_1
,
input
=
conv_pool_1
,
filter_size
=
5
,
filter_size
=
5
,
...
@@ -75,14 +91,15 @@ class TestBook(unittest.TestCase):
...
@@ -75,14 +91,15 @@ class TestBook(unittest.TestCase):
pool_size
=
2
,
pool_size
=
2
,
pool_stride
=
2
,
pool_stride
=
2
,
act
=
"relu"
,
act
=
"relu"
,
program
=
program
)
main_
program
=
program
)
predict
=
layers
.
fc
(
input
=
conv_pool_2
,
predict
=
layers
.
fc
(
input
=
conv_pool_2
,
size
=
10
,
size
=
10
,
act
=
"softmax"
,
act
=
"softmax"
,
program
=
program
)
main_program
=
program
)
cost
=
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
,
program
=
program
)
cost
=
layers
.
cross_entropy
(
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
)
input
=
predict
,
label
=
label
,
main_program
=
program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
main_program
=
program
)
program
.
append_backward
(
avg_cost
)
program
.
append_backward
(
avg_cost
)
...
@@ -93,58 +110,58 @@ class TestBook(unittest.TestCase):
...
@@ -93,58 +110,58 @@ class TestBook(unittest.TestCase):
dict_size
=
10000
dict_size
=
10000
embed_size
=
32
embed_size
=
32
first_word
=
layers
.
data
(
first_word
=
layers
.
data
(
name
=
'firstw'
,
shape
=
[
1
],
data_type
=
'int64'
,
program
=
program
)
name
=
'firstw'
,
shape
=
[
1
],
data_type
=
'int64'
,
main_
program
=
program
)
second_word
=
layers
.
data
(
second_word
=
layers
.
data
(
name
=
'secondw'
,
shape
=
[
1
],
data_type
=
'int64'
,
program
=
program
)
name
=
'secondw'
,
shape
=
[
1
],
data_type
=
'int64'
,
main_
program
=
program
)
third_word
=
layers
.
data
(
third_word
=
layers
.
data
(
name
=
'thirdw'
,
shape
=
[
1
],
data_type
=
'int64'
,
program
=
program
)
name
=
'thirdw'
,
shape
=
[
1
],
data_type
=
'int64'
,
main_
program
=
program
)
forth_word
=
layers
.
data
(
forth_word
=
layers
.
data
(
name
=
'forthw'
,
shape
=
[
1
],
data_type
=
'int64'
,
program
=
program
)
name
=
'forthw'
,
shape
=
[
1
],
data_type
=
'int64'
,
main_
program
=
program
)
next_word
=
layers
.
data
(
next_word
=
layers
.
data
(
name
=
'nextw'
,
shape
=
[
1
],
data_type
=
'int64'
,
program
=
program
)
name
=
'nextw'
,
shape
=
[
1
],
data_type
=
'int64'
,
main_
program
=
program
)
embed_first
=
layers
.
embedding
(
embed_first
=
layers
.
embedding
(
input
=
first_word
,
input
=
first_word
,
size
=
[
dict_size
,
embed_size
],
size
=
[
dict_size
,
embed_size
],
data_type
=
'float32'
,
data_type
=
'float32'
,
param_attr
=
{
'name'
:
'shared_w'
},
param_attr
=
{
'name'
:
'shared_w'
},
program
=
program
)
main_
program
=
program
)
embed_second
=
layers
.
embedding
(
embed_second
=
layers
.
embedding
(
input
=
second_word
,
input
=
second_word
,
size
=
[
dict_size
,
embed_size
],
size
=
[
dict_size
,
embed_size
],
data_type
=
'float32'
,
data_type
=
'float32'
,
param_attr
=
{
'name'
:
'shared_w'
},
param_attr
=
{
'name'
:
'shared_w'
},
program
=
program
)
main_
program
=
program
)
embed_third
=
layers
.
embedding
(
embed_third
=
layers
.
embedding
(
input
=
third_word
,
input
=
third_word
,
size
=
[
dict_size
,
embed_size
],
size
=
[
dict_size
,
embed_size
],
data_type
=
'float32'
,
data_type
=
'float32'
,
param_attr
=
{
'name'
:
'shared_w'
},
param_attr
=
{
'name'
:
'shared_w'
},
program
=
program
)
main_
program
=
program
)
embed_forth
=
layers
.
embedding
(
embed_forth
=
layers
.
embedding
(
input
=
forth_word
,
input
=
forth_word
,
size
=
[
dict_size
,
embed_size
],
size
=
[
dict_size
,
embed_size
],
data_type
=
'float32'
,
data_type
=
'float32'
,
param_attr
=
{
'name'
:
'shared_w'
},
param_attr
=
{
'name'
:
'shared_w'
},
program
=
program
)
main_
program
=
program
)
concat_embed
=
layers
.
concat
(
concat_embed
=
layers
.
concat
(
input
=
[
embed_first
,
embed_second
,
embed_third
,
embed_forth
],
input
=
[
embed_first
,
embed_second
,
embed_third
,
embed_forth
],
axis
=
1
,
axis
=
1
,
program
=
program
)
main_
program
=
program
)
hidden1
=
layers
.
fc
(
input
=
concat_embed
,
hidden1
=
layers
.
fc
(
input
=
concat_embed
,
size
=
256
,
size
=
256
,
act
=
'sigmoid'
,
act
=
'sigmoid'
,
program
=
program
)
main_
program
=
program
)
predict_word
=
layers
.
fc
(
input
=
hidden1
,
predict_word
=
layers
.
fc
(
input
=
hidden1
,
size
=
dict_size
,
size
=
dict_size
,
act
=
'softmax'
,
act
=
'softmax'
,
program
=
program
)
main_
program
=
program
)
cost
=
layers
.
cross_entropy
(
cost
=
layers
.
cross_entropy
(
input
=
predict_word
,
label
=
next_word
,
program
=
program
)
input
=
predict_word
,
label
=
next_word
,
main_
program
=
program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
main_
program
=
program
)
self
.
assertIsNotNone
(
avg_cost
)
self
.
assertIsNotNone
(
avg_cost
)
print
str
(
program
)
print
str
(
program
)
...
...
python/paddle/v2/framework/tests/test_lod_rank_table.py
浏览文件 @
51d4afaa
from
paddle.v2.framework.layers
import
lod_rank_table
,
data
from
paddle.v2.framework.layers
import
lod_rank_table
,
data
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.framework
import
g_program
from
paddle.v2.framework.framework
import
g_
main_
program
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
numpy
import
numpy
import
unittest
import
unittest
...
@@ -19,7 +19,7 @@ class TestLoDRankTable(unittest.TestCase):
...
@@ -19,7 +19,7 @@ class TestLoDRankTable(unittest.TestCase):
tensor
.
set
(
numpy
.
random
.
random
(
size
=
(
17
,
100
)),
cpu
)
tensor
.
set
(
numpy
.
random
.
random
(
size
=
(
17
,
100
)),
cpu
)
tensor
.
set_lod
([[
0
,
1
,
3
],
[
0
,
5
,
6
,
7
],
[
0
,
3
,
4
,
9
,
10
,
13
,
16
,
17
]])
tensor
.
set_lod
([[
0
,
1
,
3
],
[
0
,
5
,
6
,
7
],
[
0
,
3
,
4
,
9
,
10
,
13
,
16
,
17
]])
exe
.
run
(
g_program
,
scope
=
scope
,
feed
=
{
'x'
:
tensor
})
exe
.
run
(
g_
main_
program
,
scope
=
scope
,
feed
=
{
'x'
:
tensor
})
var
=
scope
.
find_var
(
rank_table
.
name
)
var
=
scope
.
find_var
(
rank_table
.
name
)
table
=
var
.
get_lod_rank_table
()
table
=
var
.
get_lod_rank_table
()
self
.
assertEqual
([(
0
,
5
),
(
1
,
1
),
(
2
,
1
)],
table
.
items
())
self
.
assertEqual
([(
0
,
5
),
(
1
,
1
),
(
2
,
1
)],
table
.
items
())
...
...
python/paddle/v2/framework/tests/test_operator_desc.py
浏览文件 @
51d4afaa
import
unittest
import
unittest
from
paddle.v2.framework.framework
import
Variable
,
Program
,
g_program
from
paddle.v2.framework.framework
import
Variable
,
Program
,
g_
main_
program
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
class
TestOperator
(
unittest
.
TestCase
):
class
TestOperator
(
unittest
.
TestCase
):
def
test_error_type
(
self
):
def
test_error_type
(
self
):
block
=
g_program
.
create_block
()
block
=
g_
main_
program
.
create_block
()
try
:
try
:
block
.
append_op
()
block
.
append_op
()
self
.
assertFail
()
self
.
assertFail
()
...
...
python/paddle/v2/framework/tests/test_parameter.py
浏览文件 @
51d4afaa
import
unittest
import
unittest
from
paddle.v2.framework.framework
import
g_program
from
paddle.v2.framework.framework
import
g_
main_
program
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
class
TestParameter
(
unittest
.
TestCase
):
class
TestParameter
(
unittest
.
TestCase
):
def
test_param
(
self
):
def
test_param
(
self
):
b
=
g_program
.
create_block
()
b
=
g_
main_
program
.
create_block
()
param
=
b
.
create_parameter
(
param
=
b
.
create_parameter
(
name
=
'fc.w'
,
name
=
'fc.w'
,
shape
=
[
784
,
100
],
shape
=
[
784
,
100
],
...
...
python/paddle/v2/framework/tests/test_program.py
浏览文件 @
51d4afaa
...
@@ -2,35 +2,35 @@ import unittest
...
@@ -2,35 +2,35 @@ import unittest
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
from
paddle.v2.framework.framework
import
Program
from
paddle.v2.framework.framework
import
Program
from
paddle.v2.framework.framework
import
g_program
from
paddle.v2.framework.framework
import
g_
main_
program
class
TestProgram
(
unittest
.
TestCase
):
class
TestProgram
(
unittest
.
TestCase
):
def
test_program
(
self
):
def
test_program
(
self
):
b
=
g_program
.
current_block
()
b
=
g_
main_
program
.
current_block
()
self
.
assertEqual
(
-
1
,
b
.
parent_idx
)
self
.
assertEqual
(
-
1
,
b
.
parent_idx
)
self
.
assertEqual
(
0
,
b
.
idx
)
self
.
assertEqual
(
0
,
b
.
idx
)
b
=
g_program
.
create_block
()
b
=
g_
main_
program
.
create_block
()
self
.
assertEqual
(
1
,
b
.
idx
)
self
.
assertEqual
(
1
,
b
.
idx
)
self
.
assertEqual
(
0
,
b
.
parent_idx
)
self
.
assertEqual
(
0
,
b
.
parent_idx
)
b
=
g_program
.
create_block
()
b
=
g_
main_
program
.
create_block
()
self
.
assertEqual
(
2
,
b
.
idx
)
self
.
assertEqual
(
2
,
b
.
idx
)
self
.
assertEqual
(
1
,
b
.
parent_idx
)
self
.
assertEqual
(
1
,
b
.
parent_idx
)
g_program
.
rollback
()
g_
main_
program
.
rollback
()
b
=
g_program
.
current_block
()
b
=
g_
main_
program
.
current_block
()
self
.
assertEqual
(
1
,
b
.
idx
)
self
.
assertEqual
(
1
,
b
.
idx
)
self
.
assertEqual
(
0
,
b
.
parent_idx
)
self
.
assertEqual
(
0
,
b
.
parent_idx
)
b
=
g_program
.
create_block
()
b
=
g_
main_
program
.
create_block
()
self
.
assertEqual
(
3
,
b
.
idx
)
self
.
assertEqual
(
3
,
b
.
idx
)
self
.
assertEqual
(
1
,
b
.
parent_idx
)
self
.
assertEqual
(
1
,
b
.
parent_idx
)
g_program
.
rollback
()
g_
main_
program
.
rollback
()
b
=
g_program
.
current_block
()
b
=
g_
main_
program
.
current_block
()
self
.
assertEqual
(
1
,
b
.
idx
)
self
.
assertEqual
(
1
,
b
.
idx
)
self
.
assertEqual
(
0
,
b
.
parent_idx
)
self
.
assertEqual
(
0
,
b
.
parent_idx
)
...
...
python/paddle/v2/framework/tests/test_recognize_digits_conv.py
浏览文件 @
51d4afaa
...
@@ -4,26 +4,26 @@ import paddle.v2.framework.nets as nets
...
@@ -4,26 +4,26 @@ import paddle.v2.framework.nets as nets
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.optimizer
as
optimizer
import
paddle.v2.framework.optimizer
as
optimizer
from
paddle.v2.framework.framework
import
Program
,
g_program
from
paddle.v2.framework.framework
import
Program
,
g_
main_
program
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.executor
import
Executor
import
numpy
as
np
import
numpy
as
np
init
_program
=
Program
()
startup
_program
=
Program
()
program
=
Program
()
main_
program
=
Program
()
images
=
layers
.
data
(
images
=
layers
.
data
(
name
=
'pixel'
,
name
=
'pixel'
,
shape
=
[
1
,
28
,
28
],
shape
=
[
1
,
28
,
28
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
label
=
layers
.
data
(
label
=
layers
.
data
(
name
=
'label'
,
name
=
'label'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
conv_pool_1
=
nets
.
simple_img_conv_pool
(
conv_pool_1
=
nets
.
simple_img_conv_pool
(
input
=
images
,
input
=
images
,
filter_size
=
5
,
filter_size
=
5
,
...
@@ -31,8 +31,8 @@ conv_pool_1 = nets.simple_img_conv_pool(
...
@@ -31,8 +31,8 @@ conv_pool_1 = nets.simple_img_conv_pool(
pool_size
=
2
,
pool_size
=
2
,
pool_stride
=
2
,
pool_stride
=
2
,
act
=
"relu"
,
act
=
"relu"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
conv_pool_2
=
nets
.
simple_img_conv_pool
(
conv_pool_2
=
nets
.
simple_img_conv_pool
(
input
=
conv_pool_1
,
input
=
conv_pool_1
,
filter_size
=
5
,
filter_size
=
5
,
...
@@ -40,24 +40,30 @@ conv_pool_2 = nets.simple_img_conv_pool(
...
@@ -40,24 +40,30 @@ conv_pool_2 = nets.simple_img_conv_pool(
pool_size
=
2
,
pool_size
=
2
,
pool_stride
=
2
,
pool_stride
=
2
,
act
=
"relu"
,
act
=
"relu"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
predict
=
layers
.
fc
(
input
=
conv_pool_2
,
predict
=
layers
.
fc
(
input
=
conv_pool_2
,
size
=
10
,
size
=
10
,
act
=
"softmax"
,
act
=
"softmax"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
cost
=
layers
.
cross_entropy
(
cost
=
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
,
program
=
program
,
init_program
=
init_program
)
input
=
predict
,
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
)
label
=
label
,
main_program
=
main_program
,
startup_program
=
startup_program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
main_program
=
main_program
)
accuracy
=
layers
.
accuracy
(
accuracy
=
layers
.
accuracy
(
input
=
predict
,
label
=
label
,
program
=
program
,
init_program
=
init_program
)
input
=
predict
,
label
=
label
,
main_program
=
main_program
,
startup_program
=
startup_program
)
# optimizer = optimizer.MomentumOptimizer(learning_rate=0.1 / 128.0,
# optimizer = optimizer.MomentumOptimizer(learning_rate=0.1 / 128.0,
# momentum=0.9)
# momentum=0.9)
optimizer
=
optimizer
.
AdamOptimizer
(
learning_rate
=
0.01
,
beta1
=
0.9
,
beta2
=
0.999
)
optimizer
=
optimizer
.
AdamOptimizer
(
learning_rate
=
0.01
,
beta1
=
0.9
,
beta2
=
0.999
)
opts
=
optimizer
.
minimize
(
avg_cost
,
init
_program
)
opts
=
optimizer
.
minimize
(
avg_cost
,
startup
_program
)
BATCH_SIZE
=
50
BATCH_SIZE
=
50
PASS_NUM
=
3
PASS_NUM
=
3
...
@@ -69,7 +75,7 @@ train_reader = paddle.batch(
...
@@ -69,7 +75,7 @@ train_reader = paddle.batch(
place
=
core
.
CPUPlace
()
place
=
core
.
CPUPlace
()
exe
=
Executor
(
place
)
exe
=
Executor
(
place
)
exe
.
run
(
init
_program
,
feed
=
{},
fetch_list
=
[])
exe
.
run
(
startup
_program
,
feed
=
{},
fetch_list
=
[])
for
pass_id
in
range
(
PASS_NUM
):
for
pass_id
in
range
(
PASS_NUM
):
count
=
0
count
=
0
...
@@ -84,7 +90,7 @@ for pass_id in range(PASS_NUM):
...
@@ -84,7 +90,7 @@ for pass_id in range(PASS_NUM):
tensor_img
.
set
(
img_data
,
place
)
tensor_img
.
set
(
img_data
,
place
)
tensor_y
.
set
(
y_data
,
place
)
tensor_y
.
set
(
y_data
,
place
)
outs
=
exe
.
run
(
program
,
outs
=
exe
.
run
(
main_
program
,
feed
=
{
"pixel"
:
tensor_img
,
feed
=
{
"pixel"
:
tensor_img
,
"label"
:
tensor_y
},
"label"
:
tensor_y
},
fetch_list
=
[
avg_cost
,
accuracy
])
fetch_list
=
[
avg_cost
,
accuracy
])
...
...
python/paddle/v2/framework/tests/test_recognize_digits_mlp.py
浏览文件 @
51d4afaa
...
@@ -11,14 +11,14 @@ from paddle.v2.framework.initializer import UniformInitializer
...
@@ -11,14 +11,14 @@ from paddle.v2.framework.initializer import UniformInitializer
import
numpy
as
np
import
numpy
as
np
BATCH_SIZE
=
128
BATCH_SIZE
=
128
init
_program
=
Program
()
startup
_program
=
Program
()
program
=
Program
()
main_
program
=
Program
()
image
=
layers
.
data
(
image
=
layers
.
data
(
name
=
'x'
,
name
=
'x'
,
shape
=
[
784
],
shape
=
[
784
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
param_attr
=
{
param_attr
=
{
'name'
:
None
,
'name'
:
None
,
...
@@ -30,38 +30,45 @@ param_attr = {
...
@@ -30,38 +30,45 @@ param_attr = {
hidden1
=
layers
.
fc
(
input
=
image
,
hidden1
=
layers
.
fc
(
input
=
image
,
size
=
128
,
size
=
128
,
act
=
'relu'
,
act
=
'relu'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
,
startup_program
=
startup
_program
,
param_attr
=
param_attr
)
param_attr
=
param_attr
)
hidden2
=
layers
.
fc
(
input
=
hidden1
,
hidden2
=
layers
.
fc
(
input
=
hidden1
,
size
=
64
,
size
=
64
,
act
=
'relu'
,
act
=
'relu'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
,
startup_program
=
startup
_program
,
param_attr
=
param_attr
)
param_attr
=
param_attr
)
predict
=
layers
.
fc
(
input
=
hidden2
,
predict
=
layers
.
fc
(
input
=
hidden2
,
size
=
10
,
size
=
10
,
act
=
'softmax'
,
act
=
'softmax'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
,
startup_program
=
startup
_program
,
param_attr
=
param_attr
)
param_attr
=
param_attr
)
label
=
layers
.
data
(
label
=
layers
.
data
(
name
=
'y'
,
name
=
'y'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
cost
=
layers
.
cross_entropy
(
cost
=
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
,
program
=
program
,
init_program
=
init_program
)
input
=
predict
,
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
,
init_program
=
init_program
)
label
=
label
,
main_program
=
main_program
,
startup_program
=
startup_program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
main_program
=
main_program
,
startup_program
=
startup_program
)
accuracy
=
layers
.
accuracy
(
accuracy
=
layers
.
accuracy
(
input
=
predict
,
label
=
label
,
program
=
program
,
init_program
=
init_program
)
input
=
predict
,
label
=
label
,
main_program
=
main_program
,
startup_program
=
startup_program
)
optimizer
=
optimizer
.
MomentumOptimizer
(
learning_rate
=
0.001
,
momentum
=
0.9
)
optimizer
=
optimizer
.
MomentumOptimizer
(
learning_rate
=
0.001
,
momentum
=
0.9
)
opts
=
optimizer
.
minimize
(
avg_cost
,
init
_program
)
opts
=
optimizer
.
minimize
(
avg_cost
,
startup
_program
)
train_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
reader
.
shuffle
(
...
@@ -71,7 +78,7 @@ train_reader = paddle.batch(
...
@@ -71,7 +78,7 @@ train_reader = paddle.batch(
place
=
core
.
CPUPlace
()
place
=
core
.
CPUPlace
()
exe
=
Executor
(
place
)
exe
=
Executor
(
place
)
exe
.
run
(
init
_program
,
feed
=
{},
fetch_list
=
[])
exe
.
run
(
startup
_program
,
feed
=
{},
fetch_list
=
[])
PASS_NUM
=
100
PASS_NUM
=
100
for
pass_id
in
range
(
PASS_NUM
):
for
pass_id
in
range
(
PASS_NUM
):
...
@@ -86,7 +93,7 @@ for pass_id in range(PASS_NUM):
...
@@ -86,7 +93,7 @@ for pass_id in range(PASS_NUM):
tensor_y
=
core
.
LoDTensor
()
tensor_y
=
core
.
LoDTensor
()
tensor_y
.
set
(
y_data
,
place
)
tensor_y
.
set
(
y_data
,
place
)
outs
=
exe
.
run
(
program
,
outs
=
exe
.
run
(
main_
program
,
feed
=
{
'x'
:
tensor_x
,
feed
=
{
'x'
:
tensor_x
,
'y'
:
tensor_y
},
'y'
:
tensor_y
},
fetch_list
=
[
avg_cost
,
accuracy
])
fetch_list
=
[
avg_cost
,
accuracy
])
...
...
python/paddle/v2/framework/tests/test_recommender_system.py
浏览文件 @
51d4afaa
...
@@ -4,13 +4,13 @@ import paddle.v2.framework.nets as nets
...
@@ -4,13 +4,13 @@ import paddle.v2.framework.nets as nets
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.optimizer
as
optimizer
import
paddle.v2.framework.optimizer
as
optimizer
from
paddle.v2.framework.framework
import
Program
,
g_program
from
paddle.v2.framework.framework
import
Program
,
g_
main_
program
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.executor
import
Executor
import
numpy
as
np
import
numpy
as
np
init
_program
=
Program
()
startup
_program
=
Program
()
program
=
Program
()
main_
program
=
Program
()
is_sparse
=
True
is_sparse
=
True
use_gpu
=
False
use_gpu
=
False
BATCH_SIZE
=
256
BATCH_SIZE
=
256
...
@@ -26,8 +26,8 @@ def get_usr_combined_features():
...
@@ -26,8 +26,8 @@ def get_usr_combined_features():
name
=
'user_id'
,
name
=
'user_id'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_emb
=
layers
.
embedding
(
usr_emb
=
layers
.
embedding
(
input
=
uid
,
input
=
uid
,
...
@@ -35,13 +35,13 @@ def get_usr_combined_features():
...
@@ -35,13 +35,13 @@ def get_usr_combined_features():
size
=
[
USR_DICT_SIZE
,
32
],
size
=
[
USR_DICT_SIZE
,
32
],
param_attr
=
{
'name'
:
'user_table'
},
param_attr
=
{
'name'
:
'user_table'
},
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_fc
=
layers
.
fc
(
input
=
usr_emb
,
usr_fc
=
layers
.
fc
(
input
=
usr_emb
,
size
=
32
,
size
=
32
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
USR_GENDER_DICT_SIZE
=
2
USR_GENDER_DICT_SIZE
=
2
...
@@ -49,75 +49,75 @@ def get_usr_combined_features():
...
@@ -49,75 +49,75 @@ def get_usr_combined_features():
name
=
'gender_id'
,
name
=
'gender_id'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_gender_emb
=
layers
.
embedding
(
usr_gender_emb
=
layers
.
embedding
(
input
=
usr_gender_id
,
input
=
usr_gender_id
,
size
=
[
USR_GENDER_DICT_SIZE
,
16
],
size
=
[
USR_GENDER_DICT_SIZE
,
16
],
param_attr
=
{
'name'
:
'gender_table'
},
param_attr
=
{
'name'
:
'gender_table'
},
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_gender_fc
=
layers
.
fc
(
input
=
usr_gender_emb
,
usr_gender_fc
=
layers
.
fc
(
input
=
usr_gender_emb
,
size
=
16
,
size
=
16
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
USR_AGE_DICT_SIZE
=
len
(
paddle
.
dataset
.
movielens
.
age_table
)
USR_AGE_DICT_SIZE
=
len
(
paddle
.
dataset
.
movielens
.
age_table
)
usr_age_id
=
layers
.
data
(
usr_age_id
=
layers
.
data
(
name
=
'age_id'
,
name
=
'age_id'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
"int64"
,
data_type
=
"int64"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_age_emb
=
layers
.
embedding
(
usr_age_emb
=
layers
.
embedding
(
input
=
usr_age_id
,
input
=
usr_age_id
,
size
=
[
USR_AGE_DICT_SIZE
,
16
],
size
=
[
USR_AGE_DICT_SIZE
,
16
],
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
param_attr
=
{
'name'
:
'age_table'
},
param_attr
=
{
'name'
:
'age_table'
},
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_age_fc
=
layers
.
fc
(
input
=
usr_age_emb
,
usr_age_fc
=
layers
.
fc
(
input
=
usr_age_emb
,
size
=
16
,
size
=
16
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
USR_JOB_DICT_SIZE
=
paddle
.
dataset
.
movielens
.
max_job_id
()
+
1
USR_JOB_DICT_SIZE
=
paddle
.
dataset
.
movielens
.
max_job_id
()
+
1
usr_job_id
=
layers
.
data
(
usr_job_id
=
layers
.
data
(
name
=
'job_id'
,
name
=
'job_id'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
"int64"
,
data_type
=
"int64"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_job_emb
=
layers
.
embedding
(
usr_job_emb
=
layers
.
embedding
(
input
=
usr_job_id
,
input
=
usr_job_id
,
size
=
[
USR_JOB_DICT_SIZE
,
16
],
size
=
[
USR_JOB_DICT_SIZE
,
16
],
param_attr
=
{
'name'
:
'job_table'
},
param_attr
=
{
'name'
:
'job_table'
},
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_job_fc
=
layers
.
fc
(
input
=
usr_job_emb
,
usr_job_fc
=
layers
.
fc
(
input
=
usr_job_emb
,
size
=
16
,
size
=
16
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
concat_embed
=
layers
.
concat
(
concat_embed
=
layers
.
concat
(
input
=
[
usr_fc
,
usr_gender_fc
,
usr_age_fc
,
usr_job_fc
],
input
=
[
usr_fc
,
usr_gender_fc
,
usr_age_fc
,
usr_job_fc
],
axis
=
1
,
axis
=
1
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
usr_combined_features
=
layers
.
fc
(
input
=
concat_embed
,
usr_combined_features
=
layers
.
fc
(
input
=
concat_embed
,
size
=
200
,
size
=
200
,
act
=
"tanh"
,
act
=
"tanh"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
return
usr_combined_features
return
usr_combined_features
...
@@ -130,8 +130,8 @@ def get_mov_combined_features():
...
@@ -130,8 +130,8 @@ def get_mov_combined_features():
name
=
'movie_id'
,
name
=
'movie_id'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
mov_emb
=
layers
.
embedding
(
mov_emb
=
layers
.
embedding
(
input
=
mov_id
,
input
=
mov_id
,
...
@@ -139,13 +139,13 @@ def get_mov_combined_features():
...
@@ -139,13 +139,13 @@ def get_mov_combined_features():
size
=
[
MOV_DICT_SIZE
,
32
],
size
=
[
MOV_DICT_SIZE
,
32
],
param_attr
=
{
'name'
:
'movie_table'
},
param_attr
=
{
'name'
:
'movie_table'
},
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
mov_fc
=
layers
.
fc
(
input
=
mov_emb
,
mov_fc
=
layers
.
fc
(
input
=
mov_emb
,
size
=
32
,
size
=
32
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
CATEGORY_DICT_SIZE
=
len
(
paddle
.
dataset
.
movielens
.
movie_categories
())
CATEGORY_DICT_SIZE
=
len
(
paddle
.
dataset
.
movielens
.
movie_categories
())
...
@@ -153,21 +153,21 @@ def get_mov_combined_features():
...
@@ -153,21 +153,21 @@ def get_mov_combined_features():
name
=
'category_id'
,
name
=
'category_id'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
mov_categories_emb
=
layers
.
embedding
(
mov_categories_emb
=
layers
.
embedding
(
input
=
category_id
,
input
=
category_id
,
size
=
[
CATEGORY_DICT_SIZE
,
32
],
size
=
[
CATEGORY_DICT_SIZE
,
32
],
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
mov_categories_hidden
=
layers
.
sequence_pool
(
mov_categories_hidden
=
layers
.
sequence_pool
(
input
=
mov_categories_emb
,
input
=
mov_categories_emb
,
pool_type
=
"sum"
,
pool_type
=
"sum"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
MOV_TITLE_DICT_SIZE
=
len
(
paddle
.
dataset
.
movielens
.
get_movie_title_dict
())
MOV_TITLE_DICT_SIZE
=
len
(
paddle
.
dataset
.
movielens
.
get_movie_title_dict
())
...
@@ -175,15 +175,15 @@ def get_mov_combined_features():
...
@@ -175,15 +175,15 @@ def get_mov_combined_features():
name
=
'movie_title'
,
name
=
'movie_title'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
mov_title_emb
=
layers
.
embedding
(
mov_title_emb
=
layers
.
embedding
(
input
=
mov_title_id
,
input
=
mov_title_id
,
size
=
[
MOV_TITLE_DICT_SIZE
,
32
],
size
=
[
MOV_TITLE_DICT_SIZE
,
32
],
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
mov_title_conv
=
nets
.
sequence_conv_pool
(
mov_title_conv
=
nets
.
sequence_conv_pool
(
input
=
mov_title_emb
,
input
=
mov_title_emb
,
...
@@ -191,21 +191,21 @@ def get_mov_combined_features():
...
@@ -191,21 +191,21 @@ def get_mov_combined_features():
filter_size
=
3
,
filter_size
=
3
,
act
=
"tanh"
,
act
=
"tanh"
,
pool_type
=
"sum"
,
pool_type
=
"sum"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
concat_embed
=
layers
.
concat
(
concat_embed
=
layers
.
concat
(
input
=
[
mov_fc
,
mov_categories_hidden
,
mov_title_conv
],
input
=
[
mov_fc
,
mov_categories_hidden
,
mov_title_conv
],
axis
=
1
,
axis
=
1
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
# FIXME(dzh) : need tanh operator
# FIXME(dzh) : need tanh operator
mov_combined_features
=
layers
.
fc
(
input
=
concat_embed
,
mov_combined_features
=
layers
.
fc
(
input
=
concat_embed
,
size
=
200
,
size
=
200
,
act
=
"tanh"
,
act
=
"tanh"
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
return
mov_combined_features
return
mov_combined_features
...
@@ -218,24 +218,26 @@ def model():
...
@@ -218,24 +218,26 @@ def model():
inference
=
layers
.
cos_sim
(
inference
=
layers
.
cos_sim
(
X
=
usr_combined_features
,
X
=
usr_combined_features
,
Y
=
mov_combined_features
,
Y
=
mov_combined_features
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
label
=
layers
.
data
(
label
=
layers
.
data
(
name
=
'score'
,
name
=
'score'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'float32'
,
data_type
=
'float32'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
square_cost
=
layers
.
square_error_cost
(
square_cost
=
layers
.
square_error_cost
(
input
=
inference
,
input
=
inference
,
label
=
label
,
label
=
label
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
avg_cost
=
layers
.
mean
(
avg_cost
=
layers
.
mean
(
x
=
square_cost
,
program
=
program
,
init_program
=
init_program
)
x
=
square_cost
,
main_program
=
main_program
,
startup_program
=
startup_program
)
return
avg_cost
return
avg_cost
...
@@ -243,8 +245,8 @@ def model():
...
@@ -243,8 +245,8 @@ def model():
def
main
():
def
main
():
cost
=
model
()
cost
=
model
()
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.2
)
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.2
)
opts
=
sgd_optimizer
.
minimize
(
cost
,
init_program
=
init
_program
)
opts
=
sgd_optimizer
.
minimize
(
cost
,
startup_program
=
startup
_program
)
block
=
program
.
block
(
0
)
block
=
main_
program
.
block
(
0
)
if
use_gpu
:
if
use_gpu
:
place
=
core
.
GPUPlace
(
0
)
place
=
core
.
GPUPlace
(
0
)
...
@@ -252,7 +254,7 @@ def main():
...
@@ -252,7 +254,7 @@ def main():
place
=
core
.
CPUPlace
()
place
=
core
.
CPUPlace
()
exe
=
Executor
(
place
)
exe
=
Executor
(
place
)
exe
.
run
(
init
_program
,
feed
=
{},
fetch_list
=
[])
exe
.
run
(
startup
_program
,
feed
=
{},
fetch_list
=
[])
train_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
reader
.
shuffle
(
...
@@ -301,7 +303,7 @@ def main():
...
@@ -301,7 +303,7 @@ def main():
PASS_NUM
=
100
PASS_NUM
=
100
for
pass_id
in
range
(
PASS_NUM
):
for
pass_id
in
range
(
PASS_NUM
):
for
data
in
train_reader
():
for
data
in
train_reader
():
outs
=
exe
.
run
(
program
,
outs
=
exe
.
run
(
main_
program
,
feed
=
func_feed
(
feeding
,
data
),
feed
=
func_feed
(
feeding
,
data
),
fetch_list
=
[
cost
])
fetch_list
=
[
cost
])
out
=
np
.
array
(
outs
[
0
])
out
=
np
.
array
(
outs
[
0
])
...
...
python/paddle/v2/framework/tests/test_recurrent_op.py
浏览文件 @
51d4afaa
...
@@ -99,17 +99,17 @@ class RecurrentOpTest1(unittest.TestCase):
...
@@ -99,17 +99,17 @@ class RecurrentOpTest1(unittest.TestCase):
batch_size
=
1
batch_size
=
1
sent_len
=
1
sent_len
=
1
def
init
_program
(
self
):
def
setup
_program
(
self
):
self
.
program
=
Program
()
self
.
main_
program
=
Program
()
self
.
init
_program
=
Program
()
self
.
startup
_program
=
Program
()
self
.
p_info
=
{
self
.
p_info
=
{
"
program"
:
self
.
program
,
"
main_program"
:
self
.
main_
program
,
"
init_program"
:
self
.
init
_program
"
startup_program"
:
self
.
startup
_program
}
}
self
.
place
=
core
.
CPUPlace
()
self
.
place
=
core
.
CPUPlace
()
def
setUp
(
self
):
def
setUp
(
self
):
self
.
init
_program
()
self
.
setup
_program
()
self
.
data_field
=
{
"x"
,
"h_boot"
}
self
.
data_field
=
{
"x"
,
"h_boot"
}
self
.
input_shape
=
(
self
.
sent_len
,
self
.
batch_size
,
self
.
input_dim
)
self
.
input_shape
=
(
self
.
sent_len
,
self
.
batch_size
,
self
.
input_dim
)
...
@@ -131,7 +131,7 @@ class RecurrentOpTest1(unittest.TestCase):
...
@@ -131,7 +131,7 @@ class RecurrentOpTest1(unittest.TestCase):
name
=
'h_boot'
,
name
=
'h_boot'
,
**
self
.
p_info
)
**
self
.
p_info
)
rnn
=
StaticRNN
(
program
=
self
.
program
)
rnn
=
StaticRNN
(
main_program
=
self
.
main_
program
)
with
rnn
.
step
():
with
rnn
.
step
():
h_pre
=
rnn
.
memory
(
init
=
h_boot
)
h_pre
=
rnn
.
memory
(
init
=
h_boot
)
x_t
=
rnn
.
step_input
(
x
)
x_t
=
rnn
.
step_input
(
x
)
...
@@ -153,7 +153,7 @@ class RecurrentOpTest1(unittest.TestCase):
...
@@ -153,7 +153,7 @@ class RecurrentOpTest1(unittest.TestCase):
for
x
in
self
.
data_field
for
x
in
self
.
data_field
}
}
exe
=
Executor
(
self
.
place
)
exe
=
Executor
(
self
.
place
)
out
=
exe
.
run
(
self
.
program
,
out
=
exe
.
run
(
self
.
main_
program
,
feed
=
self
.
feed_map
,
feed
=
self
.
feed_map
,
fetch_list
=
[
self
.
output
])
fetch_list
=
[
self
.
output
])
...
@@ -165,12 +165,14 @@ class RecurrentOpTest1(unittest.TestCase):
...
@@ -165,12 +165,14 @@ class RecurrentOpTest1(unittest.TestCase):
for
x
in
self
.
data_field
for
x
in
self
.
data_field
}
}
fetch_list
=
[
fetch_list
=
[
self
.
program
.
global_block
().
var
(
x
+
"@GRAD"
)
self
.
main_
program
.
global_block
().
var
(
x
+
"@GRAD"
)
for
x
in
self
.
data_field
for
x
in
self
.
data_field
]
]
exe
=
Executor
(
self
.
place
)
exe
=
Executor
(
self
.
place
)
return
exe
.
run
(
self
.
program
,
feed
=
self
.
feed_map
,
fetch_list
=
fetch_list
)
return
exe
.
run
(
self
.
main_program
,
feed
=
self
.
feed_map
,
fetch_list
=
fetch_list
)
def
test_backward
(
self
):
def
test_backward
(
self
):
self
.
check_forward
()
self
.
check_forward
()
...
@@ -237,7 +239,7 @@ class RecurrentOpTest2(RecurrentOpTest1):
...
@@ -237,7 +239,7 @@ class RecurrentOpTest2(RecurrentOpTest1):
sent_len
=
2
sent_len
=
2
def
setUp
(
self
):
def
setUp
(
self
):
self
.
init
_program
()
self
.
setup
_program
()
self
.
data_field
=
{
"x"
,
"h_boot"
,
"W"
,
"U"
}
self
.
data_field
=
{
"x"
,
"h_boot"
,
"W"
,
"U"
}
...
@@ -260,7 +262,7 @@ class RecurrentOpTest2(RecurrentOpTest1):
...
@@ -260,7 +262,7 @@ class RecurrentOpTest2(RecurrentOpTest1):
name
=
'h_boot'
,
name
=
'h_boot'
,
**
self
.
p_info
)
**
self
.
p_info
)
rnn
=
StaticRNN
(
program
=
self
.
program
)
rnn
=
StaticRNN
(
main_program
=
self
.
main_
program
)
with
rnn
.
step
():
with
rnn
.
step
():
h_pre
=
rnn
.
memory
(
init
=
h_boot
)
h_pre
=
rnn
.
memory
(
init
=
h_boot
)
x_t
=
rnn
.
step_input
(
x
)
x_t
=
rnn
.
step_input
(
x
)
...
@@ -333,7 +335,7 @@ class RecurrentOpTest3(RecurrentOpTest1):
...
@@ -333,7 +335,7 @@ class RecurrentOpTest3(RecurrentOpTest1):
sent_len
=
2
sent_len
=
2
def
setUp
(
self
):
def
setUp
(
self
):
self
.
init
_program
()
self
.
setup
_program
()
self
.
data_field
=
{
"x"
,
"h_boot1"
,
"h_boot2"
}
self
.
data_field
=
{
"x"
,
"h_boot1"
,
"h_boot2"
}
...
@@ -364,7 +366,7 @@ class RecurrentOpTest3(RecurrentOpTest1):
...
@@ -364,7 +366,7 @@ class RecurrentOpTest3(RecurrentOpTest1):
append_batch_size
=
False
,
append_batch_size
=
False
,
**
self
.
p_info
)
**
self
.
p_info
)
rnn
=
StaticRNN
(
program
=
self
.
program
)
rnn
=
StaticRNN
(
main_program
=
self
.
main_
program
)
with
rnn
.
step
():
with
rnn
.
step
():
h_pre1
=
rnn
.
memory
(
init
=
h_boot1
)
h_pre1
=
rnn
.
memory
(
init
=
h_boot1
)
h_pre2
=
rnn
.
memory
(
init
=
h_boot2
)
h_pre2
=
rnn
.
memory
(
init
=
h_boot2
)
...
...
python/paddle/v2/framework/tests/test_understand_sentiment_conv.py
浏览文件 @
51d4afaa
...
@@ -4,7 +4,7 @@ import paddle.v2.framework.nets as nets
...
@@ -4,7 +4,7 @@ import paddle.v2.framework.nets as nets
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.optimizer
as
optimizer
import
paddle.v2.framework.optimizer
as
optimizer
from
paddle.v2.framework.framework
import
Program
,
g_
program
,
g_init
_program
from
paddle.v2.framework.framework
import
Program
,
g_
main_program
,
g_startup
_program
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.executor
import
Executor
import
numpy
as
np
import
numpy
as
np
...
@@ -70,7 +70,7 @@ def main():
...
@@ -70,7 +70,7 @@ def main():
place
=
core
.
CPUPlace
()
place
=
core
.
CPUPlace
()
exe
=
Executor
(
place
)
exe
=
Executor
(
place
)
exe
.
run
(
g_
init
_program
)
exe
.
run
(
g_
startup
_program
)
for
pass_id
in
xrange
(
PASS_NUM
):
for
pass_id
in
xrange
(
PASS_NUM
):
for
data
in
train_data
():
for
data
in
train_data
():
...
@@ -82,7 +82,7 @@ def main():
...
@@ -82,7 +82,7 @@ def main():
tensor_label
=
core
.
LoDTensor
()
tensor_label
=
core
.
LoDTensor
()
tensor_label
.
set
(
label
,
place
)
tensor_label
.
set
(
label
,
place
)
outs
=
exe
.
run
(
g_program
,
outs
=
exe
.
run
(
g_
main_
program
,
feed
=
{
"words"
:
tensor_words
,
feed
=
{
"words"
:
tensor_words
,
"label"
:
tensor_label
},
"label"
:
tensor_label
},
fetch_list
=
[
cost
,
acc
])
fetch_list
=
[
cost
,
acc
])
...
...
python/paddle/v2/framework/tests/test_variable.py
浏览文件 @
51d4afaa
import
unittest
import
unittest
from
paddle.v2.framework.framework
import
Variable
,
g_program
,
Program
from
paddle.v2.framework.framework
import
Variable
,
g_
main_
program
,
Program
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
numpy
as
np
import
numpy
as
np
...
@@ -18,7 +18,7 @@ class TestVariable(unittest.TestCase):
...
@@ -18,7 +18,7 @@ class TestVariable(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
lambda
:
convert
(
"int8"
))
self
.
assertRaises
(
ValueError
,
lambda
:
convert
(
"int8"
))
def
test_var
(
self
):
def
test_var
(
self
):
b
=
g_program
.
current_block
()
b
=
g_
main_
program
.
current_block
()
w
=
b
.
create_var
(
w
=
b
.
create_var
(
dtype
=
"float64"
,
shape
=
[
784
,
100
],
lod_level
=
0
,
name
=
"fc.w"
)
dtype
=
"float64"
,
shape
=
[
784
,
100
],
lod_level
=
0
,
name
=
"fc.w"
)
self
.
assertNotEqual
(
str
(
w
),
""
)
self
.
assertNotEqual
(
str
(
w
),
""
)
...
...
python/paddle/v2/framework/tests/test_word2vec.py
浏览文件 @
51d4afaa
...
@@ -3,13 +3,13 @@ import paddle.v2.framework.layers as layers
...
@@ -3,13 +3,13 @@ import paddle.v2.framework.layers as layers
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.optimizer
as
optimizer
import
paddle.v2.framework.optimizer
as
optimizer
from
paddle.v2.framework.framework
import
Program
,
g_program
from
paddle.v2.framework.framework
import
Program
,
g_
main_
program
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.executor
import
Executor
import
numpy
as
np
import
numpy
as
np
init
_program
=
Program
()
startup
_program
=
Program
()
program
=
Program
()
main_
program
=
Program
()
embed_size
=
32
embed_size
=
32
hidden_size
=
256
hidden_size
=
256
...
@@ -24,32 +24,32 @@ first_word = layers.data(
...
@@ -24,32 +24,32 @@ first_word = layers.data(
name
=
'firstw'
,
name
=
'firstw'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
second_word
=
layers
.
data
(
second_word
=
layers
.
data
(
name
=
'secondw'
,
name
=
'secondw'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
third_word
=
layers
.
data
(
third_word
=
layers
.
data
(
name
=
'thirdw'
,
name
=
'thirdw'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
forth_word
=
layers
.
data
(
forth_word
=
layers
.
data
(
name
=
'forthw'
,
name
=
'forthw'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
next_word
=
layers
.
data
(
next_word
=
layers
.
data
(
name
=
'nextw'
,
name
=
'nextw'
,
shape
=
[
1
],
shape
=
[
1
],
data_type
=
'int64'
,
data_type
=
'int64'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
embed_first
=
layers
.
embedding
(
embed_first
=
layers
.
embedding
(
input
=
first_word
,
input
=
first_word
,
...
@@ -57,16 +57,16 @@ embed_first = layers.embedding(
...
@@ -57,16 +57,16 @@ embed_first = layers.embedding(
data_type
=
'float32'
,
data_type
=
'float32'
,
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
param_attr
=
{
'name'
:
'shared_w'
},
param_attr
=
{
'name'
:
'shared_w'
},
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
embed_second
=
layers
.
embedding
(
embed_second
=
layers
.
embedding
(
input
=
second_word
,
input
=
second_word
,
size
=
[
dict_size
,
embed_size
],
size
=
[
dict_size
,
embed_size
],
data_type
=
'float32'
,
data_type
=
'float32'
,
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
param_attr
=
{
'name'
:
'shared_w'
},
param_attr
=
{
'name'
:
'shared_w'
},
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
embed_third
=
layers
.
embedding
(
embed_third
=
layers
.
embedding
(
input
=
third_word
,
input
=
third_word
,
...
@@ -74,42 +74,43 @@ embed_third = layers.embedding(
...
@@ -74,42 +74,43 @@ embed_third = layers.embedding(
data_type
=
'float32'
,
data_type
=
'float32'
,
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
param_attr
=
{
'name'
:
'shared_w'
},
param_attr
=
{
'name'
:
'shared_w'
},
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
embed_forth
=
layers
.
embedding
(
embed_forth
=
layers
.
embedding
(
input
=
forth_word
,
input
=
forth_word
,
size
=
[
dict_size
,
embed_size
],
size
=
[
dict_size
,
embed_size
],
data_type
=
'float32'
,
data_type
=
'float32'
,
is_sparse
=
is_sparse
,
is_sparse
=
is_sparse
,
param_attr
=
{
'name'
:
'shared_w'
},
param_attr
=
{
'name'
:
'shared_w'
},
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
concat_embed
=
layers
.
concat
(
concat_embed
=
layers
.
concat
(
input
=
[
embed_first
,
embed_second
,
embed_third
,
embed_forth
],
input
=
[
embed_first
,
embed_second
,
embed_third
,
embed_forth
],
axis
=
1
,
axis
=
1
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
hidden1
=
layers
.
fc
(
input
=
concat_embed
,
hidden1
=
layers
.
fc
(
input
=
concat_embed
,
size
=
hidden_size
,
size
=
hidden_size
,
act
=
'sigmoid'
,
act
=
'sigmoid'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
predict_word
=
layers
.
fc
(
input
=
hidden1
,
predict_word
=
layers
.
fc
(
input
=
hidden1
,
size
=
dict_size
,
size
=
dict_size
,
act
=
'softmax'
,
act
=
'softmax'
,
program
=
program
,
main_program
=
main_
program
,
init_program
=
init
_program
)
startup_program
=
startup
_program
)
cost
=
layers
.
cross_entropy
(
cost
=
layers
.
cross_entropy
(
input
=
predict_word
,
input
=
predict_word
,
label
=
next_word
,
label
=
next_word
,
program
=
program
,
main_program
=
main_program
,
init_program
=
init_program
)
startup_program
=
startup_program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
program
=
program
,
init_program
=
init_program
)
avg_cost
=
layers
.
mean
(
x
=
cost
,
main_program
=
main_program
,
startup_program
=
startup_program
)
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.001
)
sgd_optimizer
=
optimizer
.
SGDOptimizer
(
learning_rate
=
0.001
)
opts
=
sgd_optimizer
.
minimize
(
avg_cost
,
init
_program
)
opts
=
sgd_optimizer
.
minimize
(
avg_cost
,
startup
_program
)
train_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
imikolov
.
train
(
word_dict
,
N
),
batch_size
)
paddle
.
dataset
.
imikolov
.
train
(
word_dict
,
N
),
batch_size
)
...
@@ -117,7 +118,7 @@ train_reader = paddle.batch(
...
@@ -117,7 +118,7 @@ train_reader = paddle.batch(
place
=
core
.
CPUPlace
()
place
=
core
.
CPUPlace
()
exe
=
Executor
(
place
)
exe
=
Executor
(
place
)
exe
.
run
(
init
_program
,
feed
=
{},
fetch_list
=
[])
exe
.
run
(
startup
_program
,
feed
=
{},
fetch_list
=
[])
PASS_NUM
=
100
PASS_NUM
=
100
for
pass_id
in
range
(
PASS_NUM
):
for
pass_id
in
range
(
PASS_NUM
):
for
data
in
train_reader
():
for
data
in
train_reader
():
...
@@ -145,7 +146,7 @@ for pass_id in range(PASS_NUM):
...
@@ -145,7 +146,7 @@ for pass_id in range(PASS_NUM):
next_tensor
=
core
.
LoDTensor
()
next_tensor
=
core
.
LoDTensor
()
next_tensor
.
set
(
next_data
,
place
)
next_tensor
.
set
(
next_data
,
place
)
outs
=
exe
.
run
(
program
,
outs
=
exe
.
run
(
main_
program
,
feed
=
{
feed
=
{
'firstw'
:
first_tensor
,
'firstw'
:
first_tensor
,
'secondw'
:
second_tensor
,
'secondw'
:
second_tensor
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录