Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
31e8d807
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
31e8d807
编写于
4月 03, 2018
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
optimize code
上级
af1d3f5b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
18 addition
and
22 deletion
+18
-22
paddle/fluid/framework/selected_rows.cc
paddle/fluid/framework/selected_rows.cc
+0
-6
paddle/fluid/framework/selected_rows.h
paddle/fluid/framework/selected_rows.h
+9
-5
paddle/fluid/operators/lookup_table_op.h
paddle/fluid/operators/lookup_table_op.h
+8
-10
paddle/fluid/operators/sgd_op.h
paddle/fluid/operators/sgd_op.h
+1
-1
未找到文件。
paddle/fluid/framework/selected_rows.cc
浏览文件 @
31e8d807
...
...
@@ -17,12 +17,6 @@ limitations under the License. */
namespace
paddle
{
namespace
framework
{
size_t
GetIndex
(
const
std
::
vector
<
int64_t
>&
rows
,
int64_t
value
)
{
auto
it
=
std
::
find
(
rows
.
begin
(),
rows
.
end
(),
value
);
PADDLE_ENFORCE
(
it
!=
rows
.
end
(),
"id should be in rows"
);
return
static_cast
<
size_t
>
(
std
::
distance
(
rows
.
begin
(),
it
));
}
void
SerializeToStream
(
std
::
ostream
&
os
,
const
SelectedRows
&
selected_rows
,
const
platform
::
DeviceContext
&
dev_ctx
)
{
{
// the 1st field, uint32_t version
...
...
paddle/fluid/framework/selected_rows.h
浏览文件 @
31e8d807
...
...
@@ -50,6 +50,15 @@ class SelectedRows {
void
set_rows
(
const
Vector
<
int64_t
>&
rows
)
{
rows_
=
rows
;
}
/**
* get the index of id in rows
*/
int64_t
index
(
int64_t
id
)
const
{
auto
it
=
std
::
find
(
rows_
.
begin
(),
rows_
.
end
(),
id
);
PADDLE_ENFORCE
(
it
!=
rows_
.
end
(),
"id should be in rows"
);
return
static_cast
<
int64_t
>
(
std
::
distance
(
rows_
.
begin
(),
it
));
}
DDim
GetCompleteDims
()
const
{
std
::
vector
<
int64_t
>
dims
=
vectorize
(
value_
->
dims
());
dims
[
0
]
=
height_
;
...
...
@@ -65,11 +74,6 @@ class SelectedRows {
int64_t
height_
;
};
/**
* Find the index of value in rows.
*/
size_t
GetIndex
(
const
std
::
vector
<
int64_t
>&
rows
,
int64_t
value
);
/*
* Serialize/Desiralize SelectedRows to std::ostream
* You can pass ofstream or ostringstream to serilize to file
...
...
paddle/fluid/operators/lookup_table_op.h
浏览文件 @
31e8d807
...
...
@@ -30,13 +30,7 @@ using LoDTensor = framework::LoDTensor;
using
SelectedRows
=
framework
::
SelectedRows
;
using
DDim
=
framework
::
DDim
;
static
constexpr
int64_t
kNoPadding
=
-
1
;
inline
size_t
getIndex
(
const
std
::
vector
<
int64_t
>
&
rows
,
int64_t
value
)
{
auto
it
=
std
::
find
(
rows
.
begin
(),
rows
.
end
(),
value
);
PADDLE_ENFORCE
(
it
!=
rows
.
end
(),
"id should be in rows"
);
return
static_cast
<
size_t
>
(
std
::
distance
(
rows
.
begin
(),
it
));
}
constexpr
int64_t
kNoPadding
=
-
1
;
template
<
typename
T
>
class
LookupTableKernel
:
public
framework
::
OpKernel
<
T
>
{
...
...
@@ -55,7 +49,9 @@ class LookupTableKernel : public framework::OpKernel<T> {
auto
*
table_t
=
context
.
Input
<
SelectedRows
>
(
"W"
);
table_dim
=
table_t
->
value
().
dims
();
}
else
{
PADDLE_THROW
(
"table only support LoDTensor and SelectedRows"
);
PADDLE_THROW
(
"The parameter W of a LookupTable "
"must be either LoDTensor or SelectedRows"
);
}
int64_t
*
ids
;
...
...
@@ -107,7 +103,7 @@ class LookupTableKernel : public framework::OpKernel<T> {
memset
(
output
+
i
*
row_width
,
0
,
row_width
*
sizeof
(
T
));
}
else
{
PADDLE_ENFORCE_GE
(
ids
[
i
],
0
);
auto
id_index
=
getIndex
(
table_t
.
rows
(),
ids
[
i
]);
auto
id_index
=
table_t
.
index
(
ids
[
i
]);
memcpy
(
output
+
i
*
row_width
,
table
+
id_index
*
row_width
,
row_width
*
sizeof
(
T
));
}
...
...
@@ -128,7 +124,9 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
auto
*
table_t
=
context
.
Input
<
SelectedRows
>
(
"W"
);
table_dim
=
table_t
->
value
().
dims
();
}
else
{
PADDLE_THROW
(
"table only support LoDTensor and SelectedRows"
);
PADDLE_THROW
(
"The parameter W of a LookupTable "
"must be either LoDTensor or SelectedRows"
);
}
bool
is_sparse
=
context
.
Attr
<
bool
>
(
"is_sparse"
);
...
...
paddle/fluid/operators/sgd_op.h
浏览文件 @
31e8d807
...
...
@@ -106,7 +106,7 @@ class SGDOpKernel : public framework::OpKernel<T> {
for
(
size_t
i
=
0
;
i
<
grad
.
rows
().
size
();
i
++
)
{
PADDLE_ENFORCE
(
grad
.
rows
()[
i
]
<
grad
.
height
(),
"Input rows index should less than height"
);
size_t
id_index
=
framework
::
GetIndex
(
param
.
rows
(),
grad
.
rows
()[
i
]);
int64_t
id_index
=
param
.
index
(
grad
.
rows
()[
i
]);
for
(
int64_t
j
=
0
;
j
<
grad_row_width
;
j
++
)
{
out_data
[
id_index
*
grad_row_width
+
j
]
-=
lr
[
0
]
*
grad_data
[
i
*
grad_row_width
+
j
];
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录