Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
c9a65382
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c9a65382
编写于
12月 10, 2018
作者:
F
frankwhzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix label_pos ,add test_layers.py, test=develop
上级
a672b291
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
51 addition
and
37 deletion
+51
-37
paddle/fluid/operators/bpr_loss_op.cc
paddle/fluid/operators/bpr_loss_op.cc
+16
-19
paddle/fluid/operators/bpr_loss_op.h
paddle/fluid/operators/bpr_loss_op.h
+9
-9
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+13
-4
python/paddle/fluid/tests/unittests/test_bpr_loss_op.py
python/paddle/fluid/tests/unittests/test_bpr_loss_op.py
+4
-5
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+9
-0
未找到文件。
paddle/fluid/operators/bpr_loss_op.cc
浏览文件 @
c9a65382
...
...
@@ -23,18 +23,17 @@ class BprLossOp : public framework::OperatorWithKernel {
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"LabelPos"
),
"Input(LabelPos) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Label"
),
"Input(Label) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Y"
),
"Output(Y) should be not null."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
label_
pos_dims
=
ctx
->
GetInputDim
(
"LabelPos
"
);
auto
label_
dims
=
ctx
->
GetInputDim
(
"Label
"
);
int
rank
=
x_dims
.
size
();
PADDLE_ENFORCE_EQ
(
rank
,
label_
pos_
dims
.
size
(),
"Input(X) and Input(Label
Pos
) shall have the same rank."
);
PADDLE_ENFORCE_EQ
(
rank
,
label_dims
.
size
(),
"Input(X) and Input(Label) shall have the same rank."
);
PADDLE_ENFORCE_EQ
(
framework
::
slice_ddim
(
x_dims
,
0
,
rank
-
1
),
framework
::
slice_ddim
(
label_
pos_
dims
,
0
,
rank
-
1
),
"Input(X) and Input(Label
Pos
) shall have the same shape "
framework
::
slice_ddim
(
label_dims
,
0
,
rank
-
1
),
"Input(X) and Input(Label) shall have the same shape "
"except the last dimension."
);
auto
y_dims
=
x_dims
;
...
...
@@ -60,25 +59,23 @@ class BprLossGradientOp : public framework::OperatorWithKernel {
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"LabelPos"
),
"Input(LabelPos) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Label"
),
"Input(Label) should be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Y"
)),
"Input(Y@GRAD) shoudl be not null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)),
"Output(X@GRAD) should be not null."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
label_
pos_dims
=
ctx
->
GetInputDim
(
"LabelPos
"
);
auto
label_
dims
=
ctx
->
GetInputDim
(
"Label
"
);
auto
dy_dims
=
ctx
->
GetInputDim
(
framework
::
GradVarName
(
"Y"
));
int
rank
=
x_dims
.
size
();
PADDLE_ENFORCE_EQ
(
dy_dims
.
size
(),
rank
,
"Input(Y@Grad) and Input(X) should have the same rank."
);
PADDLE_ENFORCE_EQ
(
label_pos_dims
.
size
(),
rank
,
"Input(LabelPos) and Input(X) should have the same rank."
);
PADDLE_ENFORCE_EQ
(
label_dims
.
size
(),
rank
,
"Input(Label) and Input(X) should have the same rank."
);
PADDLE_ENFORCE_EQ
(
framework
::
slice_ddim
(
x_dims
,
0
,
rank
-
1
),
framework
::
slice_ddim
(
label_
pos_
dims
,
0
,
rank
-
1
),
"The Input(X) and Input(Label
Pos
) should have the same "
framework
::
slice_ddim
(
label_dims
,
0
,
rank
-
1
),
"The Input(X) and Input(Label) should have the same "
"shape except the last dimension."
);
PADDLE_ENFORCE_EQ
(
framework
::
slice_ddim
(
x_dims
,
0
,
rank
-
1
),
framework
::
slice_ddim
(
dy_dims
,
0
,
rank
-
1
),
...
...
@@ -86,8 +83,8 @@ class BprLossGradientOp : public framework::OperatorWithKernel {
"shape except the last dimension."
);
PADDLE_ENFORCE_EQ
(
dy_dims
[
rank
-
1
],
1
,
"The last dimension of Input(Y@Grad) should be 1."
);
PADDLE_ENFORCE_EQ
(
label_
pos_
dims
[
rank
-
1
],
1
,
" the last dimension of Input(Label
Pos
) should be 1."
);
PADDLE_ENFORCE_EQ
(
label_dims
[
rank
-
1
],
1
,
" the last dimension of Input(Label) should be 1."
);
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
x_dims
);
ctx
->
ShareLoD
(
"X"
,
framework
::
GradVarName
(
"X"
));
}
...
...
@@ -111,7 +108,7 @@ class BprLossOpMaker : public framework::OpProtoAndCheckerMaker {
"size is equal to the number of classes. This input is a "
"real number."
);
AddInput
(
"Label
Pos
"
,
"Label"
,
"(Tensor), the tensor which represents the ground truth. It has the "
"same shape with 'X' except the last dimension. the last dimension "
"size is 1."
);
...
...
@@ -122,7 +119,7 @@ class BprLossOpMaker : public framework::OpProtoAndCheckerMaker {
AddComment
(
R"DOC(
Bayesian Personalized Ranking Loss Operator.
This operator belongs to pairwise ranking loss. Label
Pos
is the desired item.
This operator belongs to pairwise ranking loss. Label is the desired item.
The loss at a given point in one session is defined as:
$Y[i] = -\frac{1}{N_{i}} * \sum_{j=0}^{N_{i}}\log(\sigma(X[i, Label[i]]-X[i, j]))$
...
...
paddle/fluid/operators/bpr_loss_op.h
浏览文件 @
c9a65382
...
...
@@ -41,17 +41,17 @@ class BprLossOpKernel : public framework::OpKernel<T> {
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
label
_pos
=
ctx
.
Input
<
Tensor
>
(
"LabelPos
"
);
auto
*
label
=
ctx
.
Input
<
Tensor
>
(
"Label
"
);
auto
*
y
=
ctx
.
Output
<
Tensor
>
(
"Y"
);
y
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
int
rank
=
x
->
dims
().
size
();
Tensor
x_2d
=
framework
::
ReshapeToMatrix
(
*
x
,
rank
-
1
);
Tensor
labels_
Pos_2d
=
framework
::
ReshapeToMatrix
(
*
label_pos
,
rank
-
1
);
Tensor
labels_
2d
=
framework
::
ReshapeToMatrix
(
*
label
,
rank
-
1
);
Tensor
y_2d
=
framework
::
ReshapeToMatrix
(
*
y
,
rank
-
1
);
const
framework
::
Tensor
*
logits
=
&
x_2d
;
const
framework
::
Tensor
*
labels
_pos
=
&
labels_Po
s_2d
;
const
framework
::
Tensor
*
labels
=
&
label
s_2d
;
framework
::
Tensor
*
out
=
&
y_2d
;
const
int
step_size
=
logits
->
dims
()[
0
];
...
...
@@ -59,9 +59,9 @@ class BprLossOpKernel : public framework::OpKernel<T> {
const
T
*
logits_data
=
logits
->
data
<
T
>
();
T
*
loss_data
=
out
->
data
<
T
>
();
const
int64_t
*
label_
pos_data
=
labels_po
s
->
data
<
int64_t
>
();
const
int64_t
*
label_
data
=
label
s
->
data
<
int64_t
>
();
for
(
int
i
=
0
;
i
<
step_size
;
++
i
)
{
int
lbl_pos
=
label_
pos_
data
[
i
];
int
lbl_pos
=
label_data
[
i
];
PADDLE_ENFORCE_GE
(
lbl_pos
,
0
);
PADDLE_ENFORCE_LT
(
lbl_pos
,
class_num
);
int
index_pos
=
i
*
class_num
+
lbl_pos
;
...
...
@@ -84,7 +84,7 @@ class BprLossGradientOpKernel : public framework::OpKernel<T> {
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
dy
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
auto
*
label
_pos
=
ctx
.
Input
<
Tensor
>
(
"LabelPos
"
);
auto
*
label
=
ctx
.
Input
<
Tensor
>
(
"Label
"
);
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
const
int
step_size
=
x
->
dims
()[
0
];
...
...
@@ -92,16 +92,16 @@ class BprLossGradientOpKernel : public framework::OpKernel<T> {
T
*
dx_data
=
dx
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
const
T
*
dy_data
=
dy
->
data
<
T
>
();
const
T
*
x_data
=
x
->
data
<
T
>
();
const
int64_t
*
label_
pos_data
=
label_pos
->
data
<
int64_t
>
();
const
int64_t
*
label_
data
=
label
->
data
<
int64_t
>
();
for
(
size_t
sample_id
=
0
;
sample_id
<
step_size
;
sample_id
++
)
{
for
(
size_t
x_offset
=
sample_id
*
num_classes
;
x_offset
<
(
sample_id
+
1
)
*
num_classes
;
x_offset
++
)
{
dx_data
[
x_offset
]
=
static_cast
<
T
>
(
0
);
}
auto
p_index
=
sample_id
*
num_classes
+
label_
pos_
data
[
sample_id
];
auto
p_index
=
sample_id
*
num_classes
+
label_data
[
sample_id
];
for
(
size_t
ni
=
0
;
ni
<
num_classes
;
ni
++
)
{
if
(
label_
pos_
data
[
sample_id
]
==
ni
)
continue
;
if
(
label_data
[
sample_id
]
==
ni
)
continue
;
auto
n_index
=
sample_id
*
num_classes
+
ni
;
auto
grad_
=
-
dy_data
[
sample_id
]
/
((
num_classes
-
1
)
*
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
c9a65382
...
...
@@ -1349,21 +1349,30 @@ def cross_entropy(input, label, soft_label=False, ignore_index=kIgnoreIndex):
return
out
def
bpr_loss
(
input
,
label
_pos
):
def
bpr_loss
(
input
,
label
):
"""
Bayesian Personalized Ranking Loss Operator.
This operator belongs to pairwise ranking loss. Label
Pos
is the desired item.
This operator belongs to pairwise ranking loss. Label is the desired item.
The loss at a given point in one session is defined as:
$Y[i] = -
\f
rac{1}{N_{i}-1} * \sum_{0\le j<N_{i},~ j
\n
eq Label[i]}\log(\sigma(X[i, Label[i]]-X[i, j]))$
Learn more details by reading paper <session-based recommendations with recurrent
neural networks>(https://arxiv.org/abs/1511.06939)
Args:
input (Variable|list): a 2-D tensor with shape [N x D], where N is the
batch size and D is the number of classes.
This input is not probability but logits.
label (Variable|list): the ground truth which is a 2-D tensor. `label`
is a tensor<int64> with shape [N x 1].
Returns:
A 2-D tensor with shape [N x 1], the bpr loss.
Examples:
.. code-block:: python
cost = fluid.layers.bpr_loss(input=predict, label
_pos
=label)
cost = fluid.layers.bpr_loss(input=predict, label=label)
"""
helper
=
LayerHelper
(
'bpr_loss'
,
**
locals
())
...
...
@@ -1371,7 +1380,7 @@ def bpr_loss(input, label_pos):
helper
.
append_op
(
type
=
'bpr_loss'
,
inputs
=
{
'X'
:
[
input
],
'Label
Pos'
:
[
label_pos
]},
'Label
'
:
[
label
]},
outputs
=
{
'Y'
:
[
out
]})
return
out
...
...
python/paddle/fluid/tests/unittests/test_bpr_loss_op.py
浏览文件 @
c9a65382
...
...
@@ -28,18 +28,17 @@ class TestBprLossOp1(OpTest):
batch_size
=
40
class_num
=
5
X
=
randomize_probability
(
batch_size
,
class_num
,
dtype
=
'float64'
)
label_pos
=
np
.
random
.
randint
(
0
,
class_num
,
(
batch_size
,
1
),
dtype
=
"int64"
)
label
=
np
.
random
.
randint
(
0
,
class_num
,
(
batch_size
,
1
),
dtype
=
"int64"
)
bpr_loss_result
=
[]
for
i
in
range
(
batch_size
):
sum
=
0.0
for
j
in
range
(
class_num
):
if
j
==
label
_pos
[
i
][
0
]:
if
j
==
label
[
i
][
0
]:
continue
sum
+=
(
-
np
.
log
(
1.0
+
np
.
exp
(
X
[
i
][
j
]
-
X
[
i
][
label
_pos
[
i
][
0
]])))
sum
+=
(
-
np
.
log
(
1.0
+
np
.
exp
(
X
[
i
][
j
]
-
X
[
i
][
label
[
i
][
0
]])))
bpr_loss_result
.
append
(
-
sum
/
(
class_num
-
1
))
bpr_loss
=
np
.
asmatrix
([[
x
]
for
x
in
bpr_loss_result
],
dtype
=
"float64"
)
self
.
inputs
=
{
"X"
:
X
,
"Label
Pos"
:
label_pos
}
self
.
inputs
=
{
"X"
:
X
,
"Label
"
:
label
}
self
.
outputs
=
{
"Y"
:
bpr_loss
}
def
test_check_output
(
self
):
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
c9a65382
...
...
@@ -846,6 +846,15 @@ class TestBook(unittest.TestCase):
out
=
layers
.
cross_entropy
(
x
,
label
,
False
,
4
)
self
.
assertIsNotNone
(
out
)
def
test_bpr_loss
(
self
):
program
=
Program
()
with
program_guard
(
program
):
x
=
layers
.
data
(
name
=
"x"
,
shape
=
[
30
,
10
],
dtype
=
"float32"
)
label
=
layers
.
data
(
name
=
"label"
,
shape
=
[
30
,
1
],
dtype
=
"int32"
)
out
=
layers
.
bpr_loss
(
x
,
label
)
self
.
assertIsNotNone
(
out
)
print
(
str
(
program
))
def
test_expand
(
self
):
program
=
Program
()
with
program_guard
(
program
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录