Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sven357
Paddle
提交
f50e36e2
P
Paddle
项目概览
sven357
/
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,发现更多精彩内容 >>
提交
f50e36e2
编写于
9月 08, 2017
作者:
Q
qijun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
follow comments
上级
a22606ca
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
67 addition
and
79 deletion
+67
-79
paddle/framework/operator.cc
paddle/framework/operator.cc
+9
-0
paddle/framework/operator.h
paddle/framework/operator.h
+2
-20
paddle/operators/sum_op.cc
paddle/operators/sum_op.cc
+7
-3
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+3
-8
python/paddle/v2/framework/tests/op_test.py
python/paddle/v2/framework/tests/op_test.py
+41
-37
python/paddle/v2/framework/tests/test_cross_entropy_op.py
python/paddle/v2/framework/tests/test_cross_entropy_op.py
+3
-5
python/paddle/v2/framework/tests/test_sigmoid_op.py
python/paddle/v2/framework/tests/test_sigmoid_op.py
+1
-3
python/paddle/v2/framework/tests/test_sum_op.py
python/paddle/v2/framework/tests/test_sum_op.py
+1
-3
未找到文件。
paddle/framework/operator.cc
浏览文件 @
f50e36e2
...
...
@@ -123,6 +123,15 @@ OperatorBase::OperatorBase(const std::string& type,
CheckAllInputOutputSet
();
}
std
::
vector
<
std
::
string
>
OperatorBase
::
InputVars
()
const
{
std
::
vector
<
std
::
string
>
ret_val
;
for
(
auto
&
o
:
outputs_
)
{
ret_val
.
reserve
(
ret_val
.
size
()
+
o
.
second
.
size
());
ret_val
.
insert
(
ret_val
.
end
(),
o
.
second
.
begin
(),
o
.
second
.
end
());
}
return
ret_val
;
}
std
::
vector
<
std
::
string
>
OperatorBase
::
OutputVars
(
bool
has_intermediate
)
const
{
std
::
vector
<
std
::
string
>
ret_val
;
if
(
has_intermediate
)
{
...
...
paddle/framework/operator.h
浏览文件 @
f50e36e2
...
...
@@ -95,31 +95,13 @@ class OperatorBase {
const
VariableNameMap
&
Inputs
()
const
{
return
inputs_
;
}
const
VariableNameMap
&
Outputs
()
const
{
return
outputs_
;
}
const
std
::
vector
<
std
::
string
>
InputsNames
()
const
{
std
::
vector
<
std
::
string
>
result
;
for
(
auto
&
kv
:
inputs_
)
{
for
(
auto
&
name
:
kv
.
second
)
{
result
.
push_back
(
name
);
}
}
return
result
;
}
const
std
::
vector
<
std
::
string
>
OutputsNames
()
const
{
std
::
vector
<
std
::
string
>
result
;
for
(
auto
&
kv
:
outputs_
)
{
for
(
auto
&
name
:
kv
.
second
)
{
result
.
push_back
(
name
);
}
}
return
result
;
}
//! Get a input with argument's name described in `op_proto`
std
::
string
Input
(
const
std
::
string
&
name
)
const
;
//! Get a input which has multiple variables.
const
std
::
vector
<
std
::
string
>&
Inputs
(
const
std
::
string
&
name
)
const
;
std
::
vector
<
std
::
string
>
InputVars
()
const
;
//! Get a output with argument's name described in `op_proto`
std
::
string
Output
(
const
std
::
string
&
name
)
const
;
//! Get an output which has multiple variables.
...
...
paddle/operators/sum_op.cc
浏览文件 @
f50e36e2
...
...
@@ -26,10 +26,14 @@ class SumOp : public framework::OperatorWithKernel {
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
int
N
=
ins
.
size
();
PADDLE_ENFORCE_GT
(
N
,
1
,
"Input tensors count should > 1."
);
auto
in_dim
=
ins
[
0
]
->
dims
(
);
auto
dim_zero
=
ins
[
0
]
->
dims
();
out
->
Resize
(
dim_zero
);
PADDLE_ENFORCE_GT
(
N
,
1
,
"Input tensors count should > 1."
);
for
(
int
i
=
1
;
i
<
N
;
i
++
)
{
auto
dim
=
ins
[
i
]
->
dims
();
PADDLE_ENFORCE
(
in_dim
==
dim
,
"Input tensors must have same shape"
);
}
out
->
Resize
(
in_dim
);
}
};
...
...
paddle/pybind/pybind.cc
浏览文件 @
f50e36e2
...
...
@@ -214,15 +214,10 @@ All parameter, weight, gradient are variables in Paddle.
->
std
::
map
<
std
::
string
,
std
::
vector
<
std
::
string
>>
{
return
op
.
Outputs
();
})
.
def
(
"outputs_names"
,
[](
const
OperatorBase
&
op
)
->
std
::
vector
<
std
::
string
>
{
return
op
.
OutputsNames
();
})
.
def
(
"output_vars"
,
[](
const
OperatorBase
&
op
)
{
return
op
.
OutputVars
(
true
);
})
.
def
(
"inputs"
,
[](
const
OperatorBase
&
op
)
{
return
op
.
Inputs
();
})
.
def
(
"inputs_names"
,
[](
const
OperatorBase
&
op
)
->
std
::
vector
<
std
::
string
>
{
return
op
.
InputsNames
();
})
.
def
(
"input_vars"
,
[](
const
OperatorBase
&
op
)
{
return
op
.
InputVars
();
})
.
def
(
"__str__"
,
&
OperatorBase
::
DebugString
)
.
def
(
"no_intermediate_outputs"
,
[](
const
OperatorBase
&
op
)
{
return
op
.
OutputVars
(
false
);
})
...
...
python/paddle/v2/framework/tests/op_test.py
浏览文件 @
f50e36e2
...
...
@@ -9,54 +9,40 @@ def grad_var_name(var_name):
return
var_name
+
"@GRAD"
def
remove_grad_var_name
(
var_name
):
return
var_name
[
0
:
-
5
]
def
create_op
(
scope
,
op_type
,
inputs
,
outputs
,
attrs
=
None
):
kwargs
=
dict
()
for
ins
in
Operator
.
get_op_inputs
(
op_type
):
in_name
=
ins
[
0
]
in_dup
=
ins
[
1
]
for
in_name
,
in_dup
in
Operator
.
get_op_inputs
(
op_type
):
if
in_name
in
inputs
:
kwargs
[
in_name
]
=
[]
if
in_dup
:
sub_in
=
inputs
[
in_name
]
for
sub_in_name
in
sub_in
:
var
=
scope
.
new_var
(
sub_in_name
)
tensor
=
var
.
get_tensor
()
kwargs
[
in_name
].
append
(
sub_in_name
)
else
:
var
=
scope
.
new_var
(
in_name
)
tensor
=
var
.
get_tensor
()
kwargs
[
in_name
].
append
(
in_name
)
for
outs
in
Operator
.
get_op_outputs
(
op_type
):
out_name
=
outs
[
0
]
out_dup
=
outs
[
1
]
for
out_name
,
out_dup
in
Operator
.
get_op_outputs
(
op_type
):
if
out_name
in
outputs
:
kwargs
[
out_name
]
=
[]
if
out_dup
:
sub_in
=
outputs
[
out_name
]
for
sun_in_name
in
sub_in
:
var
=
scope
.
new_var
(
sun_in_name
)
tensor
=
var
.
get_tensor
()
kwargs
[
out_name
].
append
(
sun_in_name
)
else
:
var
=
scope
.
new_var
(
out_name
)
tensor
=
var
.
get_tensor
()
kwargs
[
out_name
].
append
(
out_name
)
#
for attr_name in Operator.get_op_attr_names(op_type):
#
kwargs[attr_name] = attrs[attr_name]
for
attr_name
in
Operator
.
get_op_attr_names
(
op_type
):
kwargs
[
attr_name
]
=
attrs
[
attr_name
]
return
Operator
(
op_type
,
**
kwargs
)
def
set_input
(
scope
,
op
,
inputs
,
place
):
for
ins
in
Operator
.
get_op_inputs
(
op
.
type
()):
in_name
=
ins
[
0
]
in_dup
=
ins
[
1
]
for
in_name
,
in_dup
in
Operator
.
get_op_inputs
(
op
.
type
()):
if
in_name
in
inputs
:
if
in_dup
:
sub_in
=
inputs
[
in_name
]
...
...
@@ -75,9 +61,7 @@ def set_input(scope, op, inputs, place):
def
set_output_grad
(
scope
,
op
,
outputs
,
place
):
for
outs
in
Operator
.
get_op_outputs
(
op
.
type
()):
out_name
=
outs
[
0
]
out_dup
=
outs
[
1
]
for
out_name
,
out_dup
in
Operator
.
get_op_outputs
(
op
.
type
()):
if
out_name
in
outputs
:
if
out_dup
:
sub_out
=
outputs
[
out_name
]
...
...
@@ -150,10 +134,10 @@ def get_numeric_gradient(scope,
def
get_backward_op
(
scope
,
op
,
no_grad_set
):
backward_op
=
core
.
Operator
.
backward
(
op
,
no_grad_set
)
for
input
in
backward_op
.
input
s_name
s
():
for
input
in
backward_op
.
input
_var
s
():
var
=
scope
.
new_var
(
input
)
var
.
get_tensor
()
for
output
in
backward_op
.
output
s_name
s
():
for
output
in
backward_op
.
output
_var
s
():
var
=
scope
.
new_var
(
output
)
var
.
get_tensor
()
return
backward_op
...
...
@@ -182,7 +166,7 @@ def get_gradient(scope, op, inputs, outputs, grad_name, place,
class
OpTest
(
unittest
.
TestCase
):
def
check_output
(
self
,
place
):
def
check_output
_with_place
(
self
,
place
):
self
.
scope
=
core
.
Scope
()
self
.
op
=
create_op
(
self
.
scope
,
self
.
op_type
,
self
.
inputs
,
self
.
outputs
)
if
isinstance
(
place
,
core
.
GPUPlace
)
and
not
self
.
op
.
support_gpu
():
...
...
@@ -192,9 +176,7 @@ class OpTest(unittest.TestCase):
ctx
=
core
.
DeviceContext
.
create
(
place
)
self
.
op
.
run
(
self
.
scope
,
ctx
)
for
outs
in
Operator
.
get_op_outputs
(
self
.
op
.
type
()):
out_name
=
outs
[
0
]
out_dup
=
outs
[
1
]
for
out_name
,
out_dup
in
Operator
.
get_op_outputs
(
self
.
op
.
type
()):
if
out_dup
:
sub_out
=
self
.
outputs
[
out_name
]
for
sub_out_name
in
sub_out
:
...
...
@@ -213,6 +195,13 @@ class OpTest(unittest.TestCase):
actual
,
expect
,
atol
=
1e-05
),
"output name: "
+
out_name
+
"has diff"
)
def
check_output
(
self
):
places
=
[
core
.
CPUPlace
()]
if
core
.
is_compile_gpu
()
and
self
.
op
.
support_gpu
():
places
.
append
(
core
.
GPUPlace
(
0
))
for
place
in
places
:
self
.
check_output_with_place
(
place
)
def
__assert_is_close
(
self
,
numeric_grads
,
analytic_grads
,
names
,
max_relative_error
,
msg_prefix
):
...
...
@@ -255,17 +244,32 @@ class OpTest(unittest.TestCase):
grad_var_name
(
input_to_check
)
for
input_to_check
in
inputs_to_check
]
places
=
[
core
.
CPUPlace
()]
if
core
.
is_compile_gpu
()
and
self
.
op
.
support_gpu
():
places
.
append
(
core
.
GPUPlace
(
0
))
cpu_place
=
core
.
CPUPlace
()
cpu_analytic_grads
=
[
get_gradient
(
self
.
scope
,
self
.
op
,
self
.
inputs
,
self
.
outputs
,
grad_name
,
cpu_place
,
no_grad_set
)
for
grad_name
in
grad_names
]
for
place
in
places
:
analytic_grads
=
[
self
.
__assert_is_close
(
numeric_grads
,
cpu_analytic_grads
,
grad_names
,
max_relative_error
,
"Gradient Check On %s"
%
str
(
cpu_place
))
if
core
.
is_compile_gpu
()
and
self
.
op
.
support_gpu
():
gpu_place
=
core
.
GPUPlace
(
0
)
gpu_analytic_grads
=
[
get_gradient
(
self
.
scope
,
self
.
op
,
self
.
inputs
,
self
.
outputs
,
grad_name
,
place
,
no_grad_set
)
grad_name
,
gpu_
place
,
no_grad_set
)
for
grad_name
in
grad_names
]
self
.
__assert_is_close
(
numeric_grads
,
analytic_grads
,
grad_names
,
max_relative_error
,
"Gradient Check On %s"
%
str
(
place
))
self
.
__assert_is_close
(
numeric_grads
,
gpu_analytic_grads
,
grad_names
,
max_relative_error
,
"Gradient Check On %s"
%
str
(
gpu_place
))
for
c_grad
,
g_grad
,
name
in
itertools
.
izip
(
cpu_analytic_grads
,
gpu_analytic_grads
,
grad_names
):
self
.
assertTrue
(
numpy
.
allclose
(
c_grad
,
g_grad
,
atol
=
1e-4
),
"output name: "
+
name
+
" has diff"
)
python/paddle/v2/framework/tests/test_cross_entropy_op.py
浏览文件 @
f50e36e2
import
unittest
import
numpy
from
op_test
import
OpTest
import
paddle.v2.framework.core
as
core
class
TestCrossEntropy
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"onehot_cross_entropy"
batch_size
=
4
class_num
=
4
batch_size
=
30
class_num
=
10
X
=
numpy
.
random
.
random
((
batch_size
,
class_num
)).
astype
(
"float32"
)
label
=
(
class_num
/
2
)
*
numpy
.
ones
(
batch_size
).
astype
(
"int32"
)
self
.
inputs
=
{
'X'
:
X
,
'label'
:
label
}
...
...
@@ -18,8 +17,7 @@ class TestCrossEntropy(OpTest):
self
.
outputs
=
{
'Y'
:
numpy
.
array
(
Y
).
astype
(
"float32"
)}
def
test_check_output
(
self
):
self
.
check_output
(
core
.
CPUPlace
())
self
.
check_output
(
core
.
GPUPlace
(
0
))
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
"X"
],
"Y"
)
...
...
python/paddle/v2/framework/tests/test_sigmoid_op.py
浏览文件 @
f50e36e2
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
paddle.v2.framework.core
as
core
class
TestSigmoid
(
OpTest
):
...
...
@@ -13,8 +12,7 @@ class TestSigmoid(OpTest):
self
.
outputs
=
{
'Y'
:
1
/
(
1
+
np
.
exp
(
-
self
.
inputs
[
'X'
]))}
def
test_check_output
(
self
):
self
.
check_output
(
core
.
CPUPlace
())
self
.
check_output
(
core
.
GPUPlace
(
0
))
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
"X"
],
"Y"
,
max_relative_error
=
0.007
)
...
...
python/paddle/v2/framework/tests/test_sum_op.py
浏览文件 @
f50e36e2
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
paddle.v2.framework.core
as
core
class
TestSumOp
(
OpTest
):
...
...
@@ -15,8 +14,7 @@ class TestSumOp(OpTest):
self
.
outputs
=
{
'Out'
:
y
}
def
test_check_output
(
self
):
self
.
check_output
(
core
.
CPUPlace
())
self
.
check_output
(
core
.
GPUPlace
(
0
))
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
"x0"
],
"Out"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录