Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
610a2584
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
610a2584
编写于
8月 12, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix all unit tests in Python
上级
d6d46412
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
54 addition
and
40 deletion
+54
-40
paddle/framework/pybind.cc
paddle/framework/pybind.cc
+6
-1
python/paddle/v2/framework/tests/gradient_checker.py
python/paddle/v2/framework/tests/gradient_checker.py
+21
-13
python/paddle/v2/framework/tests/test_cross_entropy_op.py
python/paddle/v2/framework/tests/test_cross_entropy_op.py
+12
-11
python/paddle/v2/framework/tests/test_net.py
python/paddle/v2/framework/tests/test_net.py
+6
-6
python/paddle/v2/framework/tests/test_protobuf.py
python/paddle/v2/framework/tests/test_protobuf.py
+3
-4
python/paddle/v2/framework/tests/test_softmax_op.py
python/paddle/v2/framework/tests/test_softmax_op.py
+6
-5
未找到文件。
paddle/framework/pybind.cc
浏览文件 @
610a2584
...
@@ -60,7 +60,12 @@ void ExposeOperator(ClassType &m) {
...
@@ -60,7 +60,12 @@ void ExposeOperator(ClassType &m) {
->
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
std
::
string
>>
{
->
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
std
::
string
>>
{
return
op
.
outputs_
;
return
op
.
outputs_
;
})
})
.
def
(
"__str__"
,
&
ClassType
::
type
::
DebugString
);
.
def
(
"inputs"
,
[](
const
typename
ClassType
::
type
&
op
)
{
return
op
.
inputs_
;
})
.
def
(
"__str__"
,
&
ClassType
::
type
::
DebugString
)
.
def
(
"no_intermediate_outputs"
,
[](
const
typename
ClassType
::
type
&
op
)
{
return
op
.
OutputVars
(
false
);
});
}
}
static
size_t
UniqueIntegerGenerator
()
{
static
size_t
UniqueIntegerGenerator
()
{
...
...
python/paddle/v2/framework/tests/gradient_checker.py
浏览文件 @
610a2584
...
@@ -53,15 +53,18 @@ def get_numeric_gradient(op,
...
@@ -53,15 +53,18 @@ def get_numeric_gradient(op,
tensor
.
set
(
input_values
[
var_name
],
core
.
CPUPlace
())
tensor
.
set
(
input_values
[
var_name
],
core
.
CPUPlace
())
# Create all output variable in local_scope
# Create all output variable in local_scope
for
output
in
op
.
outputs
():
opts
=
op
.
outputs
()
for
key
in
opts
:
for
output
in
opts
[
key
]:
if
local_scope
.
find_var
(
output
)
is
None
:
if
local_scope
.
find_var
(
output
)
is
None
:
local_scope
.
new_var
(
output
).
get_tensor
()
local_scope
.
new_var
(
output
).
get_tensor
()
op
.
infer_shape
(
local_scope
)
op
.
infer_shape
(
local_scope
)
# allocate output memory
# allocate output memory
for
output
in
op
.
outputs
():
for
key
in
opts
:
local_scope
.
find_var
(
output
).
get_tensor
().
alloc_float
(
core
.
CPUPlace
())
for
output
in
opts
[
key
]:
local_scope
.
find_var
(
output
).
get_tensor
().
alloc_float
(
core
.
CPUPlace
(
))
# TODO(yuyang18): Only CPU is support now.
# TODO(yuyang18): Only CPU is support now.
cpu_ctx
=
core
.
DeviceContext
.
create
(
core
.
CPUPlace
())
cpu_ctx
=
core
.
DeviceContext
.
create
(
core
.
CPUPlace
())
...
@@ -150,19 +153,24 @@ class GradientChecker(unittest.TestCase):
...
@@ -150,19 +153,24 @@ class GradientChecker(unittest.TestCase):
if
no_grad_set
is
None
:
if
no_grad_set
is
None
:
no_grad_set
=
set
()
no_grad_set
=
set
()
tmp_outs
=
forward_op
.
temp_outputs
()
no_tmp_out
=
forward_op
.
no_intermediate_outputs
()
no_tmp_out
=
filter
(
lambda
name
:
name
not
in
tmp_outs
,
forward_op
.
outputs
())
if
len
(
no_tmp_out
)
!=
1
:
if
len
(
no_tmp_out
)
!=
1
:
raise
ValueError
(
"non temp out_names should be 1"
)
raise
ValueError
(
"non temp out_names should be 1"
)
in_names
=
forward_op
.
inputs
()
inputs
=
forward_op
.
inputs
()
in_names
=
[
item
for
k
in
inputs
for
item
in
inputs
[
k
]]
outputs
=
forward_op
.
outputs
()
out_names
=
[
item
for
k
in
outputs
for
item
in
outputs
[
k
]]
for
no_grad
in
no_grad_set
:
for
no_grad
in
no_grad_set
:
if
no_grad
not
in
in_names
:
if
no_grad
not
in
in_names
:
raise
ValueError
(
"no_grad should be in in_names"
)
raise
ValueError
(
"no_grad should be in in_names"
)
backward_op
=
core
.
Operator
.
backward
(
forward_op
,
no_grad_set
)
backward_op
=
core
.
Operator
.
backward
(
forward_op
,
no_grad_set
)
bwd_outputs
=
backward_op
.
outputs
()
bwd_out_names
=
[
item
for
k
in
bwd_outputs
for
item
in
bwd_outputs
[
k
]]
places
=
[
core
.
CPUPlace
()]
places
=
[
core
.
CPUPlace
()]
if
not
only_cpu
and
core
.
is_compile_gpu
()
and
backward_op
.
support_gpu
():
if
not
only_cpu
and
core
.
is_compile_gpu
()
and
backward_op
.
support_gpu
():
places
.
append
(
core
.
GPUPlace
(
0
))
places
.
append
(
core
.
GPUPlace
(
0
))
...
@@ -188,7 +196,7 @@ class GradientChecker(unittest.TestCase):
...
@@ -188,7 +196,7 @@ class GradientChecker(unittest.TestCase):
var
.
set
(
value
,
place
)
var
.
set
(
value
,
place
)
# create output var
# create output var
for
out_name
in
forward_op
.
outputs
()
:
for
out_name
in
out_names
:
scope
.
new_var
(
out_name
).
get_tensor
()
scope
.
new_var
(
out_name
).
get_tensor
()
# infer the shape of output var and compute/set value of output var
# infer the shape of output var and compute/set value of output var
...
@@ -198,7 +206,7 @@ class GradientChecker(unittest.TestCase):
...
@@ -198,7 +206,7 @@ class GradientChecker(unittest.TestCase):
# create output grad var
# create output grad var
# set shape as the output var
# set shape as the output var
# set value of this grad to ones
# set value of this grad to ones
for
name
in
forward_op
.
outputs
()
:
for
name
in
out_names
:
out_tensor
=
scope
.
find_var
(
name
).
get_tensor
()
out_tensor
=
scope
.
find_var
(
name
).
get_tensor
()
grad_tensor
=
scope
.
new_var
(
grad_var_name
(
name
)).
get_tensor
()
grad_tensor
=
scope
.
new_var
(
grad_var_name
(
name
)).
get_tensor
()
grad_tensor
.
set_dims
(
out_tensor
.
shape
())
grad_tensor
.
set_dims
(
out_tensor
.
shape
())
...
@@ -206,7 +214,7 @@ class GradientChecker(unittest.TestCase):
...
@@ -206,7 +214,7 @@ class GradientChecker(unittest.TestCase):
grad_tensor
.
set
(
data
,
place
)
grad_tensor
.
set
(
data
,
place
)
# create input grad var
# create input grad var
for
name
in
b
ackward_op
.
outputs
()
:
for
name
in
b
wd_out_names
:
scope
.
new_var
(
name
).
get_tensor
()
scope
.
new_var
(
name
).
get_tensor
()
# infer the shape of input gradient var and compute/set it's value
# infer the shape of input gradient var and compute/set it's value
...
...
python/paddle/v2/framework/tests/test_cross_entropy_op.py
浏览文件 @
610a2584
...
@@ -21,17 +21,18 @@ class TestCrossEntropy(unittest.TestCase):
...
@@ -21,17 +21,18 @@ class TestCrossEntropy(unittest.TestCase):
self
.
outputs
=
{
'Y'
:
numpy
.
array
(
Y
).
astype
(
"float32"
)}
self
.
outputs
=
{
'Y'
:
numpy
.
array
(
Y
).
astype
(
"float32"
)}
# class CrossEntropyGradOpTest(GradientChecker):
class
CrossEntropyGradOpTest
(
GradientChecker
):
# def test_softmax_grad(self):
def
test_softmax_grad
(
self
):
# op = create_op("onehot_cross_entropy")
op
=
create_op
(
"onehot_cross_entropy"
)
# batch_size = 100
batch_size
=
100
# class_num = 10
class_num
=
10
# inputs = {
inputs
=
{
# "X": numpy.random.uniform(
"X"
:
numpy
.
random
.
uniform
(
# 0.1, 1.0, [batch_size, class_num]).astype("float32"),
0.1
,
1.0
,
[
batch_size
,
class_num
]).
astype
(
"float32"
),
# "label": (class_num / 2) * numpy.ones(batch_size).astype("int32")
"label"
:
(
class_num
/
2
)
*
numpy
.
ones
(
batch_size
).
astype
(
"int32"
)
# }
}
# self.check_grad(op, inputs, set("X"), "Y")
self
.
check_grad
(
op
,
inputs
,
set
(
"X"
),
"Y"
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
python/paddle/v2/framework/tests/test_net.py
浏览文件 @
610a2584
...
@@ -25,12 +25,12 @@ class TestNet(unittest.TestCase):
...
@@ -25,12 +25,12 @@ class TestNet(unittest.TestCase):
net
.
complete_add_op
(
True
)
net
.
complete_add_op
(
True
)
expected
=
'''
expected
=
'''
Op(plain_net), inputs:
(W, X, Y), outputs:(Out, fc.out, pre_activation)
.
Op(plain_net), inputs:
{all[W, X, Y]}, outputs:{all[Out, fc.out, pre_activation]}
.
Op(add_two), inputs:
(X, Y), outputs:(Out)
.
Op(add_two), inputs:
{X[X], Y[Y]}, outputs:{Out[Out]}
.
Op(plain_net), inputs:
(W, X), outputs:(fc.out, pre_activation)
.
Op(plain_net), inputs:
{all[W, X]}, outputs:{all[fc.out, pre_activation]}
.
Op(plain_net), inputs:
(W, X), outputs:(fc.out, pre_activation)
.
Op(plain_net), inputs:
{all[W, X]}, outputs:{all[fc.out, pre_activation]}
.
Op(mul), inputs:
(X, W), outputs:(pre_activation)
.
Op(mul), inputs:
{X[X], Y[W]}, outputs:{Out[pre_activation]}
.
Op(sigmoid), inputs:
(pre_activation), outputs:(fc.out)
.
Op(sigmoid), inputs:
{X[pre_activation]}, outputs:{Y[fc.out]}
.
'''
'''
self
.
assertEqual
(
expected
,
"
\n
"
+
str
(
net
))
self
.
assertEqual
(
expected
,
"
\n
"
+
str
(
net
))
...
...
python/paddle/v2/framework/tests/test_protobuf.py
浏览文件 @
610a2584
import
paddle.v2.framework.proto.op_proto_pb2
as
op_proto_lib
import
paddle.v2.framework.proto.framework_pb2
as
framework_pb2
import
paddle.v2.framework.proto.attribute_pb2
as
attr_type_lib
import
unittest
import
unittest
class
TestFrameworkProto
(
unittest
.
TestCase
):
class
TestFrameworkProto
(
unittest
.
TestCase
):
def
test_all
(
self
):
def
test_all
(
self
):
op_proto
=
op_proto_lib
.
OpProto
()
op_proto
=
framework_pb2
.
OpProto
()
ipt0
=
op_proto
.
inputs
.
add
()
ipt0
=
op_proto
.
inputs
.
add
()
ipt0
.
name
=
"a"
ipt0
.
name
=
"a"
ipt0
.
comment
=
"the input of cosine op"
ipt0
.
comment
=
"the input of cosine op"
...
@@ -19,7 +18,7 @@ class TestFrameworkProto(unittest.TestCase):
...
@@ -19,7 +18,7 @@ class TestFrameworkProto(unittest.TestCase):
attr
=
op_proto
.
attrs
.
add
()
attr
=
op_proto
.
attrs
.
add
()
attr
.
name
=
"scale"
attr
.
name
=
"scale"
attr
.
comment
=
"scale of cosine op"
attr
.
comment
=
"scale of cosine op"
attr
.
type
=
attr_type_lib
.
FLOAT
attr
.
type
=
framework_pb2
.
FLOAT
op_proto
.
type
=
"cos"
op_proto
.
type
=
"cos"
self
.
assertTrue
(
op_proto
.
IsInitialized
())
self
.
assertTrue
(
op_proto
.
IsInitialized
())
...
...
python/paddle/v2/framework/tests/test_softmax_op.py
浏览文件 @
610a2584
...
@@ -24,11 +24,12 @@ class TestSoftmaxOp(unittest.TestCase):
...
@@ -24,11 +24,12 @@ class TestSoftmaxOp(unittest.TestCase):
}
}
# class SoftmaxGradOpTest(GradientChecker):
class
SoftmaxGradOpTest
(
GradientChecker
):
# def test_softmax(self):
def
test_softmax
(
self
):
# op = create_op("softmax")
op
=
create_op
(
"softmax"
)
# inputs = {"X": np.random.uniform(0.1, 1, [10, 10]).astype("float32")}
inputs
=
{
"X"
:
np
.
random
.
uniform
(
0.1
,
1
,
[
10
,
10
]).
astype
(
"float32"
)}
# self.check_grad(op, inputs, set("X"), "Y")
self
.
check_grad
(
op
,
inputs
,
set
(
"X"
),
"Y"
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录