Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
29262ab2
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看板
提交
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 {
}
auto
num_neg_samples
=
ctx
->
Attrs
().
Get
<
int
>
(
"num_neg_samples"
);
auto
num_total_classes
=
ctx
->
Attrs
().
Get
<
int
>
(
"num_total_classes"
);
std
::
vector
<
int
>
sampled_label
s
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"
sampled_label
s"
);
std
::
vector
<
int
>
custom_neg_classe
s
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"
custom_neg_classe
s"
);
PADDLE_ENFORCE_EQ
(
num_total_classes
,
ctx
->
GetInputDim
(
"Weight"
)[
0
]);
if
(
sampled_label
s
.
size
()
>
0
)
{
PADDLE_ENFORCE_EQ
(
sampled_label
s
.
size
(),
if
(
custom_neg_classe
s
.
size
()
>
0
)
{
PADDLE_ENFORCE_EQ
(
custom_neg_classe
s
.
size
(),
static_cast
<
size_t
>
(
num_neg_samples
));
}
// set dims of output(Out)
...
...
paddle/operators/nce_op.h
浏览文件 @
29262ab2
...
...
@@ -33,14 +33,14 @@ void PrepareSamples(const framework::ExecutionContext& context) {
auto
label
=
context
.
Input
<
Tensor
>
(
"Label"
);
const
int64_t
*
label_data
=
label
->
data
<
int64_t
>
();
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
std
::
vector
<
int
>
custom_neg_classes
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"custom_neg_classes"
);
// random machine
std
::
random_device
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_dims
=
sample_labels
->
dims
();
...
...
@@ -84,13 +84,13 @@ class NCEKernel : public framework::OpKernel<T> {
}
auto
out
=
context
.
Output
<
Tensor
>
(
"Cost"
);
T
*
out_data
=
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int
num_
smalped_classes
=
context
.
Attr
<
int
>
(
"num_sampled_class
es"
);
int
num_
classes
=
context
.
Attr
<
int
>
(
"num
_classes"
);
int
num_
neg_samples
=
context
.
Attr
<
int
>
(
"num_neg_sampl
es"
);
int
num_
total_classes
=
context
.
Attr
<
int
>
(
"num_total
_classes"
);
int
num_true_class
=
1
;
if
(
label
!=
nullptr
)
{
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
auto
bias
=
context
.
Input
<
Tensor
>
(
"Bias"
);
if
(
bias
!=
nullptr
)
{
...
...
@@ -151,13 +151,13 @@ class NCEGradKernel : public framework::OpKernel<T> {
if
(
sample_weight
!=
nullptr
)
{
sample_weight_data
=
sample_weight
->
data
<
T
>
();
}
int
num_
smalped_classes
=
context
.
Attr
<
int
>
(
"num_sampled_class
es"
);
int
num_
classes
=
context
.
Attr
<
int
>
(
"num
_classes"
);
int
num_
neg_samples
=
context
.
Attr
<
int
>
(
"num_neg_sampl
es"
);
int
num_
total_classes
=
context
.
Attr
<
int
>
(
"num_total
_classes"
);
int
num_true_class
=
1
;
if
(
label
!=
nullptr
)
{
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
T
*
sample_grad_data
=
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,
o
=
sample_out
[
i
]
cost
=
-
np
.
log
(
o
/
(
o
+
b
))
if
samples
[
i
][
2
]
else
-
np
.
log
(
b
/
(
o
+
b
))
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
),
np
.
array
(
sample_labels
).
reshape
(
batch_size
,
num_sample_class
+
num_true_class
))
...
...
@@ -43,16 +43,16 @@ def nce(input, weight, bias, sample_weight, labels, num_classes,
class
TestNCE
(
OpTest
):
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
)
weight
=
np
.
random
.
randn
(
num_classes
,
dim
).
astype
(
np
.
float32
)
bias
=
np
.
random
.
randn
(
num_classes
).
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
))
self
.
attrs
=
{
'num_classes'
:
num_classes
,
'num_
sampled_classes'
:
num_sampled_class
es
,
'
sampled_labels'
:
range
(
num_sampled_class
es
)
'num_
total_
classes'
:
num_classes
,
'num_
neg_samples'
:
num_neg_sampl
es
,
'
custom_neg_classes'
:
range
(
num_neg_sampl
es
)
}
self
.
inputs
=
{
'Input'
:
input
,
...
...
@@ -68,8 +68,8 @@ class TestNCE(OpTest):
def
compute
(
self
):
out
=
nce
(
self
.
inputs
[
'Input'
],
self
.
inputs
[
'Weight'
],
self
.
inputs
[
'Bias'
],
self
.
inputs
[
'SampleWeight'
],
self
.
inputs
[
'Label'
],
self
.
attrs
[
'num_classes'
],
self
.
attrs
[
'num_
sampled_class
es'
])
self
.
inputs
[
'Label'
],
self
.
attrs
[
'num_
total_
classes'
],
self
.
attrs
[
'num_
neg_sampl
es'
])
self
.
outputs
=
{
'Cost'
:
out
[
0
],
'SampleLogits'
:
out
[
1
],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录