Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
a1326cf3
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a1326cf3
编写于
1月 22, 2019
作者:
Q
Qiao Longfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add NumpyArrayInitializer and use it to refactor nce op
上级
def70c5c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
87 addition
and
58 deletion
+87
-58
python/paddle/fluid/initializer.py
python/paddle/fluid/initializer.py
+60
-1
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+16
-11
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+11
-34
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+0
-12
未找到文件。
python/paddle/fluid/initializer.py
浏览文件 @
a1326cf3
...
@@ -24,7 +24,8 @@ __all__ = [
...
@@ -24,7 +24,8 @@ __all__ = [
'Constant'
,
'Uniform'
,
'Normal'
,
'TruncatedNormal'
,
'Xavier'
,
'Bilinear'
,
'Constant'
,
'Uniform'
,
'Normal'
,
'TruncatedNormal'
,
'Xavier'
,
'Bilinear'
,
'MSRA'
,
'force_init_on_cpu'
,
'init_on_cpu'
,
'ConstantInitializer'
,
'MSRA'
,
'force_init_on_cpu'
,
'init_on_cpu'
,
'ConstantInitializer'
,
'UniformInitializer'
,
'NormalInitializer'
,
'TruncatedNormalInitializer'
,
'UniformInitializer'
,
'NormalInitializer'
,
'TruncatedNormalInitializer'
,
'XavierInitializer'
,
'BilinearInitializer'
,
'MSRAInitializer'
'XavierInitializer'
,
'BilinearInitializer'
,
'MSRAInitializer'
,
'NumpyArrayInitializer'
]
]
_force_init_on_cpu_
=
False
_force_init_on_cpu_
=
False
...
@@ -683,6 +684,64 @@ class BilinearInitializer(Initializer):
...
@@ -683,6 +684,64 @@ class BilinearInitializer(Initializer):
return
op
return
op
class
NumpyArrayInitializer
(
Initializer
):
"""Init an parameter with an numpy array
Args:
value (numpy): numpy array to initialize the variable
Examples:
.. code-block:: python
fc = fluid.layers.fc(input=x, size=10,
param_attr=fluid.initializer.NumpyArrayInitializer(numpy.array([1,2])))
"""
def
__init__
(
self
,
value
):
import
numpy
assert
isinstance
(
value
,
numpy
.
ndarray
)
super
(
NumpyArrayInitializer
,
self
).
__init__
()
self
.
_value
=
value
def
__call__
(
self
,
var
,
block
):
"""Add constant initialization ops for a variable
Args:
var: Variable that needs to be initialized
block: The block in which initialization ops
should be added
Returns:
the initialization op
"""
assert
isinstance
(
var
,
framework
.
Variable
)
assert
isinstance
(
block
,
framework
.
Block
)
# Initialization Ops should be prepended and not appended
dtype
=
framework
.
convert_np_dtype_to_dtype_
(
self
.
_value
.
dtype
)
if
dtype
==
VarDesc
.
VarType
.
FP32
:
value_name
=
"fp32_values"
values
=
[
float
(
v
)
for
v
in
self
.
_value
.
flat
]
elif
dtype
==
VarDesc
.
VarType
.
INT32
:
value_name
=
"int32_values"
values
=
[
int
(
v
)
for
v
in
self
.
_value
.
flat
]
else
:
raise
ValueError
(
"Unsupported dtype %s"
,
self
.
_value
.
dtype
)
if
self
.
_value
.
size
>
1024
*
1024
*
5
:
raise
ValueError
(
"The size of input is too big. Please consider "
"saving it to file and 'load_op' to load it"
)
op
=
block
.
_prepend_op
(
type
=
'assign_value'
,
outputs
=
{
'Out'
:
var
},
attrs
=
{
'dtype'
:
dtype
,
'shape'
:
list
(
input
.
shape
),
value_name
:
values
},
stop_gradient
=
True
)
var
.
op
=
op
return
op
# We short the class name, since users will use the initializer with the package
# We short the class name, since users will use the initializer with the package
# name. The sample code:
# name. The sample code:
#
#
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
a1326cf3
...
@@ -22,7 +22,7 @@ import six
...
@@ -22,7 +22,7 @@ import six
import
os
import
os
import
inspect
import
inspect
from
..layer_helper
import
LayerHelper
from
..layer_helper
import
LayerHelper
from
..initializer
import
Normal
,
Constant
from
..initializer
import
Normal
,
Constant
,
NumpyArrayInitializer
from
..framework
import
Variable
,
OpProtoHolder
from
..framework
import
Variable
,
OpProtoHolder
from
..param_attr
import
ParamAttr
from
..param_attr
import
ParamAttr
from
.layer_function_generator
import
autodoc
,
templatedoc
,
_generate_doc_string_
from
.layer_function_generator
import
autodoc
,
templatedoc
,
_generate_doc_string_
...
@@ -5181,16 +5181,21 @@ def nce(input,
...
@@ -5181,16 +5181,21 @@ def nce(input,
alias_probs_
[
little
[
0
]]
=
1.0
alias_probs_
[
little
[
0
]]
=
1.0
alias_
[
little
[
0
]]
=
-
1
alias_
[
little
[
0
]]
=
-
1
probs
=
assign
(
def
_init_by_numpy_array
(
numpy_array
):
input
=
np
.
array
(
custom_dist
).
astype
(
'float32'
),
init_once
=
True
)
ret
=
helper
.
create_parameter
(
custom_alias
=
assign
(
attr
=
ParamAttr
(),
input
=
np
.
array
(
alias_
).
astype
(
'int32'
),
init_once
=
True
)
shape
=
numpy_array
.
shape
,
custom_alias_probs
=
assign
(
dtype
=
numpy_array
.
dtype
,
input
=
np
.
array
(
alias_probs_
).
astype
(
'float32'
),
init_once
=
True
)
default_initializer
=
NumpyArrayInitializer
(
numpy_array
))
ret
.
stop_gradient
=
True
inputs
[
'CustomDistProbs'
]
=
probs
return
ret
inputs
[
'CustomDistAlias'
]
=
custom_alias
inputs
[
'CustomDistAliasProbs'
]
=
custom_alias_probs
inputs
[
'CustomDistProbs'
]
=
_init_by_numpy_array
(
np
.
array
(
custom_dist
).
astype
(
'float32'
))
inputs
[
'CustomDistAlias'
]
=
_init_by_numpy_array
(
np
.
array
(
alias_
).
astype
(
'int32'
))
inputs
[
'CustomDistAliasProbs'
]
=
_init_by_numpy_array
(
np
.
array
(
alias_probs_
).
astype
(
'float32'
))
sampler
=
2
sampler
=
2
else
:
else
:
raise
Exception
(
"Unsupported sampler type."
)
raise
Exception
(
"Unsupported sampler type."
)
...
...
python/paddle/fluid/layers/tensor.py
浏览文件 @
a1326cf3
...
@@ -291,7 +291,7 @@ def sums(input, out=None):
...
@@ -291,7 +291,7 @@ def sums(input, out=None):
return
out
return
out
def
assign
(
input
,
output
=
None
,
init_once
=
False
):
def
assign
(
input
,
output
=
None
):
"""
"""
**Assign**
**Assign**
...
@@ -300,7 +300,6 @@ def assign(input, output=None, init_once=False):
...
@@ -300,7 +300,6 @@ def assign(input, output=None, init_once=False):
Args:
Args:
input(Variable|numpy.ndarray): The source variable
input(Variable|numpy.ndarray): The source variable
output(Variable|None): The destination variable
output(Variable|None): The destination variable
init_once(bool|false): assign value into global var only in startup program.
Returns:
Returns:
Variable: The destination variable that was supplied as the *output*.
Variable: The destination variable that was supplied as the *output*.
...
@@ -314,22 +313,10 @@ def assign(input, output=None, init_once=False):
...
@@ -314,22 +313,10 @@ def assign(input, output=None, init_once=False):
"""
"""
helper
=
LayerHelper
(
'assign'
,
**
locals
())
helper
=
LayerHelper
(
'assign'
,
**
locals
())
if
output
is
None
:
if
output
is
None
:
if
init_once
:
output
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
)
output
=
helper
.
create_parameter
(
attr
=
ParamAttr
(),
shape
=
input
.
shape
,
dtype
=
input
.
dtype
,
default_initializer
=
Constant
(
0.0
))
output
.
stop_gradient
=
True
else
:
output
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
)
if
isinstance
(
input
,
Variable
):
if
isinstance
(
input
,
Variable
):
if
init_once
:
raise
ValueError
(
"init once only support numpy assign!"
)
helper
.
append_op
(
helper
.
append_op
(
type
=
'assign'
,
inputs
=
{
'X'
:
[
input
]},
outputs
=
{
'Out'
:
[
output
]})
type
=
'assign'
,
inputs
=
{
'X'
:
[
input
]},
outputs
=
{
'Out'
:
[
output
]})
elif
isinstance
(
input
,
numpy
.
ndarray
):
elif
isinstance
(
input
,
numpy
.
ndarray
):
dtype
=
convert_np_dtype_to_dtype_
(
input
.
dtype
)
dtype
=
convert_np_dtype_to_dtype_
(
input
.
dtype
)
if
dtype
==
VarDesc
.
VarType
.
FP32
:
if
dtype
==
VarDesc
.
VarType
.
FP32
:
...
@@ -340,28 +327,18 @@ def assign(input, output=None, init_once=False):
...
@@ -340,28 +327,18 @@ def assign(input, output=None, init_once=False):
values
=
[
int
(
v
)
for
v
in
input
.
flat
]
values
=
[
int
(
v
)
for
v
in
input
.
flat
]
else
:
else
:
raise
ValueError
(
"Unsupported dtype %s"
,
input
.
dtype
)
raise
ValueError
(
"Unsupported dtype %s"
,
input
.
dtype
)
if
input
.
size
>
1024
*
1024
*
5
:
if
input
.
size
>
1024
*
1024
:
raise
ValueError
(
"The size of input is too big. Please consider "
raise
ValueError
(
"The size of input is too big. Please consider "
"saving it to file and 'load_op' to load it"
)
"saving it to file and 'load_op' to load it"
)
if
init_once
:
helper
.
append_op
(
helper
.
startup_program
.
global_block
().
append_op
(
type
=
'assign_value'
,
type
=
'assign_value'
,
outputs
=
{
'Out'
:
[
output
]},
outputs
=
{
'Out'
:
[
output
]},
attrs
=
{
attrs
=
{
'dtype'
:
dtype
,
'dtype'
:
dtype
,
'shape'
:
list
(
input
.
shape
),
'shape'
:
list
(
input
.
shape
),
value_name
:
values
value_name
:
values
})
})
else
:
helper
.
append_op
(
type
=
'assign_value'
,
outputs
=
{
'Out'
:
[
output
]},
attrs
=
{
'dtype'
:
dtype
,
'shape'
:
list
(
input
.
shape
),
value_name
:
values
})
else
:
else
:
raise
ValueError
(
"Wrong type for assign input: %s"
%
type
(
input
))
raise
ValueError
(
"Wrong type for assign input: %s"
%
type
(
input
))
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
a1326cf3
...
@@ -1023,18 +1023,6 @@ class TestBook(unittest.TestCase):
...
@@ -1023,18 +1023,6 @@ class TestBook(unittest.TestCase):
print
(
str
(
program
))
print
(
str
(
program
))
def
test_assign
(
self
):
import
numpy
as
np
startup
=
Program
()
main
=
Program
()
with
program_guard
(
main
,
startup
):
probs
=
layers
.
assign
(
input
=
np
.
random
.
random
([
1
,
2
]).
astype
(
'float32'
),
init_once
=
True
)
print
(
str
(
main
))
print
(
str
(
startup
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录