Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
70fecee0
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
70fecee0
编写于
11月 08, 2016
作者:
W
wangyang59
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add unittest for Matrix and Vector in API
上级
f22573bd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
71 addition
and
10 deletion
+71
-10
paddle/api/PaddleAPI.h
paddle/api/PaddleAPI.h
+3
-0
paddle/api/Vector.cpp
paddle/api/Vector.cpp
+15
-0
paddle/api/test/testMatrix.py
paddle/api/test/testMatrix.py
+11
-3
paddle/api/test/testVector.py
paddle/api/test/testVector.py
+42
-7
未找到文件。
paddle/api/PaddleAPI.h
浏览文件 @
70fecee0
...
...
@@ -272,6 +272,9 @@ public:
/// Return is GPU vector or not.
bool
isGpu
()
const
;
/// Return a list of float, the memory is alloced and copied.
FloatArray
getData
()
const
;
/// __len__ in python
size_t
getSize
()
const
;
...
...
paddle/api/Vector.cpp
浏览文件 @
70fecee0
...
...
@@ -267,6 +267,21 @@ void Vector::copyFromNumpyArray(float* data, int dim) {
m
->
vec
->
copyFrom
(
data
,
dim
);
}
FloatArray
Vector
::
getData
()
const
{
if
(
this
->
isGpu
())
{
float
*
src
=
m
->
vec
->
getData
();
size_t
len
=
m
->
vec
->
getSize
();
float
*
dest
=
new
float
[
len
];
hl_memcpy_device2host
(
dest
,
src
,
len
*
sizeof
(
float
));
FloatArray
ret_val
(
dest
,
len
);
ret_val
.
needFree
=
true
;
return
ret_val
;
}
else
{
FloatArray
ret_val
(
m
->
vec
->
getData
(),
m
->
vec
->
getSize
());
return
ret_val
;
}
}
bool
Vector
::
isGpu
()
const
{
return
std
::
dynamic_pointer_cast
<
paddle
::
GpuVector
>
(
m
->
vec
)
!=
nullptr
;
}
...
...
paddle/api/test/testMatrix.py
浏览文件 @
70fecee0
...
...
@@ -42,7 +42,7 @@ class TestMatrix(unittest.TestCase):
self
.
assertEqual
(
m
.
getSparseRowCols
(
2
),
[])
def
test_sparse_value
(
self
):
m
=
swig_paddle
.
Matrix
.
createSparse
(
3
,
3
,
6
,
False
)
m
=
swig_paddle
.
Matrix
.
createSparse
(
3
,
3
,
6
,
False
,
False
,
False
)
self
.
assertIsNotNone
(
m
)
m
.
sparseCopyFrom
([
0
,
2
,
3
,
3
],
[
0
,
1
,
2
],
[
7.3
,
4.2
,
3.2
])
...
...
@@ -66,7 +66,7 @@ class TestMatrix(unittest.TestCase):
self
.
assertIsNotNone
(
m
)
self
.
assertTrue
(
abs
(
m
.
get
(
1
,
1
)
-
0.5
)
<
1e-5
)
def
test_numpy
(
self
):
def
test_numpy
Cpu
(
self
):
numpy_mat
=
np
.
matrix
([[
1
,
2
],
[
3
,
4
],
[
5
,
6
]],
dtype
=
"float32"
)
m
=
swig_paddle
.
Matrix
.
createCpuDenseFromNumpy
(
numpy_mat
)
self
.
assertEqual
(
...
...
@@ -100,8 +100,16 @@ class TestMatrix(unittest.TestCase):
for
a
,
e
in
zip
(
gpu_m
.
getData
(),
[
1.0
,
3.23
,
3.0
,
4.0
,
5.0
,
6.0
]):
self
.
assertAlmostEqual
(
a
,
e
)
def
test_numpy
(
self
):
numpy_mat
=
np
.
matrix
([[
1
,
2
],
[
3
,
4
],
[
5
,
6
]],
dtype
=
"float32"
)
m
=
swig_paddle
.
Matrix
.
createDenseFromNumpy
(
numpy_mat
)
self
.
assertEqual
((
int
(
m
.
getHeight
()),
int
(
m
.
getWidth
())),
numpy_mat
.
shape
)
self
.
assertEqual
(
m
.
isGpu
(),
swig_paddle
.
isUsingGpu
())
for
a
,
e
in
zip
(
m
.
getData
(),
[
1.0
,
2.0
,
3.0
,
4.0
,
5.0
,
6.0
]):
self
.
assertAlmostEqual
(
a
,
e
)
if
__name__
==
"__main__"
:
swig_paddle
.
initPaddle
(
"--use_gpu=0"
)
swig_paddle
.
initPaddle
(
"--use_gpu=
1"
if
swig_paddle
.
isGpuVersion
()
else
"--use_gpu=
0"
)
unittest
.
main
()
paddle/api/test/testVector.py
浏览文件 @
70fecee0
...
...
@@ -20,20 +20,28 @@ import unittest
class
TestIVector
(
unittest
.
TestCase
):
def
test_createZero
(
self
):
m
=
swig_paddle
.
IVector
.
createZero
(
10
)
m
=
swig_paddle
.
IVector
.
createZero
(
10
,
False
)
self
.
assertIsNotNone
(
m
)
for
i
in
xrange
(
10
):
self
.
assertEqual
(
m
[
i
],
0
)
m
[
i
]
=
i
self
.
assertEqual
(
m
[
i
],
i
)
m
=
swig_paddle
.
IVector
.
createZero
(
10
)
self
.
assertEqual
(
m
.
isGpu
(),
swig_paddle
.
isUsingGpu
())
self
.
assertEqual
(
m
.
getData
(),
[
0
]
*
10
)
def
test_create
(
self
):
m
=
swig_paddle
.
IVector
.
create
(
range
(
10
))
m
=
swig_paddle
.
IVector
.
create
(
range
(
10
)
,
False
)
self
.
assertIsNotNone
(
m
)
for
i
in
xrange
(
10
):
self
.
assertEqual
(
m
[
i
],
i
)
m
=
swig_paddle
.
IVector
.
create
(
range
(
10
))
self
.
assertEqual
(
m
.
isGpu
(),
swig_paddle
.
isUsingGpu
())
self
.
assertEqual
(
m
.
getData
(),
range
(
10
))
def
test_numpy
(
self
):
def
test_
cpu_
numpy
(
self
):
vec
=
np
.
array
([
1
,
3
,
4
,
65
,
78
,
1
,
4
],
dtype
=
"int32"
)
iv
=
swig_paddle
.
IVector
.
createCpuVectorFromNumpy
(
vec
)
self
.
assertEqual
(
vec
.
shape
[
0
],
int
(
iv
.
__len__
()))
...
...
@@ -61,25 +69,43 @@ class TestIVector(unittest.TestCase):
expect_vec
=
range
(
0
,
10
)
expect_vec
[
4
]
=
7
self
.
assertEqual
(
vec
.
getData
(),
expect_vec
)
def
test_numpy
(
self
):
vec
=
np
.
array
([
1
,
3
,
4
,
65
,
78
,
1
,
4
],
dtype
=
"int32"
)
iv
=
swig_paddle
.
IVector
.
createVectorFromNumpy
(
vec
)
self
.
assertEqual
(
iv
.
isGpu
(),
swig_paddle
.
isUsingGpu
())
self
.
assertEqual
(
iv
.
getData
(),
list
(
vec
))
class
TestVector
(
unittest
.
TestCase
):
def
testCreateZero
(
self
):
v
=
swig_paddle
.
Vector
.
createZero
(
10
)
v
=
swig_paddle
.
Vector
.
createZero
(
10
,
False
)
self
.
assertIsNotNone
(
v
)
for
i
in
xrange
(
len
(
v
)):
self
.
assertTrue
(
util
.
doubleEqual
(
v
[
i
],
0
))
v
[
i
]
=
i
self
.
assertTrue
(
util
.
doubleEqual
(
v
[
i
],
i
))
v
=
swig_paddle
.
Vector
.
createZero
(
10
)
self
.
assertEqual
(
v
.
isGpu
(),
swig_paddle
.
isUsingGpu
())
self
.
assertEqual
(
v
.
getData
(),
[
0
]
*
10
)
def
testCreate
(
self
):
v
=
swig_paddle
.
Vector
.
create
([
x
/
100.0
for
x
in
xrange
(
100
)])
v
=
swig_paddle
.
Vector
.
create
([
x
/
100.0
for
x
in
xrange
(
100
)]
,
False
)
self
.
assertIsNotNone
(
v
)
for
i
in
xrange
(
len
(
v
)):
self
.
assertTrue
(
util
.
doubleEqual
(
v
[
i
],
i
/
100.0
))
self
.
assertEqual
(
100
,
len
(
v
))
v
=
swig_paddle
.
Vector
.
create
([
x
/
100.0
for
x
in
xrange
(
100
)])
self
.
assertEqual
(
v
.
isGpu
(),
swig_paddle
.
isUsingGpu
())
self
.
assertEqual
(
100
,
len
(
v
))
vdata
=
v
.
getData
()
for
i
in
xrange
(
len
(
v
)):
self
.
assertTrue
(
util
.
doubleEqual
(
vdata
[
i
],
i
/
100.0
))
def
testNumpy
(
self
):
def
test
Cpu
Numpy
(
self
):
numpy_arr
=
np
.
array
([
1.2
,
2.3
,
3.4
,
4.5
],
dtype
=
"float32"
)
vec
=
swig_paddle
.
Vector
.
createCpuVectorFromNumpy
(
numpy_arr
)
assert
isinstance
(
vec
,
swig_paddle
.
Vector
)
...
...
@@ -102,9 +128,18 @@ class TestVector(unittest.TestCase):
for
i
in
xrange
(
1
,
len
(
numpy_3
)):
util
.
doubleEqual
(
numpy_3
[
i
],
vec
[
i
])
def
testNumpy
(
self
):
numpy_arr
=
np
.
array
([
1.2
,
2.3
,
3.4
,
4.5
],
dtype
=
"float32"
)
vec
=
swig_paddle
.
Vector
.
createVectorFromNumpy
(
numpy_arr
)
self
.
assertEqual
(
vec
.
isGpu
(),
swig_paddle
.
isUsingGpu
())
vecData
=
vec
.
getData
()
for
n
,
v
in
zip
(
numpy_arr
,
vecData
):
self
.
assertTrue
(
util
.
doubleEqual
(
n
,
v
))
def
testCopyFromNumpy
(
self
):
vec
=
swig_paddle
.
Vector
.
createZero
(
1
)
vec
=
swig_paddle
.
Vector
.
createZero
(
1
,
False
)
arr
=
np
.
array
([
1.3
,
3.2
,
2.4
],
dtype
=
"float32"
)
vec
.
copyFromNumpyArray
(
arr
)
for
i
in
xrange
(
len
(
vec
)):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录