Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
70bf732f
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看板
提交
70bf732f
编写于
4月 18, 2018
作者:
Y
Yancey1989
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine get interface
上级
b920b516
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
29 addition
and
21 deletion
+29
-21
paddle/fluid/framework/selected_rows.cc
paddle/fluid/framework/selected_rows.cc
+17
-12
paddle/fluid/framework/selected_rows.h
paddle/fluid/framework/selected_rows.h
+5
-6
paddle/fluid/framework/selected_rows_test.cc
paddle/fluid/framework/selected_rows_test.cc
+7
-3
未找到文件。
paddle/fluid/framework/selected_rows.cc
浏览文件 @
70bf732f
...
...
@@ -121,27 +121,32 @@ bool SelectedRows::HasKey(int64_t key) const {
:
true
;
}
bool
SelectedRows
::
Get
(
int64_t
key
,
framework
::
Tensor
*
value
,
int64_t
offset
)
const
{
int64_t
index
=
Index
(
key
);
PADDLE_ENFORCE_GE
(
index
,
0
,
"The key should be exists in the Table."
);
std
::
vector
<
int64_t
>
SelectedRows
::
Get
(
std
::
vector
<
int64_t
>
keys
,
framework
::
Tensor
*
value
)
const
{
PADDLE_ENFORCE
(
value
->
IsInitialized
(),
"The value tensor should be initialized."
);
int64_t
value_width
=
value
->
numel
()
/
value
->
dims
()[
0
];
PADDLE_ENFORCE_EQ
(
value_width
,
value_
->
numel
()
/
value_
->
dims
()[
0
],
std
::
vector
<
int64_t
>
non_keys
;
int64_t
value_width
=
value_
->
numel
()
/
value_
->
dims
()[
0
];
PADDLE_ENFORCE_EQ
(
value_width
,
value
->
numel
()
/
value
->
dims
()[
0
],
"output tensor should have the same shape with table "
"execpt the dims[0]."
);
// TODO(Yancey1989): support other place
platform
::
CPUPlace
cpu
;
framework
::
VisitDataType
(
framework
::
ToDataType
(
value_
->
type
()),
TensorCopyVisitor
(
cpu
,
value
,
offset
*
value_width
,
*
value_
.
get
(),
index
*
value_width
,
value_width
));
return
true
;
for
(
size_t
i
=
0
;
i
<
keys
.
size
();
++
i
)
{
int64_t
index
=
Index
(
keys
[
i
]);
if
(
index
==
-
1
)
{
non_keys
.
push_back
(
keys
[
i
]);
}
else
{
framework
::
VisitDataType
(
framework
::
ToDataType
(
value_
->
type
()),
TensorCopyVisitor
(
cpu
,
value
,
i
*
value_width
,
*
value_
.
get
(),
index
*
value_width
,
value_width
));
}
}
return
non_keys
;
}
bool
SelectedRows
::
Set
(
int64_t
key
,
const
framework
::
Tensor
&
value
)
{
...
...
paddle/fluid/framework/selected_rows.h
浏览文件 @
70bf732f
...
...
@@ -35,7 +35,7 @@ class SelectedRows {
*
* HasKey(key), whether the sparse table has the specified key.
* Set(key, value), set a key-value pair into the sparse table.
* Get(key
, value*, offset), get a value by key
and apply it to the given
* Get(key
s, value*), get value by given key list
and apply it to the given
* value pointer
* with the specified offset.
*
...
...
@@ -75,13 +75,12 @@ class SelectedRows {
bool
HasKey
(
int64_t
key
)
const
;
/*
* @brief Get a value by the specified key, if the
* key does not exists, this function would throw an exception.
* @brief Get value by the key list, if the
*
* @return
true if the Get operation successed.
* @return
a list of keys which does not exists in table
*/
bool
Get
(
int64_t
key
,
framework
::
Tensor
*
tensor
,
int64_t
offset
=
0
)
const
;
std
::
vector
<
int64_t
>
Get
(
std
::
vector
<
int64_t
>
keys
,
framework
::
Tensor
*
tensor
)
const
;
/*
* @brief Set a key-value pair into the table.
...
...
paddle/fluid/framework/selected_rows_test.cc
浏览文件 @
70bf732f
...
...
@@ -68,6 +68,7 @@ TEST_F(SelectedRowsTester, Table) {
table
.
mutable_rows
()
->
push_back
(
1
);
int64_t
key
=
10000
;
int64_t
non_key
=
999
;
framework
::
Tensor
value
;
value
.
Resize
(
framework
::
make_ddim
({
1
,
100
}));
auto
ptr
=
value
.
mutable_data
<
float
>
(
cpu
);
...
...
@@ -84,10 +85,13 @@ TEST_F(SelectedRowsTester, Table) {
ASSERT_EQ
(
table
.
value
().
dims
()[
0
],
static_cast
<
int64_t
>
(
4
));
framework
::
Tensor
get_value
;
get_value
.
mutable_data
<
float
>
(
framework
::
make_ddim
({
20
,
100
}),
cpu
);
table
.
Get
(
key
,
&
get_value
,
10
);
get_value
.
mutable_data
<
float
>
(
framework
::
make_ddim
({
2
,
100
}),
cpu
);
std
::
vector
<
int64_t
>
keys
({
non_key
,
key
});
auto
non_keys
=
table
.
Get
(
keys
,
&
get_value
);
ASSERT_EQ
(
get_value
.
data
<
float
>
()[
10
*
100
],
static_cast
<
float
>
(
10
));
ASSERT_EQ
(
get_value
.
data
<
float
>
()[
100
],
static_cast
<
float
>
(
10
));
ASSERT_EQ
(
non_keys
.
size
(),
static_cast
<
size_t
>
(
1
));
ASSERT_EQ
(
non_keys
[
0
],
non_key
);
}
}
// namespace framework
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录