Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
29262ab2
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看板
提交
29262ab2
编写于
11月 29, 2017
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix unitest.
上级
76a65a83
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
19 addition
and
19 deletion
+19
-19
paddle/operators/nce_op.cc
paddle/operators/nce_op.cc
+4
-4
paddle/operators/nce_op.h
paddle/operators/nce_op.h
+8
-8
python/paddle/v2/fluid/tests/test_nce.py
python/paddle/v2/fluid/tests/test_nce.py
+7
-7
未找到文件。
paddle/operators/nce_op.cc
浏览文件 @
29262ab2
...
@@ -41,11 +41,11 @@ class NCEOp : public framework::OperatorWithKernel {
...
@@ -41,11 +41,11 @@ class NCEOp : public framework::OperatorWithKernel {
}
}
auto
num_neg_samples
=
ctx
->
Attrs
().
Get
<
int
>
(
"num_neg_samples"
);
auto
num_neg_samples
=
ctx
->
Attrs
().
Get
<
int
>
(
"num_neg_samples"
);
auto
num_total_classes
=
ctx
->
Attrs
().
Get
<
int
>
(
"num_total_classes"
);
auto
num_total_classes
=
ctx
->
Attrs
().
Get
<
int
>
(
"num_total_classes"
);
std
::
vector
<
int
>
sampled_label
s
=
std
::
vector
<
int
>
custom_neg_classe
s
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"
sampled_label
s"
);
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"
custom_neg_classe
s"
);
PADDLE_ENFORCE_EQ
(
num_total_classes
,
ctx
->
GetInputDim
(
"Weight"
)[
0
]);
PADDLE_ENFORCE_EQ
(
num_total_classes
,
ctx
->
GetInputDim
(
"Weight"
)[
0
]);
if
(
sampled_label
s
.
size
()
>
0
)
{
if
(
custom_neg_classe
s
.
size
()
>
0
)
{
PADDLE_ENFORCE_EQ
(
sampled_label
s
.
size
(),
PADDLE_ENFORCE_EQ
(
custom_neg_classe
s
.
size
(),
static_cast
<
size_t
>
(
num_neg_samples
));
static_cast
<
size_t
>
(
num_neg_samples
));
}
}
// set dims of output(Out)
// set dims of output(Out)
...
...
paddle/operators/nce_op.h
浏览文件 @
29262ab2
...
@@ -33,14 +33,14 @@ void PrepareSamples(const framework::ExecutionContext& context) {
...
@@ -33,14 +33,14 @@ void PrepareSamples(const framework::ExecutionContext& context) {
auto
label
=
context
.
Input
<
Tensor
>
(
"Label"
);
auto
label
=
context
.
Input
<
Tensor
>
(
"Label"
);
const
int64_t
*
label_data
=
label
->
data
<
int64_t
>
();
const
int64_t
*
label_data
=
label
->
data
<
int64_t
>
();
auto
label_dims
=
label
->
dims
();
auto
label_dims
=
label
->
dims
();
int
num_
classes
=
context
.
Attr
<
int
>
(
"num
_classes"
);
int
num_
total_classes
=
context
.
Attr
<
int
>
(
"num_total
_classes"
);
// for unitest
// for unitest
std
::
vector
<
int
>
custom_neg_classes
=
std
::
vector
<
int
>
custom_neg_classes
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"custom_neg_classes"
);
context
.
Attr
<
std
::
vector
<
int
>>
(
"custom_neg_classes"
);
// random machine
// random machine
std
::
random_device
rd
;
std
::
random_device
rd
;
std
::
mt19937
rng
(
rd
());
std
::
mt19937
rng
(
rd
());
std
::
uniform_int_distribution
<
int
>
rand
(
0
,
num_classes
-
1
);
std
::
uniform_int_distribution
<
int
>
rand
(
0
,
num_
total_
classes
-
1
);
auto
sample_labels
=
context
.
Output
<
Tensor
>
(
"SampleLabels"
);
auto
sample_labels
=
context
.
Output
<
Tensor
>
(
"SampleLabels"
);
auto
sample_labels_dims
=
sample_labels
->
dims
();
auto
sample_labels_dims
=
sample_labels
->
dims
();
...
@@ -84,13 +84,13 @@ class NCEKernel : public framework::OpKernel<T> {
...
@@ -84,13 +84,13 @@ class NCEKernel : public framework::OpKernel<T> {
}
}
auto
out
=
context
.
Output
<
Tensor
>
(
"Cost"
);
auto
out
=
context
.
Output
<
Tensor
>
(
"Cost"
);
T
*
out_data
=
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
T
*
out_data
=
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int
num_
smalped_classes
=
context
.
Attr
<
int
>
(
"num_sampled_class
es"
);
int
num_
neg_samples
=
context
.
Attr
<
int
>
(
"num_neg_sampl
es"
);
int
num_
classes
=
context
.
Attr
<
int
>
(
"num
_classes"
);
int
num_
total_classes
=
context
.
Attr
<
int
>
(
"num_total
_classes"
);
int
num_true_class
=
1
;
int
num_true_class
=
1
;
if
(
label
!=
nullptr
)
{
if
(
label
!=
nullptr
)
{
num_true_class
=
label
->
dims
()[
1
];
num_true_class
=
label
->
dims
()[
1
];
}
}
T
b
=
1.
/
num_
classes
*
num_smalped_class
es
;
T
b
=
1.
/
num_
total_classes
*
num_neg_sampl
es
;
// forward bias
// forward bias
auto
bias
=
context
.
Input
<
Tensor
>
(
"Bias"
);
auto
bias
=
context
.
Input
<
Tensor
>
(
"Bias"
);
if
(
bias
!=
nullptr
)
{
if
(
bias
!=
nullptr
)
{
...
@@ -151,13 +151,13 @@ class NCEGradKernel : public framework::OpKernel<T> {
...
@@ -151,13 +151,13 @@ class NCEGradKernel : public framework::OpKernel<T> {
if
(
sample_weight
!=
nullptr
)
{
if
(
sample_weight
!=
nullptr
)
{
sample_weight_data
=
sample_weight
->
data
<
T
>
();
sample_weight_data
=
sample_weight
->
data
<
T
>
();
}
}
int
num_
smalped_classes
=
context
.
Attr
<
int
>
(
"num_sampled_class
es"
);
int
num_
neg_samples
=
context
.
Attr
<
int
>
(
"num_neg_sampl
es"
);
int
num_
classes
=
context
.
Attr
<
int
>
(
"num
_classes"
);
int
num_
total_classes
=
context
.
Attr
<
int
>
(
"num_total
_classes"
);
int
num_true_class
=
1
;
int
num_true_class
=
1
;
if
(
label
!=
nullptr
)
{
if
(
label
!=
nullptr
)
{
num_true_class
=
label
->
dims
()[
1
];
num_true_class
=
label
->
dims
()[
1
];
}
}
T
b
=
1.
/
num_
classes
*
num_smalped_class
es
;
T
b
=
1.
/
num_
total_classes
*
num_neg_sampl
es
;
Tensor
sample_grad
;
// tmp tensor
Tensor
sample_grad
;
// tmp tensor
T
*
sample_grad_data
=
T
*
sample_grad_data
=
sample_grad
.
mutable_data
<
T
>
(
sample_labels
->
dims
(),
context
.
GetPlace
());
sample_grad
.
mutable_data
<
T
>
(
sample_labels
->
dims
(),
context
.
GetPlace
());
...
...
python/paddle/v2/fluid/tests/test_nce.py
浏览文件 @
29262ab2
...
@@ -35,7 +35,7 @@ def nce(input, weight, bias, sample_weight, labels, num_classes,
...
@@ -35,7 +35,7 @@ def nce(input, weight, bias, sample_weight, labels, num_classes,
o
=
sample_out
[
i
]
o
=
sample_out
[
i
]
cost
=
-
np
.
log
(
o
/
(
o
+
b
))
if
samples
[
i
][
2
]
else
-
np
.
log
(
b
/
(
o
+
b
))
cost
=
-
np
.
log
(
o
/
(
o
+
b
))
if
samples
[
i
][
2
]
else
-
np
.
log
(
b
/
(
o
+
b
))
out
[
samples
[
i
][
0
]]
+=
cost
*
samples
[
i
][
3
]
out
[
samples
[
i
][
0
]]
+=
cost
*
samples
[
i
][
3
]
return
(
out
,
np
.
array
(
sample_out
).
reshape
(
return
(
out
[:,
np
.
newaxis
]
,
np
.
array
(
sample_out
).
reshape
(
batch_size
,
num_sample_class
+
num_true_class
),
batch_size
,
num_sample_class
+
num_true_class
),
np
.
array
(
sample_labels
).
reshape
(
batch_size
,
np
.
array
(
sample_labels
).
reshape
(
batch_size
,
num_sample_class
+
num_true_class
))
num_sample_class
+
num_true_class
))
...
@@ -43,16 +43,16 @@ def nce(input, weight, bias, sample_weight, labels, num_classes,
...
@@ -43,16 +43,16 @@ def nce(input, weight, bias, sample_weight, labels, num_classes,
class
TestNCE
(
OpTest
):
class
TestNCE
(
OpTest
):
def
generate_data
(
self
,
dim
,
batch_size
,
num_classes
,
num_true_class
,
def
generate_data
(
self
,
dim
,
batch_size
,
num_classes
,
num_true_class
,
num_
sampled_class
es
):
num_
neg_sampl
es
):
input
=
np
.
random
.
randn
(
batch_size
,
dim
).
astype
(
np
.
float32
)
input
=
np
.
random
.
randn
(
batch_size
,
dim
).
astype
(
np
.
float32
)
weight
=
np
.
random
.
randn
(
num_classes
,
dim
).
astype
(
np
.
float32
)
weight
=
np
.
random
.
randn
(
num_classes
,
dim
).
astype
(
np
.
float32
)
bias
=
np
.
random
.
randn
(
num_classes
).
astype
(
np
.
float32
)
bias
=
np
.
random
.
randn
(
num_classes
).
astype
(
np
.
float32
)
sample_weight
=
np
.
random
.
randn
(
batch_size
).
astype
(
np
.
float32
)
sample_weight
=
np
.
random
.
randn
(
batch_size
).
astype
(
np
.
float32
)
labels
=
np
.
random
.
randint
(
0
,
num_classes
,
(
batch_size
,
num_true_class
))
labels
=
np
.
random
.
randint
(
0
,
num_classes
,
(
batch_size
,
num_true_class
))
self
.
attrs
=
{
self
.
attrs
=
{
'num_classes'
:
num_classes
,
'num_
total_
classes'
:
num_classes
,
'num_
sampled_classes'
:
num_sampled_class
es
,
'num_
neg_samples'
:
num_neg_sampl
es
,
'
sampled_labels'
:
range
(
num_sampled_class
es
)
'
custom_neg_classes'
:
range
(
num_neg_sampl
es
)
}
}
self
.
inputs
=
{
self
.
inputs
=
{
'Input'
:
input
,
'Input'
:
input
,
...
@@ -68,8 +68,8 @@ class TestNCE(OpTest):
...
@@ -68,8 +68,8 @@ class TestNCE(OpTest):
def
compute
(
self
):
def
compute
(
self
):
out
=
nce
(
self
.
inputs
[
'Input'
],
self
.
inputs
[
'Weight'
],
out
=
nce
(
self
.
inputs
[
'Input'
],
self
.
inputs
[
'Weight'
],
self
.
inputs
[
'Bias'
],
self
.
inputs
[
'SampleWeight'
],
self
.
inputs
[
'Bias'
],
self
.
inputs
[
'SampleWeight'
],
self
.
inputs
[
'Label'
],
self
.
attrs
[
'num_classes'
],
self
.
inputs
[
'Label'
],
self
.
attrs
[
'num_
total_
classes'
],
self
.
attrs
[
'num_
sampled_class
es'
])
self
.
attrs
[
'num_
neg_sampl
es'
])
self
.
outputs
=
{
self
.
outputs
=
{
'Cost'
:
out
[
0
],
'Cost'
:
out
[
0
],
'SampleLogits'
:
out
[
1
],
'SampleLogits'
:
out
[
1
],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录