Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
b53eb7dc
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看板
提交
b53eb7dc
编写于
12月 26, 2018
作者:
Q
Qiao Longfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add init once for assign layer
上级
dc8eca82
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
46 addition
and
13 deletion
+46
-13
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+5
-3
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+29
-10
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+12
-0
未找到文件。
python/paddle/fluid/layers/nn.py
浏览文件 @
b53eb7dc
...
@@ -5010,10 +5010,12 @@ def nce(input,
...
@@ -5010,10 +5010,12 @@ 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
(
input
=
np
.
array
(
custom_dist
).
astype
(
'float32'
))
probs
=
assign
(
custom_alias
=
assign
(
input
=
np
.
array
(
alias_
).
astype
(
'int32'
))
input
=
np
.
array
(
custom_dist
).
astype
(
'float32'
),
init_once
=
True
)
custom_alias
=
assign
(
input
=
np
.
array
(
alias_
).
astype
(
'int32'
),
init_once
=
True
)
custom_alias_probs
=
assign
(
custom_alias_probs
=
assign
(
input
=
np
.
array
(
alias_probs_
).
astype
(
'float32'
))
input
=
np
.
array
(
alias_probs_
).
astype
(
'float32'
)
,
init_once
=
True
)
inputs
[
'CustomDistProbs'
]
=
probs
inputs
[
'CustomDistProbs'
]
=
probs
inputs
[
'CustomDistAlias'
]
=
custom_alias
inputs
[
'CustomDistAlias'
]
=
custom_alias
...
...
python/paddle/fluid/layers/tensor.py
浏览文件 @
b53eb7dc
...
@@ -285,7 +285,7 @@ def sums(input, out=None):
...
@@ -285,7 +285,7 @@ def sums(input, out=None):
return
out
return
out
def
assign
(
input
,
output
=
None
):
def
assign
(
input
,
output
=
None
,
init_once
=
False
):
"""
"""
**Assign**
**Assign**
...
@@ -294,6 +294,7 @@ def assign(input, output=None):
...
@@ -294,6 +294,7 @@ def assign(input, output=None):
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*.
...
@@ -307,10 +308,18 @@ def assign(input, output=None):
...
@@ -307,10 +308,18 @@ def assign(input, output=None):
"""
"""
helper
=
LayerHelper
(
'assign'
,
**
locals
())
helper
=
LayerHelper
(
'assign'
,
**
locals
())
if
output
is
None
:
if
output
is
None
:
output
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
)
if
init_once
:
output
=
helper
.
create_parameter
(
attr
=
ParamAttr
(),
shape
=
input
.
shape
,
dtype
=
input
.
dtype
)
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
:
...
@@ -325,6 +334,16 @@ def assign(input, output=None):
...
@@ -325,6 +334,16 @@ def assign(input, output=None):
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
.
startup_program
.
global_block
().
append_op
(
type
=
'assign_value'
,
outputs
=
{
'Out'
:
[
output
]},
attrs
=
{
'dtype'
:
dtype
,
'shape'
:
list
(
input
.
shape
),
value_name
:
values
})
else
:
helper
.
append_op
(
helper
.
append_op
(
type
=
'assign_value'
,
type
=
'assign_value'
,
outputs
=
{
'Out'
:
[
output
]},
outputs
=
{
'Out'
:
[
output
]},
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
b53eb7dc
...
@@ -1015,6 +1015,18 @@ class TestBook(unittest.TestCase):
...
@@ -1015,6 +1015,18 @@ 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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录