Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
432b5df8
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
432b5df8
编写于
4月 15, 2020
作者:
Z
zhongpu
提交者:
GitHub
4月 15, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove dygraph api to paddle.imperative, test=develop (#23628) (#23864)
上级
22f06e64
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
89 addition
and
32 deletion
+89
-32
python/paddle/__init__.py
python/paddle/__init__.py
+1
-0
python/paddle/fluid/tests/unittests/test_imperative_basic.py
python/paddle/fluid/tests/unittests/test_imperative_basic.py
+19
-0
python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py
...id/tests/unittests/test_imperative_container_layerlist.py
+16
-3
python/paddle/fluid/tests/unittests/test_imperative_container_parameterlist.py
...ests/unittests/test_imperative_container_parameterlist.py
+20
-4
python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py
...le/fluid/tests/unittests/test_imperative_data_parallel.py
+1
-1
python/paddle/fluid/tests/unittests/test_imperative_mnist.py
python/paddle/fluid/tests/unittests/test_imperative_mnist.py
+1
-2
python/paddle/fluid/tests/unittests/test_imperative_named_members.py
...le/fluid/tests/unittests/test_imperative_named_members.py
+2
-1
python/paddle/fluid/tests/unittests/test_imperative_save_load.py
...paddle/fluid/tests/unittests/test_imperative_save_load.py
+6
-4
python/paddle/fluid/tests/unittests/test_imperative_selected_rows.py
...le/fluid/tests/unittests/test_imperative_selected_rows.py
+5
-4
python/paddle/imperative/__init__.py
python/paddle/imperative/__init__.py
+16
-12
python/setup.py.in
python/setup.py.in
+2
-1
未找到文件。
python/paddle/__init__.py
浏览文件 @
432b5df8
...
...
@@ -36,6 +36,7 @@ batch = batch.batch
import
paddle.sysconfig
import
paddle.tensor
import
paddle.nn
import
paddle.imperative
# TODO: define alias in tensor and framework directory
# from .tensor.creation import create_.tensor #DEFINE_ALIAS
...
...
python/paddle/fluid/tests/unittests/test_imperative_basic.py
浏览文件 @
432b5df8
...
...
@@ -21,6 +21,7 @@ from paddle.fluid import core
from
paddle.fluid
import
Linear
from
test_imperative_base
import
new_program_scope
import
paddle.fluid.dygraph_utils
as
dygraph_utils
import
paddle
class
MyLayer
(
fluid
.
Layer
):
...
...
@@ -245,6 +246,24 @@ class TestImperative(unittest.TestCase):
self
.
assertTrue
(
tmp
.
_grad_ivar
()
is
None
)
self
.
assertTrue
(
l0
.
weight
.
_grad_ivar
()
is
not
None
)
def
test_paddle_imperative_no_grad_guard
(
self
):
data
=
np
.
array
([[
2
,
3
],
[
4
,
5
]]).
astype
(
'float32'
)
with
fluid
.
dygraph
.
guard
():
l0
=
fluid
.
Linear
(
2
,
2
)
self
.
assertTrue
(
l0
.
weight
.
_grad_ivar
()
is
None
)
l1
=
fluid
.
Linear
(
2
,
2
)
with
paddle
.
imperative
.
no_grad
():
self
.
assertTrue
(
l1
.
weight
.
stop_gradient
is
False
)
tmp
=
l1
.
weight
*
2
self
.
assertTrue
(
tmp
.
stop_gradient
)
x
=
fluid
.
dygraph
.
to_variable
(
data
)
y
=
l0
(
x
)
+
tmp
o
=
l1
(
y
)
o
.
backward
()
self
.
assertTrue
(
tmp
.
_grad_ivar
()
is
None
)
self
.
assertTrue
(
l0
.
weight
.
_grad_ivar
()
is
not
None
)
def
test_sum_op
(
self
):
x
=
np
.
ones
([
2
,
2
],
np
.
float32
)
with
fluid
.
dygraph
.
guard
():
...
...
python/paddle/fluid/tests/unittests/test_imperative_container_layerlist.py
浏览文件 @
432b5df8
...
...
@@ -17,6 +17,7 @@ from __future__ import print_function
import
unittest
import
paddle.fluid
as
fluid
import
numpy
as
np
import
paddle
class
MyLayer
(
fluid
.
Layer
):
...
...
@@ -31,12 +32,20 @@ class MyLayer(fluid.Layer):
class
TestImperativeContainer
(
unittest
.
TestCase
):
def
test_layer_list
(
self
):
def
fluid_dygraph_list
(
self
):
return
fluid
.
dygraph
.
LayerList
(
[
fluid
.
dygraph
.
Linear
(
2
**
i
,
2
**
(
i
+
1
))
for
i
in
range
(
6
)])
def
paddle_imperative_list
(
self
):
return
paddle
.
imperative
.
LayerList
(
[
fluid
.
dygraph
.
Linear
(
2
**
i
,
2
**
(
i
+
1
))
for
i
in
range
(
6
)])
def
layer_list
(
self
,
use_fluid_api
):
data_np
=
np
.
random
.
uniform
(
-
1
,
1
,
[
5
,
1
]).
astype
(
'float32'
)
with
fluid
.
dygraph
.
guard
():
x
=
fluid
.
dygraph
.
to_variable
(
data_np
)
layerlist
=
fluid
.
dygraph
.
LayerL
ist
(
[
fluid
.
dygraph
.
Linear
(
2
**
i
,
2
**
(
i
+
1
))
for
i
in
range
(
6
)]
)
layerlist
=
self
.
fluid_dygraph_l
ist
(
)
if
use_fluid_api
else
self
.
paddle_imperative_list
(
)
size
=
len
(
layerlist
)
model
=
MyLayer
(
layerlist
)
...
...
@@ -75,6 +84,10 @@ class TestImperativeContainer(unittest.TestCase):
self
.
assertListEqual
(
res8
.
shape
,
[
5
,
3
**
3
])
res8
.
backward
()
def
test_layer_list
(
self
):
self
.
layer_list
(
True
)
self
.
layer_list
(
False
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_imperative_container_parameterlist.py
浏览文件 @
432b5df8
...
...
@@ -17,13 +17,25 @@ from __future__ import print_function
import
unittest
import
paddle.fluid
as
fluid
import
numpy
as
np
import
paddle
class
MyLayer
(
fluid
.
Layer
):
def
__init__
(
self
,
num_stacked_param
):
def
__init__
(
self
,
num_stacked_param
,
use_fluid_api
):
super
(
MyLayer
,
self
).
__init__
()
# create ParameterList with iterable Parameters
self
.
params
=
fluid
.
dygraph
.
ParameterList
(
self
.
params
=
self
.
fluid_dygraph_ParameterList
(
num_stacked_param
)
if
use_fluid_api
else
self
.
paddle_imperative_ParameterList
(
num_stacked_param
)
def
fluid_dygraph_ParameterList
(
self
,
num_stacked_param
):
return
fluid
.
dygraph
.
ParameterList
(
[
fluid
.
layers
.
create_parameter
(
shape
=
[
2
,
2
],
dtype
=
'float32'
)]
*
num_stacked_param
)
def
paddle_imperative_ParameterList
(
self
,
num_stacked_param
):
return
paddle
.
imperative
.
ParameterList
(
[
fluid
.
layers
.
create_parameter
(
shape
=
[
2
,
2
],
dtype
=
'float32'
)]
*
num_stacked_param
)
...
...
@@ -42,12 +54,12 @@ class MyLayer(fluid.Layer):
class
TestImperativeContainerParameterList
(
unittest
.
TestCase
):
def
test_paramter_list
(
self
):
def
paramter_list
(
self
,
use_fluid_api
):
data_np
=
np
.
random
.
uniform
(
-
1
,
1
,
[
5
,
2
]).
astype
(
'float32'
)
with
fluid
.
dygraph
.
guard
():
x
=
fluid
.
dygraph
.
to_variable
(
data_np
)
num_stacked_param
=
4
model
=
MyLayer
(
num_stacked_param
)
model
=
MyLayer
(
num_stacked_param
,
use_fluid_api
)
self
.
assertEqual
(
len
(
model
.
params
),
num_stacked_param
)
res
=
model
(
x
)
self
.
assertListEqual
(
res
.
shape
,
[
5
,
2
])
...
...
@@ -67,6 +79,10 @@ class TestImperativeContainerParameterList(unittest.TestCase):
loss
=
fluid
.
layers
.
reduce_mean
(
res
)
loss
.
backward
()
def
test_paramter_list
(
self
):
self
.
paramter_list
(
True
)
self
.
paramter_list
(
False
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_imperative_data_parallel.py
浏览文件 @
432b5df8
...
...
@@ -43,7 +43,7 @@ class MLP(fluid.Layer):
class
TestDataParallelStateDict
(
unittest
.
TestCase
):
def
test_data_parallel_state_dict
(
self
):
with
fluid
.
dygraph
.
guard
():
strategy
=
dygraph
.
parallel
.
prepare_context
()
strategy
=
paddle
.
imperative
.
prepare_context
()
mlp
=
MLP
()
parallel_mlp
=
dygraph
.
parallel
.
DataParallel
(
mlp
,
strategy
)
...
...
python/paddle/fluid/tests/unittests/test_imperative_mnist.py
浏览文件 @
432b5df8
...
...
@@ -27,7 +27,6 @@ from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear
from
paddle.fluid.dygraph.base
import
to_variable
from
test_imperative_base
import
new_program_scope
from
utils
import
DyGraphProgramDescTracerTestHelper
,
is_equal_program
from
paddle.fluid.dygraph
import
TracedLayer
class
SimpleImgConvPool
(
fluid
.
dygraph
.
Layer
):
...
...
@@ -154,7 +153,7 @@ class TestImperativeMnist(unittest.TestCase):
label
.
stop_gradient
=
True
if
batch_id
%
10
==
0
:
cost
,
traced_layer
=
TracedLayer
.
trace
(
cost
,
traced_layer
=
paddle
.
imperative
.
TracedLayer
.
trace
(
mnist
,
inputs
=
img
)
if
program
is
not
None
:
self
.
assertTrue
(
program
,
traced_layer
.
program
)
...
...
python/paddle/fluid/tests/unittests/test_imperative_named_members.py
浏览文件 @
432b5df8
...
...
@@ -15,6 +15,7 @@
import
unittest
import
numpy
as
np
import
paddle.fluid
as
fluid
import
paddle
class
MyLayer
(
fluid
.
Layer
):
...
...
@@ -66,7 +67,7 @@ class TestImperativeNamedParameters(unittest.TestCase):
fc1
=
fluid
.
Linear
(
10
,
3
)
fc2
=
fluid
.
Linear
(
3
,
10
,
bias_attr
=
False
)
custom
=
MyLayer
(
3
,
10
)
model
=
fluid
.
dygraph
.
Sequential
(
fc1
,
fc2
,
custom
)
model
=
paddle
.
imperative
.
Sequential
(
fc1
,
fc2
,
custom
)
named_parameters
=
list
(
model
.
named_parameters
())
expected_named_parameters
=
list
()
...
...
python/paddle/fluid/tests/unittests/test_imperative_save_load.py
浏览文件 @
432b5df8
...
...
@@ -26,6 +26,7 @@ from paddle.fluid.dygraph.learning_rate_scheduler import LearningRateDecay
from
test_imperative_base
import
new_program_scope
import
numpy
as
np
import
six
import
paddle
class
SimpleLSTMRNN
(
fluid
.
Layer
):
...
...
@@ -880,17 +881,18 @@ class TestDygraphPtbRnn(unittest.TestCase):
with
fluid
.
dygraph
.
guard
():
emb
=
fluid
.
dygraph
.
Embedding
([
10
,
10
])
state_dict
=
emb
.
state_dict
()
fluid
.
save_dygraph
(
state_dict
,
os
.
path
.
join
(
'saved_dy'
,
'emb_dy'
))
paddle
.
imperative
.
save_dygraph
(
state_dict
,
os
.
path
.
join
(
'saved_dy'
,
'emb_dy'
))
para_state_dict
,
opti_state_dict
=
fluid
.
load_dygraph
(
para_state_dict
,
opti_state_dict
=
paddle
.
imperative
.
load_dygraph
(
os
.
path
.
join
(
'saved_dy'
,
'emb_dy'
))
self
.
assertTrue
(
opti_state_dict
==
None
)
para_state_dict
,
opti_state_dict
=
fluid
.
load_dygraph
(
para_state_dict
,
opti_state_dict
=
paddle
.
imperative
.
load_dygraph
(
os
.
path
.
join
(
'saved_dy'
,
'emb_dy.pdparams'
))
para_state_dict
,
opti_state_dict
=
fluid
.
load_dygraph
(
para_state_dict
,
opti_state_dict
=
paddle
.
imperative
.
load_dygraph
(
os
.
path
.
join
(
'saved_dy'
,
'emb_dy.pdopt'
))
...
...
python/paddle/fluid/tests/unittests/test_imperative_selected_rows.py
浏览文件 @
432b5df8
...
...
@@ -21,9 +21,10 @@ from paddle.fluid.dygraph.nn import Embedding
from
paddle.fluid.optimizer
import
SGDOptimizer
import
numpy
as
np
import
paddle.fluid.core
as
core
import
paddle
class
SimpleNet
(
fluid
.
Layer
):
class
SimpleNet
(
paddle
.
imperative
.
Layer
):
def
__init__
(
self
,
vocab_size
,
hidden_size
,
dtype
):
super
(
SimpleNet
,
self
).
__init__
()
self
.
emb
=
fluid
.
dygraph
.
Embedding
(
...
...
@@ -46,13 +47,13 @@ class TestSimpleNet(unittest.TestCase):
for
place
in
places
:
for
dtype
in
[
"float32"
,
"float64"
]:
for
sort_sum_gradient
in
[
True
,
False
]:
with
fluid
.
dygraph
.
guard
(
place
):
backward_strategy
=
fluid
.
dygraph
.
BackwardStrategy
()
with
paddle
.
imperative
.
guard
(
place
):
backward_strategy
=
paddle
.
imperative
.
BackwardStrategy
()
backward_strategy
.
sort_sum_gradient
=
sort_sum_gradient
# grad_clip = fluid.clip.GradientClipByGlobalNorm(5.0)
input_word
=
np
.
array
([[
1
,
2
],
[
2
,
1
]]).
astype
(
'int64'
)
input
=
to_variable
(
input_word
)
input
=
paddle
.
imperative
.
to_variable
(
input_word
)
simplenet
=
SimpleNet
(
20
,
32
,
dtype
)
adam
=
SGDOptimizer
(
...
...
python/paddle/imperative/__init__.py
浏览文件 @
432b5df8
...
...
@@ -12,15 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: define api used to run in imperative mode
# __all__ = ['BackwardStrategy',
# 'guard',
# 'Layer',
# 'LayerList',
# 'load_dygraph',
# 'save_dygraph',
# 'prepare_context',
# 'to_variable',
# 'TracedLayer',
# 'no_grad',
# 'ParameterList']
# define api used to run in imperative mode
__all__
=
[
'BackwardStrategy'
,
'guard'
,
'Layer'
,
'LayerList'
,
'load_dygraph'
,
'save_dygraph'
,
'prepare_context'
,
'to_variable'
,
'TracedLayer'
,
'no_grad'
,
'ParameterList'
,
'Sequential'
]
from
paddle.fluid
import
core
from
..fluid.dygraph.base
import
guard
,
no_grad
,
to_variable
from
..fluid.dygraph.layers
import
Layer
from
..fluid.dygraph.container
import
LayerList
,
ParameterList
,
Sequential
from
..fluid.dygraph.checkpoint
import
load_dygraph
,
save_dygraph
from
..fluid.dygraph.parallel
import
prepare_context
from
..fluid.dygraph.jit
import
TracedLayer
BackwardStrategy
=
core
.
BackwardStrategy
python/setup.py.in
浏览文件 @
432b5df8
...
...
@@ -148,7 +148,8 @@ packages=['paddle',
'paddle.fluid.incubate.fleet.utils',
'paddle.nn',
'paddle.nn.functional',
'paddle.nn.layer']
'paddle.nn.layer',
'paddle.imperative']
with open('@PADDLE_SOURCE_DIR@/python/requirements.txt') as f:
setup_requires = f.read().splitlines()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录