Skip to content

Commit fa79270

Browse files
authored
replace cross_entropy in python/paddle/fluid/tests/unittests/*.py (PaddlePaddle#48975)
1 parent 9735b82 commit fa79270

19 files changed

+49
-33
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def func(self, place):
186186

187187
x = layers.data('x', shape, False, dtype)
188188
x.persistable = True
189-
y = layers.relu(x)
189+
y = F.relu(x)
190190
x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
191191
x_arr[np.abs(x_arr) < 0.005] = 0.02
192192

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import paddle
2020
import paddle.fluid as fluid
21+
import paddle.nn.functional as F
2122
import paddle.static as static
2223

2324

@@ -285,7 +286,7 @@ def test_error(self):
285286
x = fluid.data(name='x', shape=[None, 2, 8, 8], dtype='float32')
286287
x.stop_gradient = False
287288
conv = fluid.layers.conv2d(x, 4, 1, bias_attr=False)
288-
y = fluid.layers.relu(conv)
289+
y = F.relu(conv)
289290

290291
with self.assertRaises(TypeError):
291292
x_grad = fluid.gradients(y.name, x)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import paddle
2020
import paddle.fluid as fluid
21+
import paddle.nn.functional as F
2122
from paddle.fluid import core
2223

2324
paddle.enable_static()
@@ -160,7 +161,7 @@ def build_origin_program(
160161
data_layout='NHWC',
161162
)
162163
out = bn1 + bn2
163-
out = fluid.layers.relu(out)
164+
out = F.relu(out)
164165
prediction = fluid.layers.fc(
165166
input=out, size=10, act='softmax', param_attr=self.fc_param_attr
166167
)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import paddle
2323
import paddle.fluid as fluid
2424
import paddle.fluid.core as core
25+
import paddle.nn.functional as F
2526

2627

2728
class TestMNIST(TestParallelExecutorBase):
@@ -97,7 +98,7 @@ def build_program(self, main_program, startup_program):
9798
X = fluid.data(name="X", shape=[3, 3], dtype='float32')
9899
Y = fluid.data(name="Y", shape=[3, 3], dtype='float32')
99100
Out1 = X * 5
100-
Out2 = fluid.layers.relu(Out1)
101+
Out2 = F.relu(Out1)
101102
prediction = paddle.tensor.math._add_with_axis(Y, Out2, axis=1)
102103
loss = paddle.mean(prediction)
103104
sgd = fluid.optimizer.SGD(learning_rate=0.001)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import paddle
2121
import paddle.fluid as fluid
2222
import paddle.fluid.core as core
23+
import paddle.nn.functional as F
2324

2425

2526
def norm(*args, **kargs):
@@ -59,7 +60,7 @@ def simple_depthwise_net(use_feed):
5960
hidden = paddle.reshape(img, (-1, 1, 28, 28))
6061
for _ in range(4):
6162
hidden = sep_conv(hidden, channel=200, stride=2, filter=5)
62-
hidden = fluid.layers.relu(hidden)
63+
hidden = F.relu(hidden)
6364
prediction = fluid.layers.fc(hidden, size=10, act='softmax')
6465
loss = paddle.nn.functional.cross_entropy(
6566
input=prediction, label=label, reduction='none', use_softmax=False

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import numpy as np
1818

1919
import paddle.fluid as fluid
20+
import paddle.nn.functional as F
2021
from paddle.fluid.reader import use_pinned_memory
2122

2223

@@ -45,7 +46,7 @@ def setUp(self):
4546
def iter_loader_data(self, loader):
4647
for _ in range(self.epoch_num):
4748
for image, label in loader():
48-
relu = fluid.layers.relu(image)
49+
relu = F.relu(image)
4950
self.assertEqual(image.shape, [self.batch_size, 784])
5051
self.assertEqual(label.shape, [self.batch_size, 1])
5152
self.assertEqual(relu.shape, [self.batch_size, 784])

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919

2020
import paddle.fluid as fluid
21+
import paddle.nn.functional as F
2122
from paddle.fluid import core
2223

2324

@@ -112,7 +113,7 @@ def __reader__():
112113
try:
113114
for _ in range(self.epoch_num):
114115
for image, _ in loader():
115-
fluid.layers.relu(image)
116+
F.relu(image)
116117
except core.EnforceNotMet as ex:
117118
self.assertIn("Blocking queue is killed", str(ex))
118119
exception = ex

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import numpy as np
1818

1919
import paddle.fluid as fluid
20+
import paddle.nn.functional as F
2021
from paddle.io import DataLoader, Dataset
2122

2223

@@ -71,7 +72,7 @@ def prepare_data_loader(self):
7172
def run_one_epoch_with_break(self, loader):
7273
for step_id, data in enumerate(loader()):
7374
image, label = data
74-
relu = fluid.layers.relu(image)
75+
relu = F.relu(image)
7576
self.assertEqual(image.shape, [self.batch_size, 784])
7677
self.assertEqual(label.shape, [self.batch_size, 1])
7778
self.assertEqual(relu.shape, [self.batch_size, 784])

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import paddle
2121
import paddle.fluid as fluid
22+
import paddle.nn.functional as F
2223
from paddle.fluid.framework import _test_eager_guard
2324
from paddle.fluid.wrapped_decorator import wrap_decorator
2425
from paddle.vision.models import resnet50, resnet101
@@ -317,8 +318,8 @@ def func_example_no_grad_vars(self):
317318
numel = x_np.size
318319
x.stop_gradient = False
319320

320-
y1 = fluid.layers.relu(x)
321-
y2 = fluid.layers.relu(x)
321+
y1 = F.relu(x)
322+
y2 = F.relu(x)
322323
z = y1 + y2
323324
w = z * z
324325

@@ -436,7 +437,7 @@ def func_example_with_gradient_accumulation_and_create_graph(self):
436437
numel = x_np.size
437438
x.stop_gradient = False
438439

439-
y = fluid.layers.relu(x)
440+
y = F.relu(x)
440441
z = y + 1
441442
w = z * z
442443

@@ -489,8 +490,8 @@ def func_example_with_gradient_accumulation_and_no_grad_vars(self):
489490
numel = x_np.size
490491
x.stop_gradient = False
491492

492-
y1 = fluid.layers.relu(x)
493-
y2 = fluid.layers.relu(x)
493+
y1 = F.relu(x)
494+
y2 = F.relu(x)
494495
z = y1 + y2
495496
w = z * z
496497

@@ -540,7 +541,7 @@ def func_example_with_gradient_accumulation_and_not_create_graph(self):
540541
numel = x_np.size
541542
x.stop_gradient = False
542543

543-
y = fluid.layers.relu(x)
544+
y = F.relu(x)
544545
z = y + 1
545546
w = z * z
546547

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import paddle
2222
import paddle.fluid as fluid
2323
import paddle.fluid.core as core
24+
import paddle.nn.functional as F
2425
from paddle.fluid.dygraph.base import to_variable
2526
from paddle.fluid.framework import _test_eager_guard
2627
from paddle.fluid.optimizer import AdamOptimizer
@@ -58,7 +59,7 @@ def __init__(self, name_scope, num_hidden):
5859
self.gc2 = GraphConv(self.full_name(), 32, 10)
5960

6061
def forward(self, x, adj):
61-
x = fluid.layers.relu(self.gc(x, adj))
62+
x = F.relu(self.gc(x, adj))
6263
return self.gc2(x, adj)
6364

6465

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import paddle
2121
import paddle.fluid as fluid
22+
import paddle.nn.functional as F
2223
from paddle.fluid import core
2324
from paddle.fluid.dygraph.base import to_variable
2425
from paddle.fluid.dygraph.parallel import (
@@ -34,7 +35,7 @@ def __init__(self, name_scope):
3435
super().__init__(name_scope)
3536

3637
def forward(self, inputs):
37-
x = fluid.layers.relu(inputs)
38+
x = F.relu(inputs)
3839
x = paddle.multiply(x, x)
3940
x = paddle.sum(x)
4041
return [x]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import paddle
2121
import paddle.fluid as fluid
22+
import paddle.nn.functional as F
2223
from paddle.fluid import core
2324
from paddle.fluid.optimizer import SGDOptimizer
2425

@@ -38,7 +39,7 @@ def forward(self, inputs):
3839
x = paddle.reshape(inputs, shape=[-1, 4])
3940
x = self.affine1(x)
4041
x = paddle.nn.functional.dropout(x, self.dropout_ratio)
41-
x = fluid.layers.relu(x)
42+
x = F.relu(x)
4243
action_scores = self.affine2(x)
4344
return paddle.nn.functional.softmax(action_scores, axis=1)
4445

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,19 +344,19 @@ def test_SyncBatchNorm(self):
344344
def test_relu(self):
345345
with self.static_graph():
346346
t = layers.data(name='t', shape=[3, 3], dtype='float32')
347-
ret = layers.relu(t)
347+
ret = F.relu(t)
348348
static_ret = self.get_static_graph_result(
349349
feed={'t': np.ones([3, 3], dtype='float32')}, fetch_list=[ret]
350350
)[0]
351351

352352
with self.dynamic_graph():
353353
with _test_eager_guard():
354354
t = np.ones([3, 3], dtype='float32')
355-
dy_eager_ret = layers.relu(base.to_variable(t))
355+
dy_eager_ret = F.relu(base.to_variable(t))
356356
dy_eager_ret_value = dy_eager_ret.numpy()
357357

358358
t = np.ones([3, 3], dtype='float32')
359-
dy_ret = layers.relu(base.to_variable(t))
359+
dy_ret = F.relu(base.to_variable(t))
360360
dy_ret_value = dy_ret.numpy()
361361

362362
np.testing.assert_allclose(static_ret, dy_ret_value, rtol=1e-05)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import paddle
2020
import paddle.fluid as fluid
21+
import paddle.nn.functional as F
2122

2223

2324
class TestMemoryReuseExcludeFeedVar(unittest.TestCase):
@@ -29,7 +30,7 @@ def main_impl(self, place):
2930
image = fluid.layers.data(
3031
name='image', shape=self.image_shape, dtype='float32'
3132
)
32-
relu_image = fluid.layers.relu(image)
33+
relu_image = F.relu(image)
3334
loss = paddle.mean(relu_image)
3435

3536
build_strategy = fluid.BuildStrategy()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import paddle
2020
import paddle.fluid as fluid
21-
import paddle.fluid.layers as layers
21+
import paddle.nn.functional as F
2222
from paddle import _legacy_C_ops
2323

2424

@@ -66,7 +66,7 @@ def test_relu(self):
6666
a = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
6767
x = fluid.dygraph.to_variable(a)
6868

69-
res1 = layers.relu(x)
69+
res1 = F.relu(x)
7070
res2 = _legacy_C_ops.relu(x)
7171

7272
np.testing.assert_array_equal(res1.numpy(), res2.numpy())

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import paddle
2121
import paddle.fluid as fluid
22+
import paddle.nn.functional as F
2223
from paddle.fluid.framework import _in_legacy_dygraph
2324
from paddle.fluid.wrapped_decorator import wrap_decorator
2425

@@ -220,7 +221,7 @@ def func_example_with_gradient_accumulation_and_create_graph(self):
220221
numel = x_np.size
221222
x.stop_gradient = False
222223

223-
y = fluid.layers.relu(x)
224+
y = F.relu(x)
224225
z = y + 1
225226
w = z * z
226227

@@ -261,8 +262,8 @@ def test_example_with_gradient_accumulation_and_no_grad_vars(self):
261262
numel = x_np.size
262263
x.stop_gradient = False
263264

264-
y1 = fluid.layers.relu(x)
265-
y2 = fluid.layers.relu(x)
265+
y1 = F.relu(x)
266+
y2 = F.relu(x)
266267
z = y1 + y2
267268
w = z * z
268269

@@ -308,7 +309,7 @@ def test_example_with_gradient_accumulation_and_not_create_graph(self):
308309
numel = x_np.size
309310
x.stop_gradient = False
310311

311-
y = fluid.layers.relu(x)
312+
y = F.relu(x)
312313
z = y + 1
313314
w = z * z
314315

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import paddle
2020
import paddle.fluid as fluid
21+
import paddle.nn.functional as F
2122

2223

2324
class TestInferencePartialFeed(unittest.TestCase):
@@ -38,9 +39,9 @@ def run_network(self, places, use_split, has_persistable):
3839
else:
3940
lr = fluid.data(name='lr', shape=[None], dtype='float32')
4041

41-
relu_x = fluid.layers.relu(x)
42-
relu_y = fluid.layers.relu(y)
43-
relu_lr = fluid.layers.relu(lr)
42+
relu_x = F.relu(x)
43+
relu_y = F.relu(y)
44+
relu_lr = F.relu(lr)
4445

4546
exe = fluid.Executor(places[0])
4647
exe.run(startup_prog)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import paddle
2121
import paddle.fluid as fluid
2222
import paddle.fluid.core as core
23+
import paddle.nn.functional as F
2324
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
2425

2526

@@ -653,7 +654,7 @@ def func_test_backward(self):
653654
with fluid.dygraph.guard():
654655
var = fluid.dygraph.to_variable(self.array)
655656
var.stop_gradient = False
656-
loss = fluid.layers.relu(var)
657+
loss = F.relu(var)
657658
loss.backward()
658659
grad_var = var._grad_ivar()
659660
self.assertEqual(grad_var.shape, self.shape)
@@ -667,7 +668,7 @@ def func_test_gradient(self):
667668
with fluid.dygraph.guard():
668669
var = fluid.dygraph.to_variable(self.array)
669670
var.stop_gradient = False
670-
loss = fluid.layers.relu(var)
671+
loss = F.relu(var)
671672
loss.backward()
672673
grad_var = var.gradient()
673674
self.assertEqual(grad_var.shape, self.array.shape)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import paddle.fluid as fluid
2121
import paddle.fluid.core as core
2222
import paddle.fluid.layers as layers
23+
import paddle.nn.functional as F
2324
from paddle.fluid.backward import append_backward
2425
from paddle.fluid.framework import Program, program_guard
2526

@@ -96,7 +97,7 @@ def body(i, ten, test_dict, test_list, test_list_dict):
9697
test_list[0] = paddle.reshape(test_list[0], [2, -1]) + 1
9798

9899
test_list_dict[0]["test_key"] += 1
99-
test_list_dict[0]["test_key"] = fluid.layers.relu(
100+
test_list_dict[0]["test_key"] = F.relu(
100101
test_list_dict[0]["test_key"]
101102
)
102103

0 commit comments

Comments
 (0)