Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
3cd10a7c
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
3cd10a7c
编写于
12月 20, 2018
作者:
M
minqiyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Conv2D forward
test=develop
上级
8d88c5a8
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
198 addition
and
234 deletion
+198
-234
paddle/fluid/imperative/layer.cc
paddle/fluid/imperative/layer.cc
+3
-0
paddle/fluid/imperative/layer.h
paddle/fluid/imperative/layer.h
+2
-1
paddle/fluid/imperative/tracer.h
paddle/fluid/imperative/tracer.h
+11
-32
paddle/fluid/pybind/imperative.cc
paddle/fluid/pybind/imperative.cc
+2
-3
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+6
-0
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+11
-4
python/paddle/fluid/imperative/__init__.py
python/paddle/fluid/imperative/__init__.py
+4
-0
python/paddle/fluid/imperative/base.py
python/paddle/fluid/imperative/base.py
+2
-3
python/paddle/fluid/imperative/layers.py
python/paddle/fluid/imperative/layers.py
+5
-8
python/paddle/fluid/initializer.py
python/paddle/fluid/initializer.py
+16
-8
python/paddle/fluid/layer_helper.py
python/paddle/fluid/layer_helper.py
+1
-1
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+6
-51
python/paddle/fluid/tests/unittests/test_imperative.py
python/paddle/fluid/tests/unittests/test_imperative.py
+0
-123
python/paddle/fluid/tests/unittests/test_imperative_mnist.py
python/paddle/fluid/tests/unittests/test_imperative_mnist.py
+129
-0
未找到文件。
paddle/fluid/imperative/layer.cc
浏览文件 @
3cd10a7c
...
@@ -144,6 +144,9 @@ void VarBase::ApplyGrad(framework::Scope* scope, Variable* grad) {
...
@@ -144,6 +144,9 @@ void VarBase::ApplyGrad(framework::Scope* scope, Variable* grad) {
std
::
vector
<
Variable
*>
OpBase
::
ApplyGrad
(
framework
::
Scope
*
scope
)
{
std
::
vector
<
Variable
*>
OpBase
::
ApplyGrad
(
framework
::
Scope
*
scope
)
{
VLOG
(
3
)
<<
"op grad "
<<
grad_op_desc_
->
Type
();
VLOG
(
3
)
<<
"op grad "
<<
grad_op_desc_
->
Type
();
if
(
!
grad_to_var_
)
{
return
{};
}
for
(
const
std
::
string
&
grad_invar
:
grad_op_desc_
->
InputArgumentNames
())
{
for
(
const
std
::
string
&
grad_invar
:
grad_op_desc_
->
InputArgumentNames
())
{
if
(
grad_to_var_
->
find
(
grad_invar
)
==
grad_to_var_
->
end
())
{
if
(
grad_to_var_
->
find
(
grad_invar
)
==
grad_to_var_
->
end
())
{
...
...
paddle/fluid/imperative/layer.h
浏览文件 @
3cd10a7c
...
@@ -60,7 +60,8 @@ class OpBase {
...
@@ -60,7 +60,8 @@ class OpBase {
pre_ops_
(
new
std
::
vector
<
OpBase
*>
()),
pre_ops_
(
new
std
::
vector
<
OpBase
*>
()),
pre_ops_out_idx_
(
new
std
::
vector
<
int
>
()),
pre_ops_out_idx_
(
new
std
::
vector
<
int
>
()),
op_desc_
(
nullptr
),
op_desc_
(
nullptr
),
grad_op_desc_
(
nullptr
)
{}
grad_op_desc_
(
nullptr
),
grad_to_var_
(
nullptr
)
{}
virtual
~
OpBase
()
{
virtual
~
OpBase
()
{
delete
input_vars_
;
delete
input_vars_
;
...
...
paddle/fluid/imperative/tracer.h
浏览文件 @
3cd10a7c
...
@@ -43,20 +43,14 @@ void CreateGradOp(const framework::OpDesc& op_desc,
...
@@ -43,20 +43,14 @@ void CreateGradOp(const framework::OpDesc& op_desc,
class
Tracer
{
class
Tracer
{
public:
public:
explicit
Tracer
(
framework
::
BlockDesc
*
root_block
,
explicit
Tracer
(
framework
::
BlockDesc
*
root_block
)
framework
::
BlockDesc
*
startup_block
)
:
root_scope_
(
new
framework
::
Scope
())
{}
:
root_block_
(
root_block
),
startup_block_
(
startup_block
)
{
root_scope_
=
new
framework
::
Scope
();
scopes_
[
root_block_
]
=
root_scope_
;
scopes_
[
startup_block_
]
=
root_scope_
;
}
virtual
~
Tracer
()
{
delete
root_scope_
;
}
virtual
~
Tracer
()
{}
void
Trace
(
OpBase
*
op
,
const
std
::
vector
<
VarBase
*>&
inputs
,
void
Trace
(
OpBase
*
op
,
const
std
::
vector
<
VarBase
*>&
inputs
,
const
std
::
vector
<
VarBase
*>&
outputs
,
const
std
::
vector
<
VarBase
*>&
outputs
,
framework
::
BlockDesc
*
block
,
framework
::
BlockDesc
*
block
)
{
const
bool
stop_gradient
)
{
framework
::
Scope
*
scope
=
GetScope
(
block
);
framework
::
OpDesc
*
op_desc
=
op
->
op_desc_
;
framework
::
OpDesc
*
op_desc
=
op
->
op_desc_
;
VLOG
(
3
)
<<
"tracer tracing "
<<
op_desc
->
Type
();
VLOG
(
3
)
<<
"tracer tracing "
<<
op_desc
->
Type
();
op_desc
->
InferShape
(
*
block
);
op_desc
->
InferShape
(
*
block
);
...
@@ -67,7 +61,7 @@ class Tracer {
...
@@ -67,7 +61,7 @@ class Tracer {
*
op
->
input_vars_
=
inputs
;
*
op
->
input_vars_
=
inputs
;
for
(
VarBase
*
input
:
inputs
)
{
for
(
VarBase
*
input
:
inputs
)
{
const
std
::
string
vname
=
input
->
var_desc_
->
Name
();
const
std
::
string
vname
=
input
->
var_desc_
->
Name
();
framework
::
Variable
*
var
=
scope
->
Var
(
vname
);
framework
::
Variable
*
var
=
root_scope_
->
Var
(
vname
);
input
->
var_
=
var
;
input
->
var_
=
var
;
if
(
!
var
->
IsInitialized
())
{
if
(
!
var
->
IsInitialized
())
{
framework
::
VarDesc
*
var_desc
=
block
->
FindVar
(
vname
);
framework
::
VarDesc
*
var_desc
=
block
->
FindVar
(
vname
);
...
@@ -90,7 +84,7 @@ class Tracer {
...
@@ -90,7 +84,7 @@ class Tracer {
*
op
->
output_vars_
=
outputs
;
*
op
->
output_vars_
=
outputs
;
for
(
size_t
i
=
0
;
i
<
outputs
.
size
();
++
i
)
{
for
(
size_t
i
=
0
;
i
<
outputs
.
size
();
++
i
)
{
const
std
::
string
vname
=
outputs
[
i
]
->
var_desc_
->
Name
();
const
std
::
string
vname
=
outputs
[
i
]
->
var_desc_
->
Name
();
framework
::
Variable
*
var
=
scope
->
Var
(
vname
);
framework
::
Variable
*
var
=
root_scope_
->
Var
(
vname
);
if
(
!
var
->
IsInitialized
())
{
if
(
!
var
->
IsInitialized
())
{
framework
::
VarDesc
*
var_desc
=
block
->
FindVar
(
vname
);
framework
::
VarDesc
*
var_desc
=
block
->
FindVar
(
vname
);
if
(
var_desc
->
GetType
()
==
framework
::
proto
::
VarType
::
LOD_TENSOR
)
{
if
(
var_desc
->
GetType
()
==
framework
::
proto
::
VarType
::
LOD_TENSOR
)
{
...
@@ -105,11 +99,8 @@ class Tracer {
...
@@ -105,11 +99,8 @@ class Tracer {
}
}
VLOG
(
3
)
<<
"tracer running "
<<
op_desc
->
Type
();
VLOG
(
3
)
<<
"tracer running "
<<
op_desc
->
Type
();
op_base
->
Run
(
*
scope
,
platform
::
CPUPlace
());
op_base
->
Run
(
*
root_scope_
,
platform
::
CPUPlace
());
if
(
block
==
startup_block_
)
{
if
(
!
stop_gradient
)
{
op
->
grad_op_desc_
=
nullptr
;
op
->
grad_to_var_
=
nullptr
;
}
else
{
framework
::
OpDesc
*
grad_op_desc
;
framework
::
OpDesc
*
grad_op_desc
;
auto
grad_to_var
=
new
std
::
unordered_map
<
std
::
string
,
std
::
string
>
();
auto
grad_to_var
=
new
std
::
unordered_map
<
std
::
string
,
std
::
string
>
();
CreateGradOp
(
*
op_desc
,
{},
{
block
},
&
grad_op_desc
,
grad_to_var
);
CreateGradOp
(
*
op_desc
,
{},
{
block
},
&
grad_op_desc
,
grad_to_var
);
...
@@ -119,22 +110,10 @@ class Tracer {
...
@@ -119,22 +110,10 @@ class Tracer {
op
->
block_
=
block
;
op
->
block_
=
block
;
}
}
framework
::
Scope
*
GetScope
(
framework
::
BlockDesc
*
block
)
{
framework
::
Scope
*
GetScope
()
{
return
root_scope_
.
get
();
}
if
(
scopes_
.
find
(
block
)
!=
scopes_
.
end
())
{
return
scopes_
.
at
(
block
);
}
framework
::
BlockDesc
*
parent_block
=
block
->
ParentBlock
();
PADDLE_ENFORCE
(
scopes_
.
find
(
parent_block
)
!=
scopes_
.
end
());
framework
::
Scope
*
scope
=
&
scopes_
[
parent_block
]
->
NewScope
();
scopes_
[
block
]
=
scope
;
return
scope
;
}
private:
private:
std
::
map
<
framework
::
BlockDesc
*
,
framework
::
Scope
*>
scopes_
;
std
::
unique_ptr
<
framework
::
Scope
>
root_scope_
;
framework
::
BlockDesc
*
root_block_
;
framework
::
BlockDesc
*
startup_block_
;
framework
::
Scope
*
root_scope_
;
};
};
}
// namespace imperative
}
// namespace imperative
...
...
paddle/fluid/pybind/imperative.cc
浏览文件 @
3cd10a7c
...
@@ -24,9 +24,8 @@ namespace pybind {
...
@@ -24,9 +24,8 @@ namespace pybind {
void
BindTracer
(
pybind11
::
module
*
m
)
{
void
BindTracer
(
pybind11
::
module
*
m
)
{
pybind11
::
class_
<
imperative
::
Tracer
>
(
*
m
,
"Tracer"
,
""
)
pybind11
::
class_
<
imperative
::
Tracer
>
(
*
m
,
"Tracer"
,
""
)
.
def
(
"__init__"
,
.
def
(
"__init__"
,
[](
imperative
::
Tracer
&
self
,
framework
::
BlockDesc
*
root_block
,
[](
imperative
::
Tracer
&
self
,
framework
::
BlockDesc
*
root_block
)
{
framework
::
BlockDesc
*
startup_block
)
{
new
(
&
self
)
imperative
::
Tracer
(
root_block
);
new
(
&
self
)
imperative
::
Tracer
(
root_block
,
startup_block
);
})
})
.
def
(
"trace"
,
&
imperative
::
Tracer
::
Trace
)
.
def
(
"trace"
,
&
imperative
::
Tracer
::
Trace
)
.
def
(
"get_scope"
,
&
imperative
::
Tracer
::
GetScope
,
.
def
(
"get_scope"
,
&
imperative
::
Tracer
::
GetScope
,
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
3cd10a7c
...
@@ -117,6 +117,12 @@ PYBIND11_MODULE(core, m) {
...
@@ -117,6 +117,12 @@ PYBIND11_MODULE(core, m) {
self
.
RunBackward
(
scope
);
self
.
RunBackward
(
scope
);
})
})
.
def
(
"_grad"
,
&
imperative
::
VarBase
::
Grad
)
.
def
(
"_grad"
,
&
imperative
::
VarBase
::
Grad
)
.
def_property
(
"value"
,
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
var_
;
},
[](
imperative
::
VarBase
&
self
,
framework
::
Variable
*
var
)
{
self
.
var_
=
var
;
},
py
::
return_value_policy
::
reference
)
.
def_property
(
.
def_property
(
"desc"
,
"desc"
,
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
var_desc_
;
},
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
var_desc_
;
},
...
...
python/paddle/fluid/framework.py
浏览文件 @
3cd10a7c
...
@@ -361,7 +361,7 @@ class Variable(object):
...
@@ -361,7 +361,7 @@ class Variable(object):
self
.
_ivar
.
desc
=
self
.
desc
self
.
_ivar
.
desc
=
self
.
desc
def
_numpy
(
self
):
def
_numpy
(
self
):
scope
=
_imperative_tracer
().
get_scope
(
self
.
block
.
desc
)
scope
=
_imperative_tracer
().
get_scope
()
tensor
=
core
.
get_variable_tensor
(
scope
,
self
.
desc
.
name
())
tensor
=
core
.
get_variable_tensor
(
scope
,
self
.
desc
.
name
())
return
np
.
array
(
tensor
)
return
np
.
array
(
tensor
)
...
@@ -573,7 +573,8 @@ class Operator(object):
...
@@ -573,7 +573,8 @@ class Operator(object):
type
=
None
,
type
=
None
,
inputs
=
None
,
inputs
=
None
,
outputs
=
None
,
outputs
=
None
,
attrs
=
None
):
attrs
=
None
,
stop_gradient
=
False
):
self
.
block
=
block
self
.
block
=
block
self
.
desc
=
desc
self
.
desc
=
desc
# note: not add self.attrs here:
# note: not add self.attrs here:
...
@@ -1264,9 +1265,12 @@ class Block(object):
...
@@ -1264,9 +1265,12 @@ class Block(object):
"""
"""
op_desc
=
self
.
desc
.
append_op
()
op_desc
=
self
.
desc
.
append_op
()
op
=
Operator
(
block
=
self
,
desc
=
op_desc
,
*
args
,
**
kwargs
)
op
=
Operator
(
block
=
self
,
desc
=
op_desc
,
*
args
,
**
kwargs
)
print
(
"append_op"
,
kwargs
.
get
(
"type"
),
kwargs
.
get
(
"stop_gradient"
,
False
))
if
_in_imperative_mode
():
if
_in_imperative_mode
():
_imperative_tracer
().
trace
(
op
.
iop
,
[
v
.
_ivar
for
v
in
op
.
inputs
],
_imperative_tracer
().
trace
(
op
.
iop
,
[
v
.
_ivar
for
v
in
op
.
inputs
],
[
v
.
_ivar
for
v
in
op
.
outputs
],
self
.
desc
)
[
v
.
_ivar
for
v
in
op
.
outputs
],
self
.
desc
,
kwargs
.
get
(
"stop_gradient"
,
False
))
self
.
ops
.
append
(
op
)
self
.
ops
.
append
(
op
)
return
op
return
op
...
@@ -1316,9 +1320,12 @@ class Block(object):
...
@@ -1316,9 +1320,12 @@ class Block(object):
def
_prepend_op
(
self
,
*
args
,
**
kwargs
):
def
_prepend_op
(
self
,
*
args
,
**
kwargs
):
op_desc
=
self
.
desc
.
_prepend_op
()
op_desc
=
self
.
desc
.
_prepend_op
()
op
=
Operator
(
self
,
op_desc
,
*
args
,
**
kwargs
)
op
=
Operator
(
self
,
op_desc
,
*
args
,
**
kwargs
)
print
(
"prepend_op"
,
kwargs
.
get
(
"type"
),
kwargs
.
get
(
"stop_gradient"
,
False
))
if
_in_imperative_mode
():
if
_in_imperative_mode
():
_imperative_tracer
().
trace
(
op
.
iop
,
[
v
.
_ivar
for
v
in
op
.
inputs
],
_imperative_tracer
().
trace
(
op
.
iop
,
[
v
.
_ivar
for
v
in
op
.
inputs
],
[
v
.
_ivar
for
v
in
op
.
outputs
],
self
.
desc
)
[
v
.
_ivar
for
v
in
op
.
outputs
],
self
.
desc
,
kwargs
.
get
(
"stop_gradient"
,
False
))
self
.
ops
.
insert
(
0
,
op
)
self
.
ops
.
insert
(
0
,
op
)
return
op
return
op
...
...
python/paddle/fluid/imperative/__init__.py
浏览文件 @
3cd10a7c
...
@@ -20,6 +20,10 @@ from .base import *
...
@@ -20,6 +20,10 @@ from .base import *
from
.
import
layers
from
.
import
layers
from
.layers
import
*
from
.layers
import
*
from
.
import
nn
from
.nn
import
*
__all__
=
[]
__all__
=
[]
__all__
+=
layers
.
__all__
__all__
+=
layers
.
__all__
__all__
+=
base
.
__all__
__all__
+=
base
.
__all__
__all__
+=
nn
.
__all__
python/paddle/fluid/imperative/base.py
浏览文件 @
3cd10a7c
...
@@ -28,8 +28,7 @@ def enabled():
...
@@ -28,8 +28,7 @@ def enabled():
def
guard
():
def
guard
():
train
=
framework
.
Program
()
train
=
framework
.
Program
()
startup
=
framework
.
Program
()
startup
=
framework
.
Program
()
tracer
=
core
.
Tracer
(
train
.
current_block
().
desc
,
tracer
=
core
.
Tracer
(
train
.
current_block
().
desc
)
startup
.
current_block
().
desc
)
with
framework
.
program_guard
(
train
,
startup
):
with
framework
.
program_guard
(
train
,
startup
):
with
framework
.
unique_name
.
guard
():
with
framework
.
unique_name
.
guard
():
with
framework
.
_imperative_guard
(
tracer
):
with
framework
.
_imperative_guard
(
tracer
):
...
@@ -46,7 +45,7 @@ def to_variable(value, block=None):
...
@@ -46,7 +45,7 @@ def to_variable(value, block=None):
name
=
None
,
name
=
None
,
shape
=
value
.
shape
,
shape
=
value
.
shape
,
dtype
=
value
.
dtype
)
dtype
=
value
.
dtype
)
scope
=
framework
.
_imperative_tracer
().
get_scope
(
block
.
desc
)
scope
=
framework
.
_imperative_tracer
().
get_scope
()
var
=
scope
.
var
(
py_var
.
name
)
var
=
scope
.
var
(
py_var
.
name
)
tensor
=
var
.
get_tensor
()
tensor
=
var
.
get_tensor
()
tensor
.
set
(
value
,
core
.
CPUPlace
())
tensor
.
set
(
value
,
core
.
CPUPlace
())
...
...
python/paddle/fluid/imperative/layers.py
浏览文件 @
3cd10a7c
...
@@ -24,8 +24,10 @@ __all__ = ['PyLayer']
...
@@ -24,8 +24,10 @@ __all__ = ['PyLayer']
class
PyLayer
(
core
.
Layer
):
class
PyLayer
(
core
.
Layer
):
def
__init__
(
self
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_built
=
False
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
type
(
self
).
__name__
,
**
kwargs
)
self
.
_dtype
=
kwargs
.
get
(
"dtype"
,
core
.
VarDesc
.
VarType
.
FP32
)
def
__call__
(
self
,
inputs
):
def
__call__
(
self
,
inputs
):
if
not
isinstance
(
inputs
,
list
)
and
not
isinstance
(
inputs
,
tuple
):
if
not
isinstance
(
inputs
,
list
)
and
not
isinstance
(
inputs
,
tuple
):
...
@@ -35,15 +37,10 @@ class PyLayer(core.Layer):
...
@@ -35,15 +37,10 @@ class PyLayer(core.Layer):
for
x
in
inputs
:
for
x
in
inputs
:
py_var
=
base
.
to_variable
(
x
)
py_var
=
base
.
to_variable
(
x
)
var_inputs
.
append
(
py_var
)
var_inputs
.
append
(
py_var
)
if
not
self
.
_built
:
self
.
_build_once
(
inputs
)
self
.
_built
=
True
outputs
=
self
.
forward
(
var_inputs
)
outputs
=
self
.
forward
(
var_inputs
)
return
outputs
def
_build_once
(
self
,
inputs
):
return
outputs
pass
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
return
[]
return
[]
python/paddle/fluid/initializer.py
浏览文件 @
3cd10a7c
...
@@ -161,7 +161,8 @@ class ConstantInitializer(Initializer):
...
@@ -161,7 +161,8 @@ class ConstantInitializer(Initializer):
"dtype"
:
int
(
var
.
dtype
),
"dtype"
:
int
(
var
.
dtype
),
"value"
:
float
(
self
.
_value
),
"value"
:
float
(
self
.
_value
),
'force_cpu'
:
self
.
_force_cpu
or
force_init_on_cpu
()
'force_cpu'
:
self
.
_force_cpu
or
force_init_on_cpu
()
})
},
stop_gradient
=
True
)
var
.
op
=
op
var
.
op
=
op
return
op
return
op
...
@@ -216,7 +217,8 @@ class UniformInitializer(Initializer):
...
@@ -216,7 +217,8 @@ class UniformInitializer(Initializer):
"min"
:
self
.
_low
,
"min"
:
self
.
_low
,
"max"
:
self
.
_high
,
"max"
:
self
.
_high
,
"seed"
:
self
.
_seed
"seed"
:
self
.
_seed
})
},
stop_gradient
=
True
)
var
.
op
=
op
var
.
op
=
op
return
op
return
op
...
@@ -271,7 +273,8 @@ class NormalInitializer(Initializer):
...
@@ -271,7 +273,8 @@ class NormalInitializer(Initializer):
"std"
:
self
.
_std_dev
,
"std"
:
self
.
_std_dev
,
"seed"
:
self
.
_seed
,
"seed"
:
self
.
_seed
,
"use_mkldnn"
:
False
"use_mkldnn"
:
False
})
},
stop_gradient
=
True
)
var
.
op
=
op
var
.
op
=
op
return
op
return
op
...
@@ -325,7 +328,8 @@ class TruncatedNormalInitializer(Initializer):
...
@@ -325,7 +328,8 @@ class TruncatedNormalInitializer(Initializer):
"mean"
:
self
.
_mean
,
"mean"
:
self
.
_mean
,
"std"
:
self
.
_std_dev
,
"std"
:
self
.
_std_dev
,
"seed"
:
self
.
_seed
"seed"
:
self
.
_seed
})
},
stop_gradient
=
True
)
var
.
op
=
op
var
.
op
=
op
return
op
return
op
...
@@ -415,7 +419,8 @@ class XavierInitializer(Initializer):
...
@@ -415,7 +419,8 @@ class XavierInitializer(Initializer):
"min"
:
-
limit
,
"min"
:
-
limit
,
"max"
:
limit
,
"max"
:
limit
,
"seed"
:
self
.
_seed
"seed"
:
self
.
_seed
})
},
stop_gradient
=
True
)
else
:
else
:
std
=
np
.
sqrt
(
2.0
/
float
(
fan_in
+
fan_out
))
std
=
np
.
sqrt
(
2.0
/
float
(
fan_in
+
fan_out
))
...
@@ -428,7 +433,8 @@ class XavierInitializer(Initializer):
...
@@ -428,7 +433,8 @@ class XavierInitializer(Initializer):
"mean"
:
0.0
,
"mean"
:
0.0
,
"std"
:
std
,
"std"
:
std
,
"seed"
:
self
.
_seed
"seed"
:
self
.
_seed
})
},
stop_gradient
=
True
)
var
.
op
=
op
var
.
op
=
op
return
op
return
op
...
@@ -513,7 +519,8 @@ class MSRAInitializer(Initializer):
...
@@ -513,7 +519,8 @@ class MSRAInitializer(Initializer):
"min"
:
-
limit
,
"min"
:
-
limit
,
"max"
:
limit
,
"max"
:
limit
,
"seed"
:
self
.
_seed
"seed"
:
self
.
_seed
})
},
stop_gradient
=
True
)
else
:
else
:
std
=
np
.
sqrt
(
2.0
/
float
(
fan_in
))
std
=
np
.
sqrt
(
2.0
/
float
(
fan_in
))
...
@@ -526,7 +533,8 @@ class MSRAInitializer(Initializer):
...
@@ -526,7 +533,8 @@ class MSRAInitializer(Initializer):
"mean"
:
0.0
,
"mean"
:
0.0
,
"std"
:
std
,
"std"
:
std
,
"seed"
:
self
.
_seed
"seed"
:
self
.
_seed
})
},
stop_gradient
=
True
)
var
.
op
=
op
var
.
op
=
op
return
op
return
op
...
...
python/paddle/fluid/layer_helper.py
浏览文件 @
3cd10a7c
...
@@ -22,8 +22,8 @@ import numpy as np
...
@@ -22,8 +22,8 @@ import numpy as np
from
.framework
import
Variable
,
Parameter
,
default_main_program
,
default_startup_program
,
dtype_is_floating
from
.framework
import
Variable
,
Parameter
,
default_main_program
,
default_startup_program
,
dtype_is_floating
from
.
import
unique_name
from
.
import
unique_name
from
paddle.fluid.imperative.base
import
to_variable
from
paddle.fluid.initializer
import
Constant
,
Xavier
from
paddle.fluid.initializer
import
Constant
,
Xavier
from
paddle.fluid.imperative
import
base
from
.param_attr
import
ParamAttr
,
WeightNormParamAttr
from
.param_attr
import
ParamAttr
,
WeightNormParamAttr
from
.
import
core
from
.
import
core
from
six.moves
import
zip
from
six.moves
import
zip
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
3cd10a7c
...
@@ -29,7 +29,6 @@ from . import utils
...
@@ -29,7 +29,6 @@ from . import utils
from
..
import
unique_name
from
..
import
unique_name
from
functools
import
reduce
from
functools
import
reduce
from
..
import
core
from
..
import
core
from
..imperative
import
layers
__all__
=
[
__all__
=
[
'fc'
,
'fc'
,
...
@@ -2537,12 +2536,12 @@ def adaptive_pool2d(input,
...
@@ -2537,12 +2536,12 @@ def adaptive_pool2d(input,
Examples:
Examples:
.. code-block:: python
.. code-block:: python
# suppose input data in shape of [N, C, H, W], `pool_size` is [m, n],
# suppose input data in shape of [N, C, H, W], `pool_size` is [m, n],
# output shape is [N, C, m, n], adaptive pool divide H and W dimentions
# output shape is [N, C, m, n], adaptive pool divide H and W dimentions
# of input data into m * n grids averagely and performs poolings in each
# of input data into m * n grids averagely and performs poolings in each
# grid to get output.
# grid to get output.
# adaptive average pool performs calculations as follow:
# adaptive average pool performs calculations as follow:
#
#
# for i in range(m):
# for i in range(m):
# for j in range(n):
# for j in range(n):
# hstart = floor(i * H / m)
# hstart = floor(i * H / m)
...
@@ -2636,10 +2635,10 @@ def adaptive_pool3d(input,
...
@@ -2636,10 +2635,10 @@ def adaptive_pool3d(input,
# suppose input data in shape of [N, C, D, H, W], `pool_size` is [l, m, n],
# suppose input data in shape of [N, C, D, H, W], `pool_size` is [l, m, n],
# output shape is [N, C, l, m, n], adaptive pool divide D, H and W dimentions
# output shape is [N, C, l, m, n], adaptive pool divide D, H and W dimentions
# of input data into l * m * n grids averagely and performs poolings in each
# of input data into l * m * n grids averagely and performs poolings in each
# grid to get output.
# grid to get output.
# adaptive average pool performs calculations as follow:
# adaptive average pool performs calculations as follow:
#
#
# for i in range(l):
# for i in range(l):
# for j in range(m):
# for j in range(m):
# for k in range(n):
# for k in range(n):
...
@@ -2649,7 +2648,7 @@ def adaptive_pool3d(input,
...
@@ -2649,7 +2648,7 @@ def adaptive_pool3d(input,
# hend = ceil((j + 1) * H / m)
# hend = ceil((j + 1) * H / m)
# wstart = floor(k * W / n)
# wstart = floor(k * W / n)
# wend = ceil((k + 1) * W / n)
# wend = ceil((k + 1) * W / n)
# output[:, :, i, j, k] =
# output[:, :, i, j, k] =
# avg(input[:, :, dstart:dend, hstart: hend, wstart: wend])
# avg(input[:, :, dstart:dend, hstart: hend, wstart: wend])
#
#
data = fluid.layers.data(
data = fluid.layers.data(
...
@@ -9427,47 +9426,3 @@ def huber_loss(input, label, delta):
...
@@ -9427,47 +9426,3 @@ def huber_loss(input, label, delta):
'Residual'
:
residual
},
'Residual'
:
residual
},
attrs
=
{
'delta'
:
delta
})
attrs
=
{
'delta'
:
delta
})
return
out
return
out
class
FC
(
layers
.
PyLayer
):
def
__init__
(
self
,
size
,
param_attr
=
None
,
num_flatten_dims
=
1
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
):
super
(
FC
,
self
).
__init__
()
self
.
_size
=
size
self
.
_num_flatten_dims
=
num_flatten_dims
self
.
_dtype
=
dtype
self
.
_helper
=
LayerHelper
(
'FC'
,
param_attr
=
param_attr
)
def
_build_once
(
self
,
inputs
):
input_shape
=
inputs
[
0
].
shape
param_shape
=
[
reduce
(
lambda
a
,
b
:
a
*
b
,
input_shape
[
self
.
_num_flatten_dims
:],
1
)
]
+
[
self
.
_size
]
self
.
_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_helper
.
param_attr
,
shape
=
param_shape
,
dtype
=
self
.
_dtype
,
is_bias
=
False
)
def
forward
(
self
,
inputs
):
tmp
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
self
.
_helper
.
append_op
(
type
=
"mul"
,
inputs
=
{
"X"
:
inputs
[
0
],
"Y"
:
self
.
_w
},
outputs
=
{
"Out"
:
tmp
},
attrs
=
{
"x_num_col_dims"
:
self
.
_num_flatten_dims
,
"y_num_col_dims"
:
1
})
out
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
self
.
_helper
.
append_op
(
type
=
"sum"
,
inputs
=
{
"X"
:
[
tmp
]},
outputs
=
{
"Out"
:
out
},
attrs
=
{
"use_mkldnn"
:
False
})
return
out
python/paddle/fluid/tests/unittests/test_imperative.py
已删除
100644 → 0
浏览文件 @
8d88c5a8
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
contextlib
import
unittest
import
numpy
as
np
import
paddle.fluid
as
fluid
from
paddle.fluid
import
core
from
paddle.fluid.layers.nn
import
FC
@
contextlib
.
contextmanager
def
new_program_scope
():
prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
scope
=
fluid
.
core
.
Scope
()
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
program_guard
(
prog
,
startup_prog
):
yield
class
MyLayer
(
fluid
.
imperative
.
PyLayer
):
def
__init__
(
self
):
super
(
MyLayer
,
self
).
__init__
()
def
forward
(
self
,
inputs
):
x
=
fluid
.
layers
.
relu
(
inputs
[
0
])
self
.
_x_for_debug
=
x
return
[
fluid
.
layers
.
elementwise_mul
(
x
,
x
)]
class
MLP
(
fluid
.
imperative
.
PyLayer
):
def
__init__
(
self
):
super
(
MLP
,
self
).
__init__
()
self
.
_fc1
=
FC
(
3
,
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.1
)))
self
.
_fc2
=
FC
(
4
,
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.1
)))
def
forward
(
self
,
inputs
):
x
=
self
.
_fc1
(
inputs
[
0
])
x
=
self
.
_fc2
(
x
)
x
=
fluid
.
layers
.
reduce_sum
(
x
)
return
x
class
TestImperative
(
unittest
.
TestCase
):
def
test_layer
(
self
):
with
fluid
.
imperative
.
guard
():
cl
=
core
.
Layer
()
cl
.
forward
([])
l
=
fluid
.
imperative
.
PyLayer
()
l
.
forward
([])
def
test_layer_in_out
(
self
):
np_inp
=
np
.
array
([
1.0
,
2.0
,
-
1.0
],
dtype
=
np
.
float32
)
with
fluid
.
imperative
.
guard
():
l
=
MyLayer
()
x
=
l
(
np_inp
)[
0
]
self
.
assertIsNotNone
(
x
)
dy_out
=
x
.
_numpy
()
x
.
_backward
()
dy_grad
=
l
.
_x_for_debug
.
_gradient
()
with
new_program_scope
():
inp
=
fluid
.
layers
.
data
(
name
=
"inp"
,
shape
=
[
3
],
append_batch_size
=
False
)
l
=
MyLayer
()
x
=
l
(
inp
)[
0
]
param_grads
=
fluid
.
backward
.
append_backward
(
x
,
parameter_list
=
[
l
.
_x_for_debug
.
name
])[
0
]
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
static_out
,
static_grad
=
exe
.
run
(
feed
=
{
inp
.
name
:
np_inp
},
fetch_list
=
[
x
.
name
,
param_grads
[
1
].
name
])
self
.
assertTrue
(
np
.
allclose
(
dy_out
,
static_out
))
self
.
assertTrue
(
np
.
allclose
(
dy_grad
,
static_grad
))
def
test_mlp
(
self
):
np_inp
=
np
.
array
([[
1.0
,
2.0
],
[
3.0
,
4.0
]],
dtype
=
np
.
float32
)
with
fluid
.
imperative
.
guard
():
mlp
=
MLP
()
out
=
mlp
(
np_inp
)
dy_out
=
out
.
_numpy
()
out
.
_backward
()
dy_grad
=
mlp
.
_fc1
.
_w
.
_gradient
()
with
new_program_scope
():
inp
=
fluid
.
layers
.
data
(
name
=
"inp"
,
shape
=
[
2
,
2
],
append_batch_size
=
False
)
mlp
=
MLP
()
out
=
mlp
(
inp
)
param_grads
=
fluid
.
backward
.
append_backward
(
out
,
parameter_list
=
[
mlp
.
_fc1
.
_w
.
name
])[
0
]
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
exe
.
run
(
fluid
.
default_startup_program
())
static_out
,
static_grad
=
exe
.
run
(
feed
=
{
inp
.
name
:
np_inp
},
fetch_list
=
[
out
.
name
,
param_grads
[
1
].
name
])
self
.
assertTrue
(
np
.
allclose
(
dy_out
,
static_out
))
self
.
assertTrue
(
np
.
allclose
(
dy_grad
,
static_grad
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_imperative_mnist.py
0 → 100644
浏览文件 @
3cd10a7c
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
contextlib
import
unittest
import
numpy
as
np
import
paddle.fluid
as
fluid
from
paddle.fluid
import
core
from
paddle.fluid.imperative.nn
import
Conv2D
@
contextlib
.
contextmanager
def
new_program_scope
():
prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
scope
=
fluid
.
core
.
Scope
()
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
program_guard
(
prog
,
startup_prog
):
yield
class
MNIST
(
fluid
.
imperative
.
PyLayer
):
def
__init__
(
self
):
super
(
MNIST
,
self
).
__init__
()
groups
=
1
dilation
=
[
1
,
1
]
pad
=
[
0
,
0
]
stride
=
[
1
,
1
]
input_size
=
[
2
,
3
,
5
,
5
]
# NCHW
assert
np
.
mod
(
input_size
[
1
],
groups
)
==
0
f_c
=
input_size
[
1
]
//
groups
filter_size
=
[
6
,
f_c
,
3
,
3
]
self
.
_conv2d
=
Conv2D
(
num_channels
=
3
,
num_filters
=
20
,
filter_size
=
3
,
stride
=
stride
,
padding
=
pad
,
dilation
=
dilation
,
groups
=
groups
,
use_cudnn
=
False
)
def
forward
(
self
,
inputs
):
x
=
self
.
_conv2d
(
inputs
)
return
x
class
TestImperativeMnist
(
unittest
.
TestCase
):
# def test_layer(self):
# with fluid.imperative.guard():
# cl = core.Layer()
# cl.forward([])
# l = fluid.imperative.PyLayer()
# l.forward([])
# def test_layer_in_out(self):
# np_inp = np.array([1.0, 2.0, -1.0], dtype=np.float32)
# with fluid.imperative.guard():
# l = MyLayer()
# x = l(np_inp)[0]
# self.assertIsNotNone(x)
# dy_out = x._numpy()
# x._backward()
# dy_grad = l._x_for_debug._gradient()
# with new_program_scope():
# inp = fluid.layers.data(
# name="inp", shape=[3], append_batch_size=False)
# l = MyLayer()
# x = l(inp)[0]
# param_grads = fluid.backward.append_backward(
# x, parameter_list=[l._x_for_debug.name])[0]
# exe = fluid.Executor(fluid.CPUPlace())
# static_out, static_grad = exe.run(
# feed={inp.name: np_inp},
# fetch_list=[x.name, param_grads[1].name])
# self.assertTrue(np.allclose(dy_out, static_out))
# self.assertTrue(np.allclose(dy_grad, static_grad))
def
test_mnist_cpu_float32
(
self
):
with
fluid
.
imperative
.
guard
():
mnist
=
MNIST
()
data
=
np
.
random
.
rand
(
2
,
3
,
5
,
5
).
astype
(
'float32'
)
mnist
(
data
)
# np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)
# with fluid.imperative.guard():
# mlp = MLP()
# out = mlp(np_inp)
# dy_out = out._numpy()
# out._backward()
# dy_grad = mlp._fc1._w._gradient()
# with new_program_scope():
# inp = fluid.layers.data(
# name="inp", shape=[2, 2], append_batch_size=False)
# mlp = MLP()
# out = mlp(inp)
# param_grads = fluid.backward.append_backward(
# out, parameter_list=[mlp._fc1._w.name])[0]
# exe = fluid.Executor(fluid.CPUPlace())
# exe.run(fluid.default_startup_program())
# static_out, static_grad = exe.run(
# feed={inp.name: np_inp},
# fetch_list=[out.name, param_grads[1].name])
# self.assertTrue(np.allclose(dy_out, static_out))
# self.assertTrue(np.allclose(dy_grad, static_grad))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录