Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
e133d8ef
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
e133d8ef
编写于
9月 08, 2021
作者:
G
Guoxia Wang
提交者:
GitHub
9月 08, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug (#35482)
上级
3c457a38
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
79 addition
and
11 deletion
+79
-11
paddle/fluid/operators/margin_cross_entropy_op.cu
paddle/fluid/operators/margin_cross_entropy_op.cu
+5
-0
python/paddle/fluid/tests/unittests/test_class_center_sample_op.py
...ddle/fluid/tests/unittests/test_class_center_sample_op.py
+34
-6
python/paddle/fluid/tests/unittests/test_margin_cross_entropy_op.py
...dle/fluid/tests/unittests/test_margin_cross_entropy_op.py
+23
-0
python/paddle/nn/functional/common.py
python/paddle/nn/functional/common.py
+17
-5
未找到文件。
paddle/fluid/operators/margin_cross_entropy_op.cu
浏览文件 @
e133d8ef
...
...
@@ -323,6 +323,11 @@ class MarginCrossEntropyOpCUDAKernel : public framework::OpKernel<T> {
T
><<<
NumBlocks
(
N
),
threads
,
0
,
dev_ctx
.
stream
()
>>>
(
logits_ptr
,
labels
->
data
<
LabelT
>
(),
margin1
,
margin2
,
margin3
,
rank
,
nranks
,
N
,
D
,
class_interval
.
data
<
int
>
());
}
else
{
PADDLE_THROW
(
platform
::
errors
::
Unimplemented
(
"margin_cross_entropy label type noly support int32 and int64, "
"but got %s"
,
label_type
));
}
// scale by s
...
...
python/paddle/fluid/tests/unittests/test_class_center_sample_op.py
浏览文件 @
e133d8ef
...
...
@@ -138,7 +138,7 @@ class TestClassCenterSampleV2(unittest.TestCase):
label
=
paddle
.
static
.
data
(
name
=
'label'
,
shape
=
[
self
.
batch_size
],
dtype
=
self
.
dtype
)
remapped_label
,
sampled_class_index
=
paddle
.
nn
.
functional
.
class_center_sample
(
label
,
self
.
num_classes
,
self
.
num_samples
,
seed
=
self
.
seed
)
label
,
self
.
num_classes
,
self
.
num_samples
)
remapped_label_np
,
sampled_class_center_np
=
class_center_sample_numpy
(
label_np
,
[
self
.
num_classes
],
self
.
num_samples
)
...
...
@@ -163,7 +163,7 @@ class TestClassCenterSampleV2(unittest.TestCase):
label
=
paddle
.
to_tensor
(
label_np
,
dtype
=
self
.
dtype
)
remapped_label
,
sampled_class_index
=
paddle
.
nn
.
functional
.
class_center_sample
(
label
,
self
.
num_classes
,
self
.
num_samples
,
seed
=
self
.
seed
)
label
,
self
.
num_classes
,
self
.
num_samples
)
remapped_label_np
,
sampled_class_center_np
=
class_center_sample_numpy
(
label_np
,
[
self
.
num_classes
],
self
.
num_samples
)
...
...
@@ -210,13 +210,41 @@ class TestClassCenterSampleAPIError(unittest.TestCase):
label
=
paddle
.
to_tensor
(
label_np
)
remapped_label
,
sampled_class_index
=
paddle
.
nn
.
functional
.
class_center_sample
(
label
,
self
.
num_classes
,
self
.
num_samples
,
seed
=
self
.
seed
)
label
,
self
.
num_classes
,
self
.
num_samples
)
self
.
assertRaises
(
ValueError
,
test_num_samples
)
class
TestClassCenterSampleAPIError1
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
initParams
()
np
.
random
.
seed
(
self
.
seed
)
self
.
places
=
[
paddle
.
fluid
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
self
.
places
.
append
(
paddle
.
fluid
.
CUDAPlace
(
0
))
def
initParams
(
self
):
self
.
batch_size
=
5
self
.
num_samples
=
5
self
.
num_classes
=
10
self
.
seed
=
2021
self
.
init_dtype
()
def
init_dtype
(
self
):
self
.
dtype
=
np
.
int64
def
test_dynamic_errors
(
self
):
def
test_empty_label
():
for
place
in
self
.
places
:
with
paddle
.
fluid
.
dygraph
.
guard
(
place
):
label
=
paddle
.
to_tensor
(
np
.
array
([],
dtype
=
self
.
dtype
))
remapped_label
,
sampled_class_index
=
paddle
.
nn
.
functional
.
class_center_sample
(
label
,
self
.
num_classes
,
self
.
num_samples
)
print
(
remapped_label
,
sampled_class_index
)
self
.
assertRaises
(
ValueError
,
test_empty_label
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_margin_cross_entropy_op.py
浏览文件 @
e133d8ef
...
...
@@ -378,7 +378,30 @@ class TestMarginCrossEntropyOpAPIError(unittest.TestCase):
return_softmax
=
True
,
reduction
=
None
)
def
test_label_type
():
for
place
in
self
.
places
:
with
paddle
.
fluid
.
dygraph
.
guard
(
place
):
labels_np
=
np
.
random
.
uniform
(
0
,
self
.
num_class
,
(
self
.
batch_dim
,
1
)).
astype
(
self
.
dtype
)
logits_np
=
np
.
random
.
uniform
(
-
0.99
,
0.99
,
[
self
.
batch_dim
,
self
.
num_class
]).
astype
(
self
.
dtype
)
labels
=
paddle
.
to_tensor
(
labels_np
)
logits
=
paddle
.
to_tensor
(
logits_np
)
loss
,
softmax
=
paddle
.
nn
.
functional
.
margin_cross_entropy
(
logits
,
labels
,
margin1
=
self
.
margin1
,
margin2
=
self
.
margin2
,
margin3
=
self
.
margin3
,
scale
=
self
.
scale
,
return_softmax
=
True
,
reduction
=
None
)
self
.
assertRaises
(
ValueError
,
test_dim
)
self
.
assertRaises
(
NotImplementedError
,
test_label_type
)
if
__name__
==
'__main__'
:
...
...
python/paddle/nn/functional/common.py
浏览文件 @
e133d8ef
...
...
@@ -1584,7 +1584,7 @@ def label_smooth(label, prior_dist=None, epsilon=0.1, name=None):
return
smooth_label
def
class_center_sample
(
label
,
num_classes
,
num_samples
,
group
=
None
,
seed
=
None
):
def
class_center_sample
(
label
,
num_classes
,
num_samples
,
group
=
None
):
"""
Class center sample method is proposed from the paper PartialFC that only sample a subset of the class centers.
The process of sampling subset class centers is straightforward:
...
...
@@ -1611,7 +1611,6 @@ def class_center_sample(label, num_classes, num_samples, group=None, seed=None):
num_samples (int): A positive integer to specify the number of class center to sample.
group (Group, optional): The abstract representation of group.
See paddle.distributed.collective.Group. Default is ``None``.
seed (int, optional): Random seed. Default is ``None``.
Returns:
Tuple of two ``Tensor`` : (remapped_label, sampled_class_center), remapped label using sampled class center,
...
...
@@ -1702,6 +1701,19 @@ def class_center_sample(label, num_classes, num_samples, group=None, seed=None):
'Expected num_samples less than or equal to {}, got num_samples {}'
.
format
(
num_classes
,
num_samples
))
label_size
=
1
for
dim
in
list
(
label
.
shape
):
label_size
*=
dim
if
label_size
<
1
:
raise
ValueError
(
'Expected label_size > 0
\
(got label_size{})'
.
format
(
label_size
))
label_dims
=
len
(
list
(
label
.
shape
))
if
label_dims
!=
1
:
raise
ValueError
(
'Expected label_dims == 1
\
(got label_dims{})'
.
format
(
label_dims
))
seed
=
None
if
(
seed
is
None
or
seed
==
0
)
and
default_main_program
().
random_seed
!=
0
:
seed
=
default_main_program
().
random_seed
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录