Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
b83ffda4
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
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) {
...
@@ -78,37 +78,37 @@ PYBIND11_PLUGIN(core) {
py
::
class_
<
Tensor
>
(
m
,
"Tensor"
,
py
::
buffer_protocol
())
py
::
class_
<
Tensor
>
(
m
,
"Tensor"
,
py
::
buffer_protocol
())
.
def_buffer
(
.
def_buffer
(
[](
Tensor
&
self
)
->
py
::
buffer_info
{
return
CastToPyBuffer
(
self
);
})
[](
Tensor
&
self
)
->
py
::
buffer_info
{
return
CastToPyBuffer
(
self
);
})
.
def
(
"get_dims"
,
.
def
(
"
_
get_dims"
,
[](
const
Tensor
&
self
)
{
return
vectorize
(
self
.
dims
());
})
[](
const
Tensor
&
self
)
{
return
vectorize
(
self
.
dims
());
})
.
def
(
"set_dims"
,
.
def
(
"
_
set_dims"
,
[](
Tensor
&
self
,
const
std
::
vector
<
int64_t
>
&
dim
)
{
[](
Tensor
&
self
,
const
std
::
vector
<
int64_t
>
&
dim
)
{
self
.
Resize
(
make_ddim
(
dim
));
self
.
Resize
(
make_ddim
(
dim
));
})
})
.
def
(
"set_layout"
,
.
def
(
"
_
set_layout"
,
[](
Tensor
&
self
,
const
std
::
string
&
layout
)
{
[](
Tensor
&
self
,
const
std
::
string
&
layout
)
{
self
.
set_layout
(
StringToDataLayout
(
layout
));
self
.
set_layout
(
StringToDataLayout
(
layout
));
})
})
.
def
(
"alloc_float"
,
.
def
(
"
_
alloc_float"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPlace
&
place
)
{
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPlace
&
place
)
{
self
.
mutable_data
<
float
>
(
place
);
self
.
mutable_data
<
float
>
(
place
);
})
})
.
def
(
"alloc_float"
,
.
def
(
"
_
alloc_float"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CPUPlace
&
place
)
{
[](
Tensor
&
self
,
paddle
::
platform
::
CPUPlace
&
place
)
{
self
.
mutable_data
<
float
>
(
place
);
self
.
mutable_data
<
float
>
(
place
);
})
})
.
def
(
"alloc_int"
,
.
def
(
"
_
alloc_int"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CPUPlace
&
place
)
{
[](
Tensor
&
self
,
paddle
::
platform
::
CPUPlace
&
place
)
{
self
.
mutable_data
<
int
>
(
place
);
self
.
mutable_data
<
int
>
(
place
);
})
})
.
def
(
"alloc_int"
,
.
def
(
"
_
alloc_int"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPlace
&
place
)
{
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPlace
&
place
)
{
self
.
mutable_data
<
int
>
(
place
);
self
.
mutable_data
<
int
>
(
place
);
})
})
.
def
(
"alloc_int"
,
.
def
(
"
_
alloc_int"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPinnedPlace
&
place
)
{
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPinnedPlace
&
place
)
{
self
.
mutable_data
<
int
>
(
place
);
self
.
mutable_data
<
int
>
(
place
);
})
})
.
def
(
"alloc_float"
,
.
def
(
"
_
alloc_float"
,
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPinnedPlace
&
place
)
{
[](
Tensor
&
self
,
paddle
::
platform
::
CUDAPinnedPlace
&
place
)
{
self
.
mutable_data
<
float
>
(
place
);
self
.
mutable_data
<
float
>
(
place
);
})
})
...
@@ -136,11 +136,11 @@ PYBIND11_PLUGIN(core) {
...
@@ -136,11 +136,11 @@ PYBIND11_PLUGIN(core) {
.
def
(
"set"
,
PyCUDAPinnedTensorSetFromArray
<
uint8_t
>
)
.
def
(
"set"
,
PyCUDAPinnedTensorSetFromArray
<
uint8_t
>
)
#endif
#endif
.
def
(
"shape"
,
[](
Tensor
&
self
)
{
return
vectorize
(
self
.
dims
());
})
.
def
(
"shape"
,
[](
Tensor
&
self
)
{
return
vectorize
(
self
.
dims
());
})
.
def
(
"set_float_element"
,
TensorSetElement
<
float
>
)
.
def
(
"
_
set_float_element"
,
TensorSetElement
<
float
>
)
.
def
(
"get_float_element"
,
TensorGetElement
<
float
>
)
.
def
(
"
_
get_float_element"
,
TensorGetElement
<
float
>
)
.
def
(
"set_double_element"
,
TensorSetElement
<
double
>
)
.
def
(
"
_
set_double_element"
,
TensorSetElement
<
double
>
)
.
def
(
"get_double_element"
,
TensorGetElement
<
double
>
)
.
def
(
"
_
get_double_element"
,
TensorGetElement
<
double
>
)
.
def
(
"dtype"
,
[](
Tensor
&
self
)
{
return
ToDataType
(
self
.
type
());
});
.
def
(
"
_
dtype"
,
[](
Tensor
&
self
)
{
return
ToDataType
(
self
.
type
());
});
py
::
class_
<
LoDTensor
,
Tensor
>
(
m
,
"LoDTensor"
)
py
::
class_
<
LoDTensor
,
Tensor
>
(
m
,
"LoDTensor"
)
.
def_buffer
(
.
def_buffer
(
...
...
python/paddle/fluid/tests/unittests/op_test.py
浏览文件 @
b83ffda4
...
@@ -60,8 +60,8 @@ def get_numeric_gradient(place,
...
@@ -60,8 +60,8 @@ def get_numeric_gradient(place,
return
np
.
array
(
sum
).
mean
()
return
np
.
array
(
sum
).
mean
()
tensor_to_check
=
scope
.
find_var
(
input_to_check
).
get_tensor
()
tensor_to_check
=
scope
.
find_var
(
input_to_check
).
get_tensor
()
tensor_size
=
product
(
tensor_to_check
.
get_dims
())
tensor_size
=
product
(
tensor_to_check
.
shape
())
tensor_to_check_dtype
=
tensor_to_check
.
dtype
()
tensor_to_check_dtype
=
tensor_to_check
.
_
dtype
()
if
tensor_to_check_dtype
==
core
.
VarDesc
.
VarType
.
FP32
:
if
tensor_to_check_dtype
==
core
.
VarDesc
.
VarType
.
FP32
:
tensor_to_check_dtype
=
np
.
float32
tensor_to_check_dtype
=
np
.
float32
elif
tensor_to_check_dtype
==
core
.
VarDesc
.
VarType
.
FP64
:
elif
tensor_to_check_dtype
==
core
.
VarDesc
.
VarType
.
FP64
:
...
@@ -74,15 +74,15 @@ def get_numeric_gradient(place,
...
@@ -74,15 +74,15 @@ def get_numeric_gradient(place,
def
__get_elem__
(
tensor
,
i
):
def
__get_elem__
(
tensor
,
i
):
if
tensor_to_check_dtype
==
np
.
float32
:
if
tensor_to_check_dtype
==
np
.
float32
:
return
tensor
.
get_float_element
(
i
)
return
tensor
.
_
get_float_element
(
i
)
else
:
else
:
return
tensor
.
get_double_element
(
i
)
return
tensor
.
_
get_double_element
(
i
)
def
__set_elem__
(
tensor
,
i
,
e
):
def
__set_elem__
(
tensor
,
i
,
e
):
if
tensor_to_check_dtype
==
np
.
float32
:
if
tensor_to_check_dtype
==
np
.
float32
:
tensor
.
set_float_element
(
i
,
e
)
tensor
.
_
set_float_element
(
i
,
e
)
else
:
else
:
tensor
.
set_double_element
(
i
,
e
)
tensor
.
_
set_double_element
(
i
,
e
)
# we only compute gradient of one element each time.
# we only compute gradient of one element each time.
# we use a for loop to compute the gradient of every element.
# we use a for loop to compute the gradient of every element.
...
@@ -107,7 +107,7 @@ def get_numeric_gradient(place,
...
@@ -107,7 +107,7 @@ def get_numeric_gradient(place,
__set_elem__
(
tensor_to_check
,
i
,
origin
)
__set_elem__
(
tensor_to_check
,
i
,
origin
)
gradient_flat
[
i
]
=
(
y_pos
-
y_neg
)
/
delta
/
2
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
):
class
OpTest
(
unittest
.
TestCase
):
...
@@ -125,7 +125,7 @@ class OpTest(unittest.TestCase):
...
@@ -125,7 +125,7 @@ class OpTest(unittest.TestCase):
@
classmethod
@
classmethod
def
tearDownClass
(
cls
):
def
tearDownClass
(
cls
):
'''Restore random seeds'''
"""Restore random seeds"""
np
.
random
.
set_state
(
cls
.
_np_rand_state
)
np
.
random
.
set_state
(
cls
.
_np_rand_state
)
random
.
setstate
(
cls
.
_py_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):
...
@@ -26,7 +26,7 @@ class TestTensor(unittest.TestCase):
tensor
=
var
.
get_tensor
()
tensor
=
var
.
get_tensor
()
tensor
.
set_dims
([
1000
,
784
])
tensor
.
set_dims
([
1000
,
784
])
tensor
.
alloc_int
(
place
)
tensor
.
_
alloc_int
(
place
)
tensor_array
=
numpy
.
array
(
tensor
)
tensor_array
=
numpy
.
array
(
tensor
)
self
.
assertEqual
((
1000
,
784
),
tensor_array
.
shape
)
self
.
assertEqual
((
1000
,
784
),
tensor_array
.
shape
)
tensor_array
[
3
,
9
]
=
1
tensor_array
[
3
,
9
]
=
1
...
@@ -45,7 +45,7 @@ class TestTensor(unittest.TestCase):
...
@@ -45,7 +45,7 @@ class TestTensor(unittest.TestCase):
tensor
=
var
.
get_tensor
()
tensor
=
var
.
get_tensor
()
tensor
.
set_dims
([
1000
,
784
])
tensor
.
set_dims
([
1000
,
784
])
tensor
.
alloc_float
(
place
)
tensor
.
_
alloc_float
(
place
)
tensor_array
=
numpy
.
array
(
tensor
)
tensor_array
=
numpy
.
array
(
tensor
)
self
.
assertEqual
((
1000
,
784
),
tensor_array
.
shape
)
self
.
assertEqual
((
1000
,
784
),
tensor_array
.
shape
)
...
@@ -64,7 +64,7 @@ class TestTensor(unittest.TestCase):
...
@@ -64,7 +64,7 @@ class TestTensor(unittest.TestCase):
lod_tensor
=
var_lod
.
get_tensor
()
lod_tensor
=
var_lod
.
get_tensor
()
lod_tensor
.
set_dims
([
4
,
4
,
6
])
lod_tensor
.
set_dims
([
4
,
4
,
6
])
lod_tensor
.
alloc_int
(
place
)
lod_tensor
.
_
alloc_int
(
place
)
array
=
numpy
.
array
(
lod_tensor
)
array
=
numpy
.
array
(
lod_tensor
)
array
[
0
,
0
,
0
]
=
3
array
[
0
,
0
,
0
]
=
3
array
[
3
,
3
,
5
]
=
10
array
[
3
,
3
,
5
]
=
10
...
@@ -85,7 +85,7 @@ class TestTensor(unittest.TestCase):
...
@@ -85,7 +85,7 @@ class TestTensor(unittest.TestCase):
lod_tensor
=
var_lod
.
get_tensor
()
lod_tensor
=
var_lod
.
get_tensor
()
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
alloc_float
(
place
)
lod_tensor
.
_
alloc_float
(
place
)
tensor_array
=
numpy
.
array
(
lod_tensor
)
tensor_array
=
numpy
.
array
(
lod_tensor
)
self
.
assertEqual
((
5
,
2
,
3
,
4
),
tensor_array
.
shape
)
self
.
assertEqual
((
5
,
2
,
3
,
4
),
tensor_array
.
shape
)
...
@@ -104,14 +104,13 @@ class TestTensor(unittest.TestCase):
...
@@ -104,14 +104,13 @@ class TestTensor(unittest.TestCase):
self
.
assertListEqual
(
lod_py
,
lod
)
self
.
assertListEqual
(
lod_py
,
lod
)
def
test_lod_tensor_init
(
self
):
def
test_lod_tensor_init
(
self
):
scope
=
core
.
Scope
()
place
=
core
.
CPUPlace
()
place
=
core
.
CPUPlace
()
lod_py
=
[[
2
,
1
],
[
1
,
2
,
2
]]
lod_py
=
[[
2
,
1
],
[
1
,
2
,
2
]]
lod_tensor
=
core
.
LoDTensor
()
lod_tensor
=
core
.
LoDTensor
()
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
set_recursive_sequence_lengths
(
lod_py
)
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
=
numpy
.
array
(
lod_tensor
)
tensor_array
[
0
,
0
,
0
,
0
]
=
1.0
tensor_array
[
0
,
0
,
0
,
0
]
=
1.0
tensor_array
[
0
,
0
,
0
,
1
]
=
2.0
tensor_array
[
0
,
0
,
0
,
1
]
=
2.0
...
@@ -131,7 +130,7 @@ class TestTensor(unittest.TestCase):
...
@@ -131,7 +130,7 @@ class TestTensor(unittest.TestCase):
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
set_dims
([
5
,
2
,
3
,
4
])
lod_tensor
.
set_recursive_sequence_lengths
(
lod_py
)
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
=
numpy
.
array
(
lod_tensor
)
tensor_array
[
0
,
0
,
0
,
0
]
=
1.0
tensor_array
[
0
,
0
,
0
,
0
]
=
1.0
tensor_array
[
0
,
0
,
0
,
1
]
=
2.0
tensor_array
[
0
,
0
,
0
,
1
]
=
2.0
...
@@ -150,14 +149,14 @@ class TestTensor(unittest.TestCase):
...
@@ -150,14 +149,14 @@ class TestTensor(unittest.TestCase):
tensor
=
var
.
get_tensor
()
tensor
=
var
.
get_tensor
()
tensor
.
set_dims
([
0
,
1
])
tensor
.
set_dims
([
0
,
1
])
tensor
.
alloc_float
(
place
)
tensor
.
_
alloc_float
(
place
)
tensor_array
=
numpy
.
array
(
tensor
)
tensor_array
=
numpy
.
array
(
tensor
)
self
.
assertEqual
((
0
,
1
),
tensor_array
.
shape
)
self
.
assertEqual
((
0
,
1
),
tensor_array
.
shape
)
if
core
.
is_compiled_with_cuda
():
if
core
.
is_compiled_with_cuda
():
gpu_place
=
core
.
CUDAPlace
(
0
)
gpu_place
=
core
.
CUDAPlace
(
0
)
tensor
.
alloc_float
(
gpu_place
)
tensor
.
_
alloc_float
(
gpu_place
)
tensor_array
=
numpy
.
array
(
tensor
)
tensor_array
=
numpy
.
array
(
tensor
)
self
.
assertEqual
((
0
,
1
),
tensor_array
.
shape
)
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):
...
@@ -75,7 +75,7 @@ def set_input(scope, op, inputs, place):
if
isinstance
(
var
,
tuple
):
if
isinstance
(
var
,
tuple
):
tensor
.
set_recursive_sequence_lengths
(
var
[
1
])
tensor
.
set_recursive_sequence_lengths
(
var
[
1
])
var
=
var
[
0
]
var
=
var
[
0
]
tensor
.
set_dims
(
var
.
shape
)
tensor
.
_
set_dims
(
var
.
shape
)
tensor
.
set
(
var
,
place
)
tensor
.
set
(
var
,
place
)
elif
isinstance
(
var
,
float
):
elif
isinstance
(
var
,
float
):
scope
.
find_var
(
var_name
).
set_float
(
var
)
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录