Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
cdc236cb
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
cdc236cb
编写于
10月 16, 2017
作者:
Q
QI JUN
提交者:
GitHub
10月 16, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4841 from QiJune/pybind_selected_rows
export SelectedRows to Python
上级
8e52b34a
c65bdd95
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
64 addition
and
0 deletion
+64
-0
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+27
-0
python/paddle/v2/framework/tests/test_selected_rows.py
python/paddle/v2/framework/tests/test_selected_rows.py
+37
-0
未找到文件。
paddle/pybind/pybind.cc
浏览文件 @
cdc236cb
...
...
@@ -18,6 +18,7 @@ limitations under the License. */
#include "paddle/framework/executor.h"
#include "paddle/framework/feed_fetch_method.h"
#include "paddle/framework/lod_tensor.h"
#include "paddle/framework/selected_rows.h"
#include "paddle/framework/tensor_array.h"
#include "paddle/operators/cond_op.h"
#include "paddle/operators/dynamic_recurrent_op.h"
...
...
@@ -139,6 +140,32 @@ PYBIND11_PLUGIN(core) {
#endif
});
py
::
class_
<
SelectedRows
>
(
m
,
"SelectedRows"
)
.
def
(
"__init__"
,
[](
SelectedRows
&
instance
)
{
new
(
&
instance
)
SelectedRows
();
})
.
def
(
"__init__"
,
[](
SelectedRows
&
instance
,
const
std
::
vector
<
int64_t
>
rows
,
const
int64_t
&
height
)
{
new
(
&
instance
)
SelectedRows
(
rows
,
height
);
})
.
def
(
"get_tensor"
,
[](
SelectedRows
&
self
)
{
return
self
.
mutable_value
();
},
py
::
return_value_policy
::
reference
)
.
def
(
"set_height"
,
&
SelectedRows
::
set_height
)
.
def
(
"height"
,
&
SelectedRows
::
height
)
.
def
(
"set_rows"
,
&
SelectedRows
::
set_rows
)
.
def
(
"rows"
,
[](
SelectedRows
&
self
)
{
#ifndef PADDLE_WITH_CUDA
return
self
.
rows
();
#else
auto
rows
=
self
.
rows
();
std
::
vector
<
int64_t
>
new_rows
;
new_rows
.
reserve
(
rows
.
size
());
std
::
copy
(
rows
.
begin
(),
rows
.
end
(),
std
::
back_inserter
(
new_rows
));
return
new_rows
;
#endif
});
py
::
class_
<
Variable
>
(
m
,
"Variable"
,
R"DOC(Variable Class.
All parameter, weight, gradient are variables in Paddle.
...
...
python/paddle/v2/framework/tests/test_selected_rows.py
0 → 100644
浏览文件 @
cdc236cb
import
paddle.v2.framework.core
as
core
import
unittest
import
numpy
as
np
class
TestSelectedRows
(
unittest
.
TestCase
):
def
test_selected_rows
(
self
):
place
=
core
.
CPUPlace
()
height
=
10
rows
=
[
0
,
4
,
7
]
row_numel
=
10
selcted_rows
=
core
.
SelectedRows
(
rows
,
row_numel
)
np_array
=
np
.
ones
((
len
(
rows
),
height
)).
astype
(
"float32"
)
np_array
[
0
,
0
]
=
2.0
np_array
[
2
,
8
]
=
4.0
tensor
=
selcted_rows
.
get_tensor
()
tensor
.
set
(
np_array
,
place
)
# compare rows
self
.
assertEqual
(
0
,
selcted_rows
.
rows
()[
0
])
self
.
assertEqual
(
4
,
selcted_rows
.
rows
()[
1
])
self
.
assertEqual
(
7
,
selcted_rows
.
rows
()[
2
])
# compare height
self
.
assertEqual
(
10
,
selcted_rows
.
height
())
# compare tensor
self
.
assertAlmostEqual
(
2.0
,
selcted_rows
.
get_tensor
().
get_float_element
(
0
))
self
.
assertAlmostEqual
(
1.0
,
selcted_rows
.
get_tensor
().
get_float_element
(
1
))
self
.
assertAlmostEqual
(
4.0
,
selcted_rows
.
get_tensor
().
get_float_element
(
2
*
row_numel
+
8
))
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录