Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e133d8ef
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
e133d8ef
编写于
9月 08, 2021
作者:
G
Guoxia Wang
提交者:
GitHub
9月 08, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug (#35482)
上级
3c457a38
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
85 addition
and
17 deletion
+85
-17
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
python/paddle/nn/functional/loss.py
python/paddle/nn/functional/loss.py
+6
-6
未找到文件。
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:
...
...
@@ -1605,13 +1605,12 @@ def class_center_sample(label, num_classes, num_samples, group=None, seed=None):
The API supports CPU, single GPU and multi GPU.
Args:
label (Tensor): 1-D tensor with shape [N], each label in [0, num_classes)
num_classes (int): A positive integer to specify the number of classes at local rank.
label (Tensor): 1-D tensor with shape [N], each label in [0, num_classes)
num_classes (int): A positive integer to specify the number of classes at local rank.
Note that num_classes of each GPU can be different.
num_samples (int): A positive integer to specify the number of class center to sample.
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
...
...
python/paddle/nn/functional/loss.py
浏览文件 @
e133d8ef
...
...
@@ -1120,13 +1120,13 @@ def margin_cross_entropy(logits,
The API supports model parallel and single GPU. And logits.shape[-1] can be different at each rank.
Args:
logits (Tensor): shape[N, local_num_classes], the output of the normalized X multiply the normalized W.
logits (Tensor): shape[N, local_num_classes], the output of the normalized X multiply the normalized W.
The logits is shard_logits when using model parallel.
label (Tensor): shape[N] or shape[N, 1], the groud truth label.
margin1 (float, optional): m1 of margin loss, default value is `1.0`.
margin2 (float, optional): m2 of margin loss, default value is `0.5`.
margin3 (float, optional): m3 of margin loss, default value is `0.0`.
scale (float, optional): s of margin loss, default value is `64.0`.
label (Tensor): shape[N] or shape[N, 1], the groud truth label.
margin1 (float, optional): m1 of margin loss, default value is `1.0`.
margin2 (float, optional): m2 of margin loss, default value is `0.5`.
margin3 (float, optional): m3 of margin loss, default value is `0.0`.
scale (float, optional): s of margin loss, default value is `64.0`.
group (Group, optional): The abstract representation of group, see paddle.distributed.collective.Group.
Default `None`.
return_softmax (bool, optional): Whether return softmax probability. Default value is `False`.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录