Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
9203aaf1
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
9203aaf1
编写于
3月 17, 2020
作者:
S
songyouwei
提交者:
GitHub
3月 17, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix unittest for coverage (#23007)
test=develop
上级
5842ae67
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
68 addition
and
23 deletion
+68
-23
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+22
-10
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+10
-11
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+36
-2
未找到文件。
python/paddle/fluid/layers/nn.py
浏览文件 @
9203aaf1
...
...
@@ -4335,12 +4335,13 @@ def split(input, num_or_sections, dim=-1, name=None):
if in_dygraph_mode():
inputs = {'X': [input]}
attrs = {}
if isinstance(dim, int):
if isinstance(dim, Variable):
dim = dim.numpy()
assert dim.shape == (1,
), "dim of type Variable should have shape [1]"
dim = dim[0]
dim = (len(input.shape) + dim) if dim < 0 else dim
attrs['axis'] = dim
else:
dim.stop_gradient = True
inputs['AxisTensor'] = [dim]
if isinstance(num_or_sections, int):
num = num_or_sections
...
...
@@ -4717,17 +4718,23 @@ def topk(input, k, name=None):
"""
inputs = {"X": [input]}
attrs = {}
if isinstance(k, Variable):
inputs['K'] = [k]
else:
attrs = {'k': k}
if in_dygraph_mode():
if isinstance(k, Variable):
k = k.numpy()
assert k.shape == (1, ), "k of type Variable should have shape [1]"
k = k[0]
attrs = {'k': k}
outs = core.ops.top_k(inputs, attrs)
outs['Out'][0].stop_gradient = True
outs['Indices'][0].stop_gradient = True
return outs['Out'][0], outs['Indices'][0]
if isinstance(k, Variable):
inputs['K'] = [k]
else:
attrs = {'k': k}
helper = LayerHelper("top_k", **locals())
values = helper.create_variable_for_type_inference(dtype=input.dtype)
indices = helper.create_variable_for_type_inference(dtype="int64")
...
...
@@ -5401,6 +5408,11 @@ def one_hot(input, depth, allow_out_of_range=False):
one_hot_label = fluid.layers.one_hot(input=label, depth=4)
"""
if in_dygraph_mode():
if isinstance(depth, Variable):
depth = depth.numpy()
assert depth.shape == (
1, ), "depth of type Variable should have shape [1]"
depth = depth[0]
inputs = {'X': [input]}
attrs = {'depth': depth, 'allow_out_of_range': allow_out_of_range}
outs = core.ops.one_hot(inputs, attrs)
...
...
python/paddle/fluid/layers/tensor.py
浏览文件 @
9203aaf1
...
...
@@ -256,9 +256,11 @@ def concat(input, axis=0, name=None):
if
in_dygraph_mode
():
inputs
=
{
'X'
:
input
}
if
not
isinstance
(
axis
,
int
):
raise
TypeError
(
"Input 'axis' in concat must be int in Dygraph mode."
)
if
isinstance
(
axis
,
Variable
):
axis
=
axis
.
numpy
()
assert
axis
.
shape
==
(
1
,
),
"axis of type Variable should have shape [1]"
axis
=
axis
[
0
]
attrs
=
{
'axis'
:
axis
}
outs
=
core
.
ops
.
concat
(
inputs
,
attrs
)
return
outs
[
'Out'
][
0
]
...
...
@@ -579,15 +581,12 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None):
if
in_dygraph_mode
():
if
isinstance
(
shape
,
(
list
,
tuple
)):
if
utils
.
_contain_var
(
shape
):
raise
TypeError
(
"The type of 'shape' in fill_constant must be list[int] or tuple(int) in Dygraph mode, but "
"received %s, which contains Variable."
%
type
(
shape
))
attrs
[
'shape'
]
=
shape
shape
=
list
(
map
(
lambda
x
:
x
.
numpy
()[
0
]
if
isinstance
(
x
,
Variable
)
else
x
,
shape
))
else
:
raise
TypeError
(
"The type of 'shape' in fill_constant must be list[int] or tuple(int) in Dygraph mode, but "
"received %s."
%
type
(
shape
))
shape
=
list
(
shape
.
numpy
().
astype
(
int
))
attrs
[
'shape'
]
=
shape
if
out
is
None
:
out
=
_varbase_creator
(
dtype
=
dtype
)
attrs
[
'dtype'
]
=
out
.
dtype
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
9203aaf1
...
...
@@ -876,7 +876,8 @@ class TestLayer(LayerTest):
emb_rlt
=
emb
(
words
[
i
])
embs3
.
append
(
emb_rlt
)
embs3
=
layers
.
concat
(
input
=
embs3
,
axis
=
1
)
embs3
=
layers
.
concat
(
input
=
embs3
,
axis
=
fluid
.
dygraph
.
to_variable
(
np
.
array
([
1
])))
nce
=
nn
.
NCE
(
num_total_classes
=
dict_size
,
dim
=
embs3
.
shape
[
1
],
num_neg_samples
=
2
,
...
...
@@ -903,7 +904,9 @@ class TestLayer(LayerTest):
for
i
in
range
(
window_size
):
words
.
append
(
base
.
to_variable
(
inp_word
[
i
]))
sample_weights
=
layers
.
fill_constant
(
shape
=
[
5
,
1
],
dtype
=
'float32'
,
value
=
1
)
shape
=
fluid
.
dygraph
.
to_variable
(
np
.
array
([
5
,
1
])),
dtype
=
'float32'
,
value
=
1
)
emb
=
nn
.
Embedding
(
size
=
[
dict_size
,
32
],
param_attr
=
'emb.w'
,
is_sparse
=
False
)
...
...
@@ -955,6 +958,37 @@ class TestLayer(LayerTest):
self
.
assertTrue
(
np
.
array_equal
(
nce1
.
bias
.
numpy
(),
nce2
.
bias
.
numpy
()))
def
test_one_hot
(
self
):
with
self
.
dynamic_graph
():
label
=
fluid
.
dygraph
.
to_variable
(
np
.
array
([[
1
],
[
1
],
[
3
],
[
0
]]))
one_hot_label1
=
fluid
.
layers
.
one_hot
(
input
=
label
,
depth
=
4
)
one_hot_label2
=
fluid
.
layers
.
one_hot
(
input
=
label
,
depth
=
fluid
.
dygraph
.
to_variable
(
np
.
array
([
4
])))
self
.
assertTrue
(
np
.
array_equal
(
one_hot_label1
.
numpy
(),
one_hot_label2
.
numpy
()))
def
test_split
(
self
):
with
self
.
dynamic_graph
():
input
=
fluid
.
dygraph
.
to_variable
(
np
.
random
.
random
((
3
,
8
,
5
)))
x0
,
x1
=
fluid
.
layers
.
split
(
input
,
num_or_sections
=
2
,
dim
=
1
)
x00
,
x11
=
fluid
.
layers
.
split
(
input
,
num_or_sections
=
2
,
dim
=
fluid
.
dygraph
.
to_variable
(
np
.
array
([
1
])))
self
.
assertTrue
(
np
.
array_equal
(
x0
.
numpy
(),
x00
.
numpy
()))
self
.
assertTrue
(
np
.
array_equal
(
x1
.
numpy
(),
x11
.
numpy
()))
def
test_topk
(
self
):
with
self
.
dynamic_graph
():
input
=
fluid
.
dygraph
.
to_variable
(
np
.
random
.
random
((
13
,
11
)))
top5_values1
,
top5_indices1
=
layers
.
topk
(
input
,
k
=
5
)
top5_values2
,
top5_indices2
=
layers
.
topk
(
input
,
k
=
fluid
.
dygraph
.
to_variable
(
np
.
array
([
5
])))
self
.
assertTrue
(
np
.
array_equal
(
top5_values1
.
numpy
(),
top5_values2
.
numpy
()))
self
.
assertTrue
(
np
.
array_equal
(
top5_indices1
.
numpy
(),
top5_indices2
.
numpy
()))
def
test_conv3d
(
self
):
with
self
.
static_graph
():
images
=
layers
.
data
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录