Skip to content

Commit d3c9db7

Browse files
authored
copy api from paddle to paddle.fluid (#24164)
* copy api from paddle to paddle.fluid, test=develop * fix optest, test=develop
1 parent a4519a5 commit d3c9db7

21 files changed

+1827
-315
lines changed

python/paddle/fluid/dygraph/nn.py

Lines changed: 561 additions & 1 deletion
Large diffs are not rendered by default.

python/paddle/fluid/layers/nn.py

Lines changed: 712 additions & 149 deletions
Large diffs are not rendered by default.

python/paddle/fluid/layers/tensor.py

Lines changed: 414 additions & 28 deletions
Large diffs are not rendered by default.

python/paddle/fluid/tests/unittests/test_allclose_layer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def allclose_check(self, use_cuda):
2323
a = fluid.data(name="a", shape=[2], dtype='float32')
2424
b = fluid.data(name="b", shape=[2], dtype='float32')
2525

26-
result = paddle.allclose(
26+
result = fluid.layers.allclose(
2727
a, b, rtol=1e-05, atol=1e-08, equal_nan=False, name="ignore_nan")
28-
result_nan = paddle.allclose(
28+
result_nan = fluid.layers.allclose(
2929
a, b, rtol=1e-05, atol=1e-08, equal_nan=True, name="equal_nan")
3030

3131
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
@@ -82,15 +82,15 @@ def test_dygraph_mode(self):
8282
with fluid.dygraph.guard():
8383
x_v_1 = fluid.dygraph.to_variable(x_1)
8484
y_v_1 = fluid.dygraph.to_variable(y_1)
85-
ret_1 = paddle.allclose(
85+
ret_1 = fluid.layers.allclose(
8686
x_v_1,
8787
y_v_1,
8888
rtol=1e-05,
8989
atol=1e-08,
9090
equal_nan=False,
9191
name='test_1')
9292
self.assertEqual(ret_1.numpy()[0], False)
93-
ret_1 = paddle.allclose(
93+
ret_1 = fluid.layers.allclose(
9494
x_v_1,
9595
y_v_1,
9696
rtol=1e-05,
@@ -100,15 +100,15 @@ def test_dygraph_mode(self):
100100
self.assertEqual(ret_1.numpy()[0], False)
101101
x_v_2 = fluid.dygraph.to_variable(x_2)
102102
y_v_2 = fluid.dygraph.to_variable(y_2)
103-
ret_2 = paddle.allclose(
103+
ret_2 = fluid.layers.allclose(
104104
x_v_2,
105105
y_v_2,
106106
rtol=1e-05,
107107
atol=1e-08,
108108
equal_nan=False,
109109
name='test_3')
110110
self.assertEqual(ret_2.numpy()[0], True)
111-
ret_2 = paddle.allclose(
111+
ret_2 = fluid.layers.allclose(
112112
x_v_2,
113113
y_v_2,
114114
rtol=1e-05,
@@ -118,15 +118,15 @@ def test_dygraph_mode(self):
118118
self.assertEqual(ret_2.numpy()[0], True)
119119
x_v_3 = fluid.dygraph.to_variable(x_3)
120120
y_v_3 = fluid.dygraph.to_variable(y_3)
121-
ret_3 = paddle.allclose(
121+
ret_3 = fluid.layers.allclose(
122122
x_v_3,
123123
y_v_3,
124124
rtol=1e-05,
125125
atol=1e-08,
126126
equal_nan=False,
127127
name='test_5')
128128
self.assertEqual(ret_3.numpy()[0], False)
129-
ret_3 = paddle.allclose(
129+
ret_3 = fluid.layers.allclose(
130130
x_v_3,
131131
y_v_3,
132132
rtol=1e-05,

python/paddle/fluid/tests/unittests/test_arange.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from __future__ import print_function
1616

17-
import paddle
1817
import paddle.fluid as fluid
1918
import unittest
2019
import numpy as np
@@ -71,15 +70,15 @@ def init_config(self):
7170
class TestArangeAPI(unittest.TestCase):
7271
def test_out(self):
7372
with fluid.program_guard(fluid.Program()):
74-
data = paddle.arange(0, 5, 1)
73+
data = fluid.layers.arange(0, 5, 1)
7574
place = fluid.CPUPlace()
7675
exe = fluid.Executor(place)
7776
result, = exe.run(fetch_list=[data])
7877
expected_data = np.arange(0, 5, 1).astype(np.float32)
7978
self.assertEqual((result == expected_data).all(), True)
8079

8180
with fluid.program_guard(fluid.Program()):
82-
data = paddle.arange(0.0, 5.0, 1.0, 'int32')
81+
data = fluid.layers.arange(0.0, 5.0, 1.0, 'int32')
8382
place = fluid.CPUPlace()
8483
exe = fluid.Executor(place)
8584
result, = exe.run(fetch_list=[data])

python/paddle/fluid/tests/unittests/test_bce_loss.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_BCELoss(self):
3636
name='input', shape=[None, 30], dtype='float64')
3737
label = fluid.data(
3838
name='label', shape=[None, 30], dtype='float64')
39-
bce_loss = paddle.nn.loss.BCELoss(reduction=red)
39+
bce_loss = fluid.dygraph.BCELoss(reduction=red)
4040
res = bce_loss(input, label)
4141

4242
exe = fluid.Executor(place)
@@ -47,7 +47,7 @@ def test_BCELoss(self):
4747
fetch_list=[res])
4848

4949
with fluid.dygraph.guard():
50-
bce_loss = paddle.nn.loss.BCELoss(reduction=red)
50+
bce_loss = fluid.dygraph.BCELoss(reduction=red)
5151
dy_res = bce_loss(
5252
fluid.dygraph.to_variable(input_np),
5353
fluid.dygraph.to_variable(label_np))
@@ -80,7 +80,7 @@ def test_BCELoss_weight(self):
8080
name='label', shape=[None, 3, 4, 10], dtype='float64')
8181
weight = fluid.data(
8282
name='weight', shape=[3, 4, 10], dtype='float64')
83-
bce_loss = paddle.nn.loss.BCELoss(weight=weight)
83+
bce_loss = fluid.dygraph.BCELoss(weight=weight)
8484
res = bce_loss(input, label)
8585

8686
exe = fluid.Executor(place)
@@ -93,7 +93,7 @@ def test_BCELoss_weight(self):
9393
fetch_list=[res])
9494

9595
with fluid.dygraph.guard():
96-
bce_loss = paddle.nn.loss.BCELoss(
96+
bce_loss = fluid.dygraph.BCELoss(
9797
weight=fluid.dygraph.to_variable(weight_np))
9898
dy_res = bce_loss(
9999
fluid.dygraph.to_variable(input_np),

python/paddle/fluid/tests/unittests/test_compare_op.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_api(self):
8282
with fluid.program_guard(fluid.Program(), fluid.Program()):
8383
label = fluid.layers.assign(np.array([3, 3], dtype="int32"))
8484
limit = fluid.layers.assign(np.array([3, 2], dtype="int32"))
85-
out = paddle.elementwise_equal(x=label, y=limit)
85+
out = fluid.layers.elementwise_equal(x=label, y=limit)
8686
place = fluid.CPUPlace()
8787
exe = fluid.Executor(place)
8888
res, = exe.run(fetch_list=[out])
@@ -91,7 +91,7 @@ def test_api(self):
9191
with fluid.program_guard(fluid.Program(), fluid.Program()):
9292
label = fluid.layers.assign(np.array([3, 3], dtype="int32"))
9393
limit = fluid.layers.assign(np.array([3, 3], dtype="int32"))
94-
out = paddle.elementwise_equal(x=label, y=limit)
94+
out = fluid.layers.elementwise_equal(x=label, y=limit)
9595
place = fluid.CPUPlace()
9696
exe = fluid.Executor(place)
9797
res, = exe.run(fetch_list=[out])

python/paddle/fluid/tests/unittests/test_cross_entropy_loss.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_cross_entropy_loss_mean(self):
3535
label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64')
3636
weight = fluid.layers.data(
3737
name='weight', shape=[100], dtype='float32')
38-
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(weight=weight)
38+
cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(weight=weight)
3939
ret = cross_entropy_loss(input, label)
4040

4141
exe = fluid.Executor(place)
@@ -48,7 +48,7 @@ def test_cross_entropy_loss_mean(self):
4848
fetch_list=[ret])
4949
self.assertIsNotNone(static_ret)
5050
with fluid.dygraph.guard():
51-
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(
51+
cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
5252
weight=fluid.dygraph.to_variable(weight_np))
5353
dy_ret = cross_entropy_loss(
5454
fluid.dygraph.to_variable(input_np),
@@ -71,7 +71,7 @@ def test_cross_entropy_loss_sum(self):
7171
label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64')
7272
weight = fluid.layers.data(
7373
name='weight', shape=[100], dtype='float32')
74-
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(
74+
cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
7575
weight=weight, reduction='sum')
7676
ret = cross_entropy_loss(input, label)
7777

@@ -85,7 +85,7 @@ def test_cross_entropy_loss_sum(self):
8585
fetch_list=[ret])
8686
self.assertIsNotNone(static_ret)
8787
with fluid.dygraph.guard():
88-
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(
88+
cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
8989
weight=fluid.dygraph.to_variable(weight_np), reduction='sum')
9090
dy_ret = cross_entropy_loss(
9191
fluid.dygraph.to_variable(input_np),
@@ -108,7 +108,7 @@ def test_cross_entropy_loss_none(self):
108108
label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64')
109109
weight = fluid.layers.data(
110110
name='weight', shape=[100], dtype='float32')
111-
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(
111+
cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
112112
weight=weight, reduction='none')
113113
ret = cross_entropy_loss(input, label)
114114

@@ -122,7 +122,7 @@ def test_cross_entropy_loss_none(self):
122122
fetch_list=[ret])
123123
self.assertIsNotNone(static_ret)
124124
with fluid.dygraph.guard():
125-
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(
125+
cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
126126
weight=fluid.dygraph.to_variable(weight_np), reduction='none')
127127
dy_ret = cross_entropy_loss(
128128
fluid.dygraph.to_variable(input_np),

python/paddle/fluid/tests/unittests/test_fill_any_like_op.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_attr_tensor_API(self):
106106
with fluid.program_guard(train_program, startup_program):
107107
fill_value = 2.0
108108
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
109-
output = paddle.full_like(input, fill_value)
109+
output = fluid.layers.full_like(input, fill_value)
110110

111111
place = fluid.CPUPlace()
112112
if fluid.core.is_compiled_with_cuda():
@@ -132,20 +132,20 @@ def test_errors(self):
132132
#for ci coverage
133133

134134
input_data = fluid.data(name='input', dtype='float32', shape=[2, 3])
135-
output = paddle.full_like(input_data, 2.0)
135+
output = fluid.layers.full_like(input_data, 2.0)
136136

137137
def test_input_dtype():
138-
paddle.full_like
138+
fluid.layers.full_like
139139

140140
self.assertRaises(
141141
ValueError,
142-
paddle.full_like,
142+
fluid.layers.full_like,
143143
input=input_data,
144144
fill_value=2,
145145
dtype='uint4')
146146
self.assertRaises(
147147
TypeError,
148-
paddle.full_like,
148+
fluid.layers.full_like,
149149
input=input_data,
150150
fill_value=2,
151151
dtype='int16')

python/paddle/fluid/tests/unittests/test_flip.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import unittest
1818
import numpy as np
19-
import paddle
2019
import paddle.fluid as fluid
2120
import paddle.fluid.core as core
2221
from paddle.fluid import Program, program_guard
@@ -32,7 +31,7 @@ def test_static_graph(self):
3231
with fluid.program_guard(train_program, startup_program):
3332
dims = [0]
3433
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
35-
output = paddle.flip(input, dims)
34+
output = fluid.layers.flip(input, dims)
3635
place = fluid.CPUPlace()
3736
if fluid.core.is_compiled_with_cuda():
3837
place = fluid.CUDAPlace(0)
@@ -52,7 +51,7 @@ def test_dygraph(self):
5251
img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
5352
with fluid.dygraph.guard():
5453
inputs = fluid.dygraph.to_variable(img)
55-
ret = paddle.flip(inputs, [0])
54+
ret = fluid.layers.flip(inputs, [0])
5655
out_ref = np.array([[4, 5, 6], [1, 2, 3]]).astype(np.float32)
5756
self.assertTrue(
5857
(ret.numpy() == out_ref).all(),

0 commit comments

Comments
 (0)