Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
afeixing77
Paddle
提交
05d2b7a3
P
Paddle
项目概览
afeixing77
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
05d2b7a3
编写于
2月 03, 2021
作者:
J
joejiong
提交者:
GitHub
2月 03, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update paddle.static.Print with paddle2.0 api (#30846)
As the title
上级
e49d0746
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
36 addition
and
35 deletion
+36
-35
python/paddle/fluid/tests/unittests/test_print_op.py
python/paddle/fluid/tests/unittests/test_print_op.py
+36
-35
未找到文件。
python/paddle/fluid/tests/unittests/test_print_op.py
100644 → 100755
浏览文件 @
05d2b7a3
...
@@ -13,25 +13,26 @@
...
@@ -13,25 +13,26 @@
# limitations under the License.
# limitations under the License.
from
__future__
import
print_function
from
__future__
import
print_function
import
unittest
import
unittest
import
paddle.fluid.core
as
core
from
paddle.fluid.executor
import
Executor
import
numpy
as
np
from
op_test
import
OpTest
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
paddle.fluid.layers
as
layers
import
paddle.fluid.layers
as
layers
from
paddle.fluid
.backward
import
append_backward
from
paddle.fluid
import
core
from
paddle.fluid.framework
import
switch_main_program
from
paddle.fluid.framework
import
switch_main_program
from
paddle.fluid.framework
import
Program
import
numpy
as
np
from
simple_nets
import
simple_fc_net
,
init_data
from
simple_nets
import
simple_fc_net
,
init_data
from
paddle.fluid
import
compiler
,
Program
,
program_guard
from
paddle.static
import
Program
,
program_guard
from
op_test
import
OpTest
paddle
.
enable_static
()
class
TestPrintOpCPU
(
unittest
.
TestCase
):
class
TestPrintOpCPU
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
place
=
cor
e
.
CPUPlace
()
self
.
place
=
paddl
e
.
CPUPlace
()
self
.
x_tensor
=
core
.
LoDTensor
()
self
.
x_tensor
=
fluid
.
core
.
LoDTensor
()
tensor_np
=
np
.
random
.
random
(
size
=
(
2
,
3
)).
astype
(
'float32'
)
tensor_np
=
np
.
random
.
random
(
size
=
(
2
,
3
)).
astype
(
'float32'
)
self
.
x_tensor
.
set
(
tensor_np
,
self
.
place
)
self
.
x_tensor
.
set
(
tensor_np
,
self
.
place
)
self
.
x_tensor
.
set_recursive_sequence_lengths
([[
1
,
1
]])
self
.
x_tensor
.
set_recursive_sequence_lengths
([[
1
,
1
]])
...
@@ -39,15 +40,15 @@ class TestPrintOpCPU(unittest.TestCase):
...
@@ -39,15 +40,15 @@ class TestPrintOpCPU(unittest.TestCase):
def
build_network
(
self
,
only_forward
,
**
kargs
):
def
build_network
(
self
,
only_forward
,
**
kargs
):
x
=
layers
.
data
(
'x'
,
shape
=
[
3
],
dtype
=
'float32'
,
lod_level
=
1
)
x
=
layers
.
data
(
'x'
,
shape
=
[
3
],
dtype
=
'float32'
,
lod_level
=
1
)
x
.
stop_gradient
=
False
x
.
stop_gradient
=
False
layers
.
Print
(
input
=
x
,
**
kargs
)
paddle
.
static
.
Print
(
input
=
x
,
**
kargs
)
loss
=
layers
.
mean
(
x
)
loss
=
paddle
.
mean
(
x
)
append_backward
(
loss
=
loss
)
paddle
.
static
.
append_backward
(
loss
=
loss
)
return
loss
return
loss
def
test_forward
(
self
):
def
test_forward
(
self
):
switch_main_program
(
Program
())
switch_main_program
(
Program
())
printed
=
self
.
build_network
(
True
,
print_phase
=
'forward'
)
printed
=
self
.
build_network
(
True
,
print_phase
=
'forward'
)
exe
=
Executor
(
self
.
place
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
outs
=
exe
.
run
(
feed
=
{
'x'
:
self
.
x_tensor
},
outs
=
exe
.
run
(
feed
=
{
'x'
:
self
.
x_tensor
},
fetch_list
=
[
printed
],
fetch_list
=
[
printed
],
return_numpy
=
False
)
return_numpy
=
False
)
...
@@ -55,7 +56,7 @@ class TestPrintOpCPU(unittest.TestCase):
...
@@ -55,7 +56,7 @@ class TestPrintOpCPU(unittest.TestCase):
def
test_backward
(
self
):
def
test_backward
(
self
):
switch_main_program
(
Program
())
switch_main_program
(
Program
())
loss
=
self
.
build_network
(
False
,
print_phase
=
'backward'
)
loss
=
self
.
build_network
(
False
,
print_phase
=
'backward'
)
exe
=
Executor
(
self
.
place
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
outs
=
exe
.
run
(
feed
=
{
'x'
:
self
.
x_tensor
},
outs
=
exe
.
run
(
feed
=
{
'x'
:
self
.
x_tensor
},
fetch_list
=
[
loss
],
fetch_list
=
[
loss
],
return_numpy
=
False
)
return_numpy
=
False
)
...
@@ -68,15 +69,15 @@ class TestPrintOpCPU(unittest.TestCase):
...
@@ -68,15 +69,15 @@ class TestPrintOpCPU(unittest.TestCase):
for
print_tensor_type
in
[
True
,
False
]:
for
print_tensor_type
in
[
True
,
False
]:
for
print_tensor_shape
in
[
True
,
False
]:
for
print_tensor_shape
in
[
True
,
False
]:
for
print_tensor_lod
in
[
True
,
False
]:
for
print_tensor_lod
in
[
True
,
False
]:
layers
.
Print
(
paddle
.
static
.
Print
(
input
=
x
,
input
=
x
,
print_tensor_name
=
print_tensor_name
,
print_tensor_name
=
print_tensor_name
,
print_tensor_type
=
print_tensor_type
,
print_tensor_type
=
print_tensor_type
,
print_tensor_shape
=
print_tensor_shape
,
print_tensor_shape
=
print_tensor_shape
,
print_tensor_lod
=
print_tensor_lod
,
)
print_tensor_lod
=
print_tensor_lod
,
)
loss
=
layers
.
mean
(
x
)
loss
=
paddle
.
mean
(
x
)
append_backward
(
loss
=
loss
)
paddle
.
static
.
append_backward
(
loss
=
loss
)
exe
=
Executor
(
self
.
place
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
outs
=
exe
.
run
(
feed
=
{
'x'
:
self
.
x_tensor
},
outs
=
exe
.
run
(
feed
=
{
'x'
:
self
.
x_tensor
},
fetch_list
=
[
loss
],
fetch_list
=
[
loss
],
return_numpy
=
False
)
return_numpy
=
False
)
...
@@ -84,7 +85,7 @@ class TestPrintOpCPU(unittest.TestCase):
...
@@ -84,7 +85,7 @@ class TestPrintOpCPU(unittest.TestCase):
def
test_no_summarize
(
self
):
def
test_no_summarize
(
self
):
switch_main_program
(
Program
())
switch_main_program
(
Program
())
printed
=
self
.
build_network
(
True
,
summarize
=-
1
,
print_phase
=
'forward'
)
printed
=
self
.
build_network
(
True
,
summarize
=-
1
,
print_phase
=
'forward'
)
exe
=
Executor
(
self
.
place
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
outs
=
exe
.
run
(
feed
=
{
'x'
:
self
.
x_tensor
},
outs
=
exe
.
run
(
feed
=
{
'x'
:
self
.
x_tensor
},
fetch_list
=
[
printed
],
fetch_list
=
[
printed
],
return_numpy
=
False
)
return_numpy
=
False
)
...
@@ -95,19 +96,19 @@ class TestPrintOpError(unittest.TestCase):
...
@@ -95,19 +96,19 @@ class TestPrintOpError(unittest.TestCase):
with
program_guard
(
Program
(),
Program
()):
with
program_guard
(
Program
(),
Program
()):
# The input type of Print_op must be Variable.
# The input type of Print_op must be Variable.
x1
=
fluid
.
create_lod_tensor
(
x1
=
fluid
.
create_lod_tensor
(
np
.
array
([[
-
1
]]),
[[
1
]],
fluid
.
CPUPlace
())
np
.
array
([[
-
1
]]),
[[
1
]],
paddle
.
CPUPlace
())
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
Print
,
x1
)
self
.
assertRaises
(
TypeError
,
paddle
.
static
.
Print
,
x1
)
# The input dtype of Print_op must be float32, float64, int32_t, int64_t or bool.
# The input dtype of Print_op must be float32, float64, int32_t, int64_t or bool.
x2
=
fluid
.
layers
.
data
(
name
=
'x2'
,
shape
=
[
4
],
dtype
=
"float16"
)
x2
=
paddle
.
static
.
data
(
name
=
'x2'
,
shape
=
[
4
],
dtype
=
"float16"
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
Print
,
x2
)
self
.
assertRaises
(
TypeError
,
paddle
.
static
.
Print
,
x2
)
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_cuda
(),
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_cuda
(),
"core is not compiled with CUDA"
)
"core is not compiled with CUDA"
)
class
TestPrintOpGPU
(
TestPrintOpCPU
):
class
TestPrintOpGPU
(
TestPrintOpCPU
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
place
=
cor
e
.
CUDAPlace
(
0
)
self
.
place
=
paddl
e
.
CUDAPlace
(
0
)
self
.
x_tensor
=
core
.
LoDTensor
()
self
.
x_tensor
=
fluid
.
core
.
LoDTensor
()
tensor_np
=
np
.
random
.
random
(
size
=
(
2
,
3
)).
astype
(
'float32'
)
tensor_np
=
np
.
random
.
random
(
size
=
(
2
,
3
)).
astype
(
'float32'
)
self
.
x_tensor
.
set
(
tensor_np
,
self
.
place
)
self
.
x_tensor
.
set
(
tensor_np
,
self
.
place
)
self
.
x_tensor
.
set_recursive_sequence_lengths
([[
1
,
1
]])
self
.
x_tensor
.
set_recursive_sequence_lengths
([[
1
,
1
]])
...
@@ -115,22 +116,22 @@ class TestPrintOpGPU(TestPrintOpCPU):
...
@@ -115,22 +116,22 @@ class TestPrintOpGPU(TestPrintOpCPU):
class
TestPrintOpBackward
(
unittest
.
TestCase
):
class
TestPrintOpBackward
(
unittest
.
TestCase
):
def
check_backward
(
self
,
use_cuda
):
def
check_backward
(
self
,
use_cuda
):
main
=
fluid
.
Program
()
main
=
paddle
.
static
.
Program
()
startup
=
fluid
.
Program
()
startup
=
paddle
.
static
.
Program
()
with
fluid
.
program_guard
(
main
,
startup
):
with
program_guard
(
main
,
startup
):
loss
=
simple_fc_net
()
loss
=
simple_fc_net
()
loss
=
fluid
.
layers
.
Print
(
loss
)
loss
=
paddle
.
static
.
Print
(
loss
)
fluid
.
optimizer
.
Adam
().
minimize
(
loss
)
paddle
.
optimizer
.
Adam
().
minimize
(
loss
)
print_ops
=
[
op
for
op
in
main
.
blocks
[
0
].
ops
if
op
.
type
==
u
'print'
]
print_ops
=
[
op
for
op
in
main
.
blocks
[
0
].
ops
if
op
.
type
==
u
'print'
]
assert
len
(
print_ops
)
==
2
,
"The number of print op should be 2"
assert
len
(
print_ops
)
==
2
,
"The number of print op should be 2"
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
place
=
paddle
.
CUDAPlace
(
0
)
if
use_cuda
else
paddle
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup
)
exe
.
run
(
startup
)
binary
=
fluid
.
compiler
.
CompiledProgram
(
main
).
with_data_parallel
(
binary
=
paddle
.
static
.
CompiledProgram
(
main
).
with_data_parallel
(
loss_name
=
loss
.
name
)
loss_name
=
loss
.
name
)
img
,
label
=
init_data
()
img
,
label
=
init_data
()
...
@@ -138,7 +139,7 @@ class TestPrintOpBackward(unittest.TestCase):
...
@@ -138,7 +139,7 @@ class TestPrintOpBackward(unittest.TestCase):
exe
.
run
(
binary
,
feed_dict
)
exe
.
run
(
binary
,
feed_dict
)
def
test_fw_bw
(
self
):
def
test_fw_bw
(
self
):
if
cor
e
.
is_compiled_with_cuda
():
if
paddl
e
.
is_compiled_with_cuda
():
self
.
check_backward
(
use_cuda
=
True
)
self
.
check_backward
(
use_cuda
=
True
)
self
.
check_backward
(
use_cuda
=
False
)
self
.
check_backward
(
use_cuda
=
False
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录