Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f95c291b
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看板
未验证
提交
f95c291b
编写于
11月 15, 2017
作者:
Q
qingqing01
提交者:
GitHub
11月 15, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5651 from emailweixu/fix_conv_bias
Fix conv2d bias
上级
4fc9f55e
81bb26f3
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
65 addition
and
31 deletion
+65
-31
python/paddle/v2/fluid/io.py
python/paddle/v2/fluid/io.py
+38
-6
python/paddle/v2/fluid/layer_helper.py
python/paddle/v2/fluid/layer_helper.py
+11
-15
python/paddle/v2/fluid/layers.py
python/paddle/v2/fluid/layers.py
+2
-2
python/paddle/v2/fluid/tests/test_parameter.py
python/paddle/v2/fluid/tests/test_parameter.py
+14
-8
未找到文件。
python/paddle/v2/fluid/io.py
浏览文件 @
f95c291b
...
...
@@ -165,7 +165,7 @@ def save_inference_model(dirname,
:param target_vars: Variables from which we can get inference results.
:param executor: executor that save inference model
:param main_program: original program, which will be pruned to build the inference model.
Default g_program.
Default g_
main_
program.
:return: None
"""
...
...
@@ -234,3 +234,35 @@ def load_inference_model(dirname, executor):
fetch_vars
=
[
program
.
global_block
().
var
(
name
)
for
name
in
fetch_var_names
]
return
[
program
,
feed_var_names
,
fetch_vars
]
def
get_parameter_value
(
para
,
executor
):
"""
Get the LoDTensor for the parameter
:param executor: executor for retrieving the value
:param para: the given parameter
:return: the LoDTensor for the parameter
"""
assert
is_parameter
(
para
)
get_program
=
Program
()
block
=
get_program
.
global_block
()
new_var
=
_clone_var_in_block_
(
block
,
para
)
return
executor
.
run
(
get_program
,
feed
=
{},
fetch_list
=
[
new_var
])[
0
]
def
get_parameter_value_by_name
(
name
,
executor
,
program
=
None
):
"""
Get the LoDTensor for paramter with the given name
:param executor: executor for retrieving the value
:param name: the name of the parameter
:param program: the program where the variable is found
Default g_main_program.
:return: the LoDTensor for the variable
"""
if
program
is
None
:
program
=
g_main_program
var
=
program
.
global_block
().
var
(
name
)
return
get_parameter_value
(
var
,
executor
)
python/paddle/v2/fluid/layer_helper.py
浏览文件 @
f95c291b
...
...
@@ -72,7 +72,7 @@ class LayerHelper(object):
@
property
def
bias_attr
(
self
):
default
=
{
'name'
:
None
,
'initializer'
:
Xavier
Initializer
()}
default
=
{
'name'
:
None
,
'initializer'
:
Constant
Initializer
()}
bias_attr
=
self
.
kwargs
.
get
(
'bias_attr'
,
None
)
if
bias_attr
is
None
:
bias_attr
=
default
...
...
@@ -149,24 +149,19 @@ class LayerHelper(object):
persistable
=
True
,
initializer
=
initializer
)
def
append_bias_op
(
self
,
input_var
,
num_flatten_dims
=
None
):
def
append_bias_op
(
self
,
input_var
,
dim_start
=
1
,
dim_end
=
None
):
"""
Append bias operator and return its output. If the user does not set
bias_attr, append_bias_op will return input_var
:param input_var: the input variable. The len(input_var.shape) is larger
or equal than 2.
:param
num_flatten_dims: The input tensor will be flatten as a matrix
when adding bias.
`matrix.shape = product(input_var.shape[0:num_flatten_dims]), product(
input_var.shape[num_flatten_dims:])`
:param
dim_start:
:param dim_end: the shape of the bias will be
input_var.shape[dim_start:dim_end]. The bias is broadcasted to other
dimensions and added to input_var to get the output
"""
if
num_flatten_dims
is
None
:
num_flatten_dims
=
self
.
kwargs
.
get
(
'num_flatten_dims'
,
None
)
if
num_flatten_dims
is
None
:
num_flatten_dims
=
1
size
=
list
(
input_var
.
shape
[
num_flatten_dims
:])
size
=
list
(
input_var
.
shape
[
dim_start
:
dim_end
])
bias_attr
=
self
.
bias_attr
if
not
bias_attr
:
return
input_var
...
...
@@ -178,7 +173,8 @@ class LayerHelper(object):
type
=
'elementwise_add'
,
inputs
=
{
'X'
:
[
input_var
],
'Y'
:
[
b
]},
outputs
=
{
'Out'
:
[
tmp
]})
outputs
=
{
'Out'
:
[
tmp
]},
attrs
=
{
'axis'
:
dim_start
})
return
tmp
def
append_activation
(
self
,
input_var
):
...
...
python/paddle/v2/fluid/layers.py
浏览文件 @
f95c291b
...
...
@@ -694,7 +694,7 @@ def conv2d(input,
'paddings'
:
padding
,
'groups'
:
groups
})
pre_act
=
helper
.
append_bias_op
(
pre_bias
,
1
)
pre_act
=
helper
.
append_bias_op
(
pre_bias
,
dim_start
=
1
,
dim_end
=
2
)
return
helper
.
append_activation
(
pre_act
)
...
...
python/paddle/v2/fluid/tests/test_parameter.py
浏览文件 @
f95c291b
import
unittest
from
paddle.v2.fluid.framework
import
g_main_program
import
paddle.v2.fluid.core
as
core
from
paddle.v2.fluid.executor
import
Executor
import
paddle.v2.fluid.io
as
io
from
paddle.v2.fluid.initializer
import
ConstantInitializer
import
numpy
as
np
class
TestParameter
(
unittest
.
TestCase
):
def
test_param
(
self
):
b
=
g_main_program
.
create_block
()
shape
=
[
784
,
100
]
val
=
1.0625
b
=
g_main_program
.
global_block
()
param
=
b
.
create_parameter
(
name
=
'fc.w'
,
shape
=
[
784
,
100
]
,
shape
=
shape
,
dtype
=
'float32'
,
initialize_attr
=
{
'type'
:
'uniform_random'
,
'seed'
:
13
,
'min'
:
-
5.0
,
'max'
:
5.0
})
initializer
=
ConstantInitializer
(
val
))
self
.
assertIsNotNone
(
param
)
self
.
assertEqual
(
'fc.w'
,
param
.
name
)
self
.
assertEqual
((
784
,
100
),
param
.
shape
)
self
.
assertEqual
(
core
.
DataType
.
FP32
,
param
.
data_type
)
self
.
assertEqual
(
0
,
param
.
block
.
idx
)
exe
=
Executor
(
core
.
CPUPlace
())
p
=
exe
.
run
(
g_main_program
,
fetch_list
=
[
param
])[
0
]
self
.
assertTrue
(
np
.
allclose
(
np
.
array
(
p
),
np
.
ones
(
shape
)
*
val
))
p
=
io
.
get_parameter_value_by_name
(
'fc.w'
,
exe
,
g_main_program
)
self
.
assertTrue
(
np
.
allclose
(
np
.
array
(
p
),
np
.
ones
(
shape
)
*
val
))
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录