Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
b83ffda4
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看板
未验证
提交
b83ffda4
编写于
7月 11, 2018
作者:
Y
yuyang18
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Try hide APIs
上级
5832e817
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
31 addition
and
32 deletion
+31
-32
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+14
-14
python/paddle/fluid/tests/unittests/op_test.py
python/paddle/fluid/tests/unittests/op_test.py
+8
-8
python/paddle/fluid/tests/unittests/test_tensor.py
python/paddle/fluid/tests/unittests/test_tensor.py
+8
-9
python/paddle/fluid/tests/unittests/testsuite.py
python/paddle/fluid/tests/unittests/testsuite.py
+1
-1
未找到文件。
paddle/fluid/pybind/pybind.cc
浏览文件 @
b83ffda4
...
...
@@ -78,37 +78,37 @@ PYBIND11_PLUGIN(core) {
py
::
class_
<
Tensor
>
(
m
,
"Tensor"
,
py
::
buffer_protocol
())
.
def_buffer
(
[](
Tensor
&
self
)
->
py
::
buffer_info
{
return
CastToPyBuffer
(
self
);
})
.
def
(
"get_dims"
,
.
def
(
"
_
get_dims"
,
[](
const
Tensor
&
self
)
{
return
vectorize
(
self
.
dims
());
})
.
def
(
"set_dims"
,
.
def
(
"
_
set_dims"
,
[](
Tensor
&
self
,
const
std
::
vector
<
int64_t
>
&
dim
)
{
self
.
Resize
(
make_ddim
(
dim
));
})
.
def
(
"set_layout"
,
.
def
(
"
_
set_layout"
,
[](
Tensor
&
self
,
const
std
::
string
&
layout
)
{
self
.
set_layout
(
StringToDataLayout
(
layout
));
})
.
def
(
"alloc_float"
,
.
def
(
"
_
alloc_float"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPlace
&
place
)
{
self
.
mutable_data
<
float
>
(
place
);
})
.
def
(
"alloc_float"
,
.
def
(
"
_
alloc_float"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CPUPlace
&
place
)
{
self
.
mutable_data
<
float
>
(
place
);
})
.
def
(
"alloc_int"
,
.
def
(
"
_
alloc_int"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CPUPlace
&
place
)
{
self
.
mutable_data
<
int
>
(
place
);
})
.
def
(
"alloc_int"
,
.
def
(
"
_
alloc_int"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPlace
&
place
)
{
self
.
mutable_data
<
int
>
(
place
);
})
.
def
(
"alloc_int"
,
.
def
(
"
_
alloc_int"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPinnedPlace
&
place
)
{
self
.
mutable_data
<
int
>
(
place
);
})
.
def
(
"alloc_float"
,
.
def
(
"
_
alloc_float"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPinnedPlace
&
place
)
{
self
.
mutable_data
<
float
>
(
place
);
})
...
...
@@ -136,11 +136,11 @@ PYBIND11_PLUGIN(core) {
.
def
(
"set"
,
PyCUDAPinnedTensorSetFromArray
<
uint8_t
>
)
#endif
.
def
(
"shape"
,
[](
Tensor
&
self
)
{
return
vectorize
(
self
.
dims
());
})
.
def
(
"set_float_element"
,
TensorSetElement
<
float
>
)
.
def
(
"get_float_element"
,
TensorGetElement
<
float
>
)
.
def
(
"set_double_element"
,
TensorSetElement
<
double
>
)
.
def
(
"get_double_element"
,
TensorGetElement
<
double
>
)
.
def
(
"dtype"
,
[](
Tensor
&
self
)
{
return
ToDataType
(
self
.
type
());
});
.
def
(
"
_
set_float_element"
,
TensorSetElement
<
float
>
)
.
def
(
"
_
get_float_element"
,
TensorGetElement
<
float
>
)
.
def
(
"
_
set_double_element"
,
TensorSetElement
<
double
>
)
.
def
(
"
_
get_double_element"
,
TensorGetElement
<
double
>
)
.
def
(
"
_
dtype"
,
[](
Tensor
&
self
)
{
return
ToDataType
(
self
.
type
());
});
py
::
class_
<
LoDTensor
,
Tensor
>
(
m
,
"LoDTensor"
)
.
def_buffer
(
...
...
python/paddle/fluid/tests/unittests/op_test.py
浏览文件 @
b83ffda4
...
...
@@ -60,8 +60,8 @@ def get_numeric_gradient(place,
return
np
.
array
(
sum
).
mean
()
tensor_to_check
=
scope
.
find_var
(
input_to_check
).
get_tensor
()
tensor_size
=
product
(
tensor_to_check
.
get_dims
())
tensor_to_check_dtype
=
tensor_to_check
.
dtype
()
tensor_size
=
product
(
tensor_to_check
.
shape
())
tensor_to_check_dtype
=
tensor_to_check
.
_
dtype
()
if
tensor_to_check_dtype
==
core
.
VarDesc
.
VarType
.
FP32
:
tensor_to_check_dtype
=
np
.
float32
elif
tensor_to_check_dtype
==
core
.
VarDesc
.
VarType
.
FP64
:
...
...
@@ -74,15 +74,15 @@ def get_numeric_gradient(place,
def
__get_elem__
(
tensor
,
i
):
if
tensor_to_check_dtype
==
np
.
float32
:
return
tensor
.
get_float_element
(
i
)
return
tensor
.
_
get_float_element
(
i
)
else
:
return
tensor
.
get_double_element
(
i
)
return
tensor
.
_
get_double_element
(
i
)
def
__set_elem__
(
tensor
,
i
,
e
):
if
tensor_to_check_dtype
==
np
.
float32
:
tensor
.
set_float_element
(
i
,
e
)
tensor
.
_
set_float_element
(
i
,
e
)
else
:
tensor
.
set_double_element
(
i
,
e
)
tensor
.
_
set_double_element
(
i
,
e
)
# we only compute gradient of one element each time.
# we use a for loop to compute the gradient of every element.
...
...
@@ -107,7 +107,7 @@ def get_numeric_gradient(place,
__set_elem__
(
tensor_to_check
,
i
,
origin
)
gradient_flat
[
i
]
=
(
y_pos
-
y_neg
)
/
delta
/
2
return
gradient_flat
.
reshape
(
tensor_to_check
.
get_dims
())
return
gradient_flat
.
reshape
(
tensor_to_check
.
shape
())
class
OpTest
(
unittest
.
TestCase
):
...
...
@@ -125,7 +125,7 @@ class OpTest(unittest.TestCase):
@
classmethod
def
tearDownClass
(
cls
):
'''Restore random seeds'''
"""Restore random seeds"""
np
.
random
.
set_state
(
cls
.
_np_rand_state
)
random
.
setstate
(
cls
.
_py_rand_state
)
...
...
python/paddle/fluid/tests/unittests/test_tensor.py
浏览文件 @
b83ffda4
...
...
@@ -26,7 +26,7 @@ class TestTensor(unittest.TestCase):
tensor
=
var
.
get_tensor
()
tensor
.
set_dims
([
1000
,
784
])
tensor
.
alloc_int
(
place
)
tensor
.
_
alloc_int
(
place
)
tensor_array
=
numpy
.
array
(
tensor
)
self
.
assertEqual
((
1000
,
784
),
tensor_array
.
shape
)
tensor_array
[
3
,
9
]
=
1
...
...
@@ -45,7 +45,7 @@ class TestTensor(unittest.TestCase):
tensor
=
var
.
get_tensor
()
tensor
.
set_dims
([
1000
,
784
])
tensor
.
alloc_float
(
place
)
tensor
.
_
alloc_float
(
place
)
tensor_array
=
numpy
.
array
(
tensor
)
self
.
assertEqual
((
1000
,
784
),
tensor_array
.
shape
)
...
...
@@ -64,7 +64,7 @@ class TestTensor(unittest.TestCase):
lod_tensor
=
var_lod
.
get_tensor
()
lod_tensor
.
set_dims
([
4
,
4
,
6
])
lod_tensor
.
alloc_int
(
place
)
lod_tensor
.
_
alloc_int
(
place
)
array
=
numpy
.
array
(
lod_tensor
)
array
[
0
,
0
,
0
]
=
3
array
[
3
,
3
,
5
]
=
10
...
...
@@ -85,7 +85,7 @@ class TestTensor(unittest.TestCase):
lod_tensor
=
var_lod
.
get_tensor
()
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
alloc_float
(
place
)
lod_tensor
.
_
alloc_float
(
place
)
tensor_array
=
numpy
.
array
(
lod_tensor
)
self
.
assertEqual
((
5
,
2
,
3
,
4
),
tensor_array
.
shape
)
...
...
@@ -104,14 +104,13 @@ class TestTensor(unittest.TestCase):
self
.
assertListEqual
(
lod_py
,
lod
)
def
test_lod_tensor_init
(
self
):
scope
=
core
.
Scope
()
place
=
core
.
CPUPlace
()
lod_py
=
[[
2
,
1
],
[
1
,
2
,
2
]]
lod_tensor
=
core
.
LoDTensor
()
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
set_recursive_sequence_lengths
(
lod_py
)
lod_tensor
.
alloc_float
(
place
)
lod_tensor
.
_
alloc_float
(
place
)
tensor_array
=
numpy
.
array
(
lod_tensor
)
tensor_array
[
0
,
0
,
0
,
0
]
=
1.0
tensor_array
[
0
,
0
,
0
,
1
]
=
2.0
...
...
@@ -131,7 +130,7 @@ class TestTensor(unittest.TestCase):
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
set_recursive_sequence_lengths
(
lod_py
)
lod_tensor
.
alloc_float
(
place
)
lod_tensor
.
_
alloc_float
(
place
)
tensor_array
=
numpy
.
array
(
lod_tensor
)
tensor_array
[
0
,
0
,
0
,
0
]
=
1.0
tensor_array
[
0
,
0
,
0
,
1
]
=
2.0
...
...
@@ -150,14 +149,14 @@ class TestTensor(unittest.TestCase):
tensor
=
var
.
get_tensor
()
tensor
.
set_dims
([
0
,
1
])
tensor
.
alloc_float
(
place
)
tensor
.
_
alloc_float
(
place
)
tensor_array
=
numpy
.
array
(
tensor
)
self
.
assertEqual
((
0
,
1
),
tensor_array
.
shape
)
if
core
.
is_compiled_with_cuda
():
gpu_place
=
core
.
CUDAPlace
(
0
)
tensor
.
alloc_float
(
gpu_place
)
tensor
.
_
alloc_float
(
gpu_place
)
tensor_array
=
numpy
.
array
(
tensor
)
self
.
assertEqual
((
0
,
1
),
tensor_array
.
shape
)
...
...
python/paddle/fluid/tests/unittests/testsuite.py
浏览文件 @
b83ffda4
...
...
@@ -75,7 +75,7 @@ def set_input(scope, op, inputs, place):
if
isinstance
(
var
,
tuple
):
tensor
.
set_recursive_sequence_lengths
(
var
[
1
])
var
=
var
[
0
]
tensor
.
set_dims
(
var
.
shape
)
tensor
.
_
set_dims
(
var
.
shape
)
tensor
.
set
(
var
,
place
)
elif
isinstance
(
var
,
float
):
scope
.
find_var
(
var_name
).
set_float
(
var
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录