Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
9247aee7
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
9247aee7
编写于
1月 18, 2018
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Enhance lookup_table_op to support padding_idx
上级
f086ebb8
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
22 addition
and
3 deletion
+22
-3
paddle/operators/lookup_table_op.cc
paddle/operators/lookup_table_op.cc
+6
-0
paddle/operators/lookup_table_op.h
paddle/operators/lookup_table_op.h
+16
-3
未找到文件。
paddle/operators/lookup_table_op.cc
浏览文件 @
9247aee7
...
@@ -66,6 +66,12 @@ class LookupTableOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -66,6 +66,12 @@ class LookupTableOpMaker : public framework::OpProtoAndCheckerMaker {
"(boolean, default false) "
"(boolean, default false) "
"Sparse update"
)
"Sparse update"
)
.
SetDefault
(
false
);
.
SetDefault
(
false
);
AddAttr
<
int64_t
>
(
"padding_idx"
,
"(int64_t, default -1) "
" If given, pads the output with zeros whenever it encounters "
"the index."
)
.
SetDefault
(
-
1
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
Lookup Table Operator.
Lookup Table Operator.
...
...
paddle/operators/lookup_table_op.h
浏览文件 @
9247aee7
...
@@ -32,6 +32,7 @@ class LookupTableKernel : public framework::OpKernel<T> {
...
@@ -32,6 +32,7 @@ class LookupTableKernel : public framework::OpKernel<T> {
auto
*
table_t
=
context
.
Input
<
LoDTensor
>
(
"W"
);
// float tensor
auto
*
table_t
=
context
.
Input
<
LoDTensor
>
(
"W"
);
// float tensor
auto
*
ids_t
=
context
.
Input
<
LoDTensor
>
(
"Ids"
);
// int tensor
auto
*
ids_t
=
context
.
Input
<
LoDTensor
>
(
"Ids"
);
// int tensor
auto
*
output_t
=
context
.
Output
<
LoDTensor
>
(
"Out"
);
// float tensor
auto
*
output_t
=
context
.
Output
<
LoDTensor
>
(
"Out"
);
// float tensor
int64_t
padding_idx
=
context
.
Attr
<
int64_t
>
(
"padding_idx"
);
int
N
=
table_t
->
dims
()[
0
];
int
N
=
table_t
->
dims
()[
0
];
int
D
=
table_t
->
dims
()[
1
];
int
D
=
table_t
->
dims
()[
1
];
...
@@ -39,11 +40,15 @@ class LookupTableKernel : public framework::OpKernel<T> {
...
@@ -39,11 +40,15 @@ class LookupTableKernel : public framework::OpKernel<T> {
auto
*
table
=
table_t
->
data
<
T
>
();
auto
*
table
=
table_t
->
data
<
T
>
();
auto
*
output
=
output_t
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
*
output
=
output_t
->
mutable_data
<
T
>
(
context
.
GetPlace
());
for
(
int64_t
i
=
0
;
i
<
ids_t
->
numel
();
++
i
)
{
for
(
int64_t
i
=
0
;
i
<
ids_t
->
numel
();
++
i
)
{
if
(
ids
[
i
]
==
padding_idx
)
{
memset
(
output
+
i
*
D
,
0
,
D
*
sizeof
(
T
));
}
else
{
PADDLE_ENFORCE_LT
(
ids
[
i
],
N
);
PADDLE_ENFORCE_LT
(
ids
[
i
],
N
);
PADDLE_ENFORCE_GE
(
ids
[
i
],
0
);
PADDLE_ENFORCE_GE
(
ids
[
i
],
0
);
memcpy
(
output
+
i
*
D
,
table
+
ids
[
i
]
*
D
,
D
*
sizeof
(
T
));
memcpy
(
output
+
i
*
D
,
table
+
ids
[
i
]
*
D
,
D
*
sizeof
(
T
));
}
}
}
}
}
};
};
template
<
typename
T
>
template
<
typename
T
>
...
@@ -51,6 +56,8 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
...
@@ -51,6 +56,8 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
bool
is_sparse
=
context
.
Attr
<
bool
>
(
"is_sparse"
);
bool
is_sparse
=
context
.
Attr
<
bool
>
(
"is_sparse"
);
int64_t
padding_idx
=
context
.
Attr
<
int64_t
>
(
"padding_idx"
);
if
(
is_sparse
)
{
if
(
is_sparse
)
{
auto
*
ids
=
context
.
Input
<
LoDTensor
>
(
"Ids"
);
auto
*
ids
=
context
.
Input
<
LoDTensor
>
(
"Ids"
);
auto
*
table
=
context
.
Input
<
LoDTensor
>
(
"W"
);
auto
*
table
=
context
.
Input
<
LoDTensor
>
(
"W"
);
...
@@ -63,6 +70,9 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
...
@@ -63,6 +70,9 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
framework
::
Vector
<
int64_t
>
new_rows
;
framework
::
Vector
<
int64_t
>
new_rows
;
new_rows
.
reserve
(
ids_dim
[
0
]);
new_rows
.
reserve
(
ids_dim
[
0
]);
for
(
int64_t
i
=
0
;
i
<
ids_dim
[
0
];
i
++
)
{
for
(
int64_t
i
=
0
;
i
<
ids_dim
[
0
];
i
++
)
{
if
(
ids_data
[
i
]
==
padding_idx
)
continue
;
// Paddings are not trainable and the gradient are not
// necessary.
new_rows
.
push_back
(
ids_data
[
i
]);
new_rows
.
push_back
(
ids_data
[
i
]);
}
}
d_table
->
set_rows
(
new_rows
);
d_table
->
set_rows
(
new_rows
);
...
@@ -96,6 +106,9 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
...
@@ -96,6 +106,9 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
memset
(
d_table_data
,
0
,
d_table
->
numel
()
*
sizeof
(
T
));
memset
(
d_table_data
,
0
,
d_table
->
numel
()
*
sizeof
(
T
));
for
(
int64_t
i
=
0
;
i
<
ids
->
numel
();
++
i
)
{
for
(
int64_t
i
=
0
;
i
<
ids
->
numel
();
++
i
)
{
if
(
ids_data
[
i
]
==
padding_idx
)
continue
;
// Paddings are not trainable and the gradient are not
// necessary.
PADDLE_ENFORCE_LT
(
ids_data
[
i
],
N
);
PADDLE_ENFORCE_LT
(
ids_data
[
i
],
N
);
PADDLE_ENFORCE_GE
(
ids_data
[
i
],
0
);
PADDLE_ENFORCE_GE
(
ids_data
[
i
],
0
);
for
(
int
j
=
0
;
j
<
D
;
++
j
)
{
for
(
int
j
=
0
;
j
<
D
;
++
j
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录