From 13b8a90a42f9b275e4263377b377b7f8f27634e4 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Fri, 9 Dec 2022 11:33:11 +0000 Subject: [PATCH] replace cross_entropy in python/paddle/fluid/tests/unittests/*.py --- .../tests/unittests/test_activation_nn_grad.py | 2 +- .../paddle/fluid/tests/unittests/test_backward.py | 3 ++- .../tests/unittests/test_fuse_bn_add_act_pass.py | 3 ++- .../unittests/test_fuse_elewise_add_act_pass.py | 3 ++- .../unittests/test_fuse_relu_depthwise_conv_pass.py | 3 ++- .../unittests/test_imperative_data_loader_base.py | 3 ++- .../test_imperative_data_loader_exception.py | 3 ++- .../test_imperative_data_loader_fds_clear.py | 3 ++- .../tests/unittests/test_imperative_double_grad.py | 13 +++++++------ .../fluid/tests/unittests/test_imperative_gnn.py | 3 ++- .../test_imperative_parallel_coalesce_split.py | 3 ++- .../unittests/test_imperative_reinforcement.py | 3 ++- python/paddle/fluid/tests/unittests/test_layers.py | 6 +++--- .../unittests/test_memory_reuse_exclude_feed_var.py | 3 ++- .../tests/unittests/test_op_function_generator.py | 4 ++-- .../unittests/test_paddle_imperative_double_grad.py | 9 +++++---- ...parallel_executor_inference_feed_partial_data.py | 7 ++++--- .../paddle/fluid/tests/unittests/test_var_base.py | 5 +++-- .../fluid/tests/unittests/test_while_loop_op.py | 3 ++- 19 files changed, 49 insertions(+), 33 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_activation_nn_grad.py b/python/paddle/fluid/tests/unittests/test_activation_nn_grad.py index f10232cf02bce..4e4d6a08bc3bd 100644 --- a/python/paddle/fluid/tests/unittests/test_activation_nn_grad.py +++ b/python/paddle/fluid/tests/unittests/test_activation_nn_grad.py @@ -186,7 +186,7 @@ def func(self, place): x = layers.data('x', shape, False, dtype) x.persistable = True - y = layers.relu(x) + y = F.relu(x) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.02 diff --git a/python/paddle/fluid/tests/unittests/test_backward.py b/python/paddle/fluid/tests/unittests/test_backward.py index a24d49b5ead06..2409e7a1ad5fb 100644 --- a/python/paddle/fluid/tests/unittests/test_backward.py +++ b/python/paddle/fluid/tests/unittests/test_backward.py @@ -18,6 +18,7 @@ import paddle import paddle.fluid as fluid +import paddle.nn.functional as F import paddle.static as static @@ -285,7 +286,7 @@ def test_error(self): x = fluid.data(name='x', shape=[None, 2, 8, 8], dtype='float32') x.stop_gradient = False conv = fluid.layers.conv2d(x, 4, 1, bias_attr=False) - y = fluid.layers.relu(conv) + y = F.relu(conv) with self.assertRaises(TypeError): x_grad = fluid.gradients(y.name, x) diff --git a/python/paddle/fluid/tests/unittests/test_fuse_bn_add_act_pass.py b/python/paddle/fluid/tests/unittests/test_fuse_bn_add_act_pass.py index c644391eeea8a..503433df1373f 100644 --- a/python/paddle/fluid/tests/unittests/test_fuse_bn_add_act_pass.py +++ b/python/paddle/fluid/tests/unittests/test_fuse_bn_add_act_pass.py @@ -18,6 +18,7 @@ import paddle import paddle.fluid as fluid +import paddle.nn.functional as F from paddle.fluid import core paddle.enable_static() @@ -160,7 +161,7 @@ def build_origin_program( data_layout='NHWC', ) out = bn1 + bn2 - out = fluid.layers.relu(out) + out = F.relu(out) prediction = fluid.layers.fc( input=out, size=10, act='softmax', param_attr=self.fc_param_attr ) diff --git a/python/paddle/fluid/tests/unittests/test_fuse_elewise_add_act_pass.py b/python/paddle/fluid/tests/unittests/test_fuse_elewise_add_act_pass.py index b840fa205dc95..e8743f93d0ee9 100644 --- a/python/paddle/fluid/tests/unittests/test_fuse_elewise_add_act_pass.py +++ b/python/paddle/fluid/tests/unittests/test_fuse_elewise_add_act_pass.py @@ -22,6 +22,7 @@ import paddle import paddle.fluid as fluid import paddle.fluid.core as core +import paddle.nn.functional as F class TestMNIST(TestParallelExecutorBase): @@ -97,7 +98,7 @@ def build_program(self, main_program, startup_program): X = fluid.data(name="X", shape=[3, 3], dtype='float32') Y = fluid.data(name="Y", shape=[3, 3], dtype='float32') Out1 = X * 5 - Out2 = fluid.layers.relu(Out1) + Out2 = F.relu(Out1) prediction = fluid.layers.elementwise_add(Y, Out2, axis=1) loss = paddle.mean(prediction) sgd = fluid.optimizer.SGD(learning_rate=0.001) diff --git a/python/paddle/fluid/tests/unittests/test_fuse_relu_depthwise_conv_pass.py b/python/paddle/fluid/tests/unittests/test_fuse_relu_depthwise_conv_pass.py index bb2b22ff18e5d..649b61af7b578 100644 --- a/python/paddle/fluid/tests/unittests/test_fuse_relu_depthwise_conv_pass.py +++ b/python/paddle/fluid/tests/unittests/test_fuse_relu_depthwise_conv_pass.py @@ -20,6 +20,7 @@ import paddle import paddle.fluid as fluid import paddle.fluid.core as core +import paddle.nn.functional as F def norm(*args, **kargs): @@ -59,7 +60,7 @@ def simple_depthwise_net(use_feed): hidden = paddle.reshape(img, (-1, 1, 28, 28)) for _ in range(4): hidden = sep_conv(hidden, channel=200, stride=2, filter=5) - hidden = fluid.layers.relu(hidden) + hidden = F.relu(hidden) prediction = fluid.layers.fc(hidden, size=10, act='softmax') loss = paddle.nn.functional.cross_entropy( input=prediction, label=label, reduction='none', use_softmax=False diff --git a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_base.py b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_base.py index 5a8b8ca532cec..1ecdd7c9248e0 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_base.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_base.py @@ -17,6 +17,7 @@ import numpy as np import paddle.fluid as fluid +import paddle.nn.functional as F from paddle.fluid.framework import _test_eager_guard from paddle.fluid.reader import use_pinned_memory @@ -46,7 +47,7 @@ def setUp(self): def iter_loader_data(self, loader): for _ in range(self.epoch_num): for image, label in loader(): - relu = fluid.layers.relu(image) + relu = F.relu(image) self.assertEqual(image.shape, [self.batch_size, 784]) self.assertEqual(label.shape, [self.batch_size, 1]) self.assertEqual(relu.shape, [self.batch_size, 784]) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exception.py b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exception.py index d7f6240c872c1..e0f01c4c81a01 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exception.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exception.py @@ -18,6 +18,7 @@ import numpy as np import paddle.fluid as fluid +import paddle.nn.functional as F from paddle.fluid import core from paddle.fluid.framework import _test_eager_guard @@ -128,7 +129,7 @@ def __reader__(): try: for _ in range(self.epoch_num): for image, _ in loader(): - fluid.layers.relu(image) + F.relu(image) except core.EnforceNotMet as ex: self.assertIn("Blocking queue is killed", str(ex)) exception = ex diff --git a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_fds_clear.py b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_fds_clear.py index 2bff5c9e3384b..368d46d3d82b1 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_fds_clear.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_fds_clear.py @@ -17,6 +17,7 @@ import numpy as np import paddle.fluid as fluid +import paddle.nn.functional as F from paddle.fluid.framework import _test_eager_guard from paddle.io import DataLoader, Dataset @@ -72,7 +73,7 @@ def prepare_data_loader(self): def run_one_epoch_with_break(self, loader): for step_id, data in enumerate(loader()): image, label = data - relu = fluid.layers.relu(image) + relu = F.relu(image) self.assertEqual(image.shape, [self.batch_size, 784]) self.assertEqual(label.shape, [self.batch_size, 1]) self.assertEqual(relu.shape, [self.batch_size, 784]) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py b/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py index 39927e0a2da74..495940d97888e 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py @@ -19,6 +19,7 @@ import paddle import paddle.fluid as fluid +import paddle.nn.functional as F from paddle.fluid.framework import _test_eager_guard from paddle.fluid.wrapped_decorator import wrap_decorator from paddle.vision.models import resnet50, resnet101 @@ -317,8 +318,8 @@ def func_example_no_grad_vars(self): numel = x_np.size x.stop_gradient = False - y1 = fluid.layers.relu(x) - y2 = fluid.layers.relu(x) + y1 = F.relu(x) + y2 = F.relu(x) z = y1 + y2 w = z * z @@ -436,7 +437,7 @@ def func_example_with_gradient_accumulation_and_create_graph(self): numel = x_np.size x.stop_gradient = False - y = fluid.layers.relu(x) + y = F.relu(x) z = y + 1 w = z * z @@ -489,8 +490,8 @@ def func_example_with_gradient_accumulation_and_no_grad_vars(self): numel = x_np.size x.stop_gradient = False - y1 = fluid.layers.relu(x) - y2 = fluid.layers.relu(x) + y1 = F.relu(x) + y2 = F.relu(x) z = y1 + y2 w = z * z @@ -540,7 +541,7 @@ def func_example_with_gradient_accumulation_and_not_create_graph(self): numel = x_np.size x.stop_gradient = False - y = fluid.layers.relu(x) + y = F.relu(x) z = y + 1 w = z * z diff --git a/python/paddle/fluid/tests/unittests/test_imperative_gnn.py b/python/paddle/fluid/tests/unittests/test_imperative_gnn.py index db750a5aa11f1..bce19cc20c9e3 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_gnn.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_gnn.py @@ -21,6 +21,7 @@ import paddle import paddle.fluid as fluid import paddle.fluid.core as core +import paddle.nn.functional as F from paddle.fluid.dygraph.base import to_variable from paddle.fluid.framework import _test_eager_guard from paddle.fluid.optimizer import AdamOptimizer @@ -58,7 +59,7 @@ def __init__(self, name_scope, num_hidden): self.gc2 = GraphConv(self.full_name(), 32, 10) def forward(self, x, adj): - x = fluid.layers.relu(self.gc(x, adj)) + x = F.relu(self.gc(x, adj)) return self.gc2(x, adj) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_parallel_coalesce_split.py b/python/paddle/fluid/tests/unittests/test_imperative_parallel_coalesce_split.py index 2482359d745c2..fdfe2e0c082e8 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_parallel_coalesce_split.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_parallel_coalesce_split.py @@ -19,6 +19,7 @@ import paddle import paddle.fluid as fluid +import paddle.nn.functional as F from paddle.fluid import core from paddle.fluid.dygraph.base import to_variable from paddle.fluid.dygraph.parallel import ( @@ -34,7 +35,7 @@ def __init__(self, name_scope): super().__init__(name_scope) def forward(self, inputs): - x = fluid.layers.relu(inputs) + x = F.relu(inputs) x = paddle.multiply(x, x) x = paddle.sum(x) return [x] diff --git a/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py b/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py index 06982a0fc3da9..b94e452c05e80 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py @@ -19,6 +19,7 @@ import paddle import paddle.fluid as fluid +import paddle.nn.functional as F from paddle.fluid import core from paddle.fluid.framework import _test_eager_guard from paddle.fluid.optimizer import SGDOptimizer @@ -39,7 +40,7 @@ def forward(self, inputs): x = paddle.reshape(inputs, shape=[-1, 4]) x = self.affine1(x) x = fluid.layers.dropout(x, self.dropout_ratio) - x = fluid.layers.relu(x) + x = F.relu(x) action_scores = self.affine2(x) return paddle.nn.functional.softmax(action_scores, axis=1) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 01a9c098b0e91..bb482c46f9679 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -266,7 +266,7 @@ def test_SyncBatchNorm(self): def test_relu(self): with self.static_graph(): t = layers.data(name='t', shape=[3, 3], dtype='float32') - ret = layers.relu(t) + ret = F.relu(t) static_ret = self.get_static_graph_result( feed={'t': np.ones([3, 3], dtype='float32')}, fetch_list=[ret] )[0] @@ -274,11 +274,11 @@ def test_relu(self): with self.dynamic_graph(): with _test_eager_guard(): t = np.ones([3, 3], dtype='float32') - dy_eager_ret = layers.relu(base.to_variable(t)) + dy_eager_ret = F.relu(base.to_variable(t)) dy_eager_ret_value = dy_eager_ret.numpy() t = np.ones([3, 3], dtype='float32') - dy_ret = layers.relu(base.to_variable(t)) + dy_ret = F.relu(base.to_variable(t)) dy_ret_value = dy_ret.numpy() np.testing.assert_allclose(static_ret, dy_ret_value, rtol=1e-05) diff --git a/python/paddle/fluid/tests/unittests/test_memory_reuse_exclude_feed_var.py b/python/paddle/fluid/tests/unittests/test_memory_reuse_exclude_feed_var.py index 232c0f5c4925b..bdd6b3d304899 100644 --- a/python/paddle/fluid/tests/unittests/test_memory_reuse_exclude_feed_var.py +++ b/python/paddle/fluid/tests/unittests/test_memory_reuse_exclude_feed_var.py @@ -18,6 +18,7 @@ import paddle import paddle.fluid as fluid +import paddle.nn.functional as F class TestMemoryReuseExcludeFeedVar(unittest.TestCase): @@ -29,7 +30,7 @@ def main_impl(self, place): image = fluid.layers.data( name='image', shape=self.image_shape, dtype='float32' ) - relu_image = fluid.layers.relu(image) + relu_image = F.relu(image) loss = paddle.mean(relu_image) build_strategy = fluid.BuildStrategy() diff --git a/python/paddle/fluid/tests/unittests/test_op_function_generator.py b/python/paddle/fluid/tests/unittests/test_op_function_generator.py index f3991150193c3..eff73a4548f0e 100644 --- a/python/paddle/fluid/tests/unittests/test_op_function_generator.py +++ b/python/paddle/fluid/tests/unittests/test_op_function_generator.py @@ -18,7 +18,7 @@ import paddle import paddle.fluid as fluid -import paddle.fluid.layers as layers +import paddle.nn.functional as F from paddle import _legacy_C_ops @@ -66,7 +66,7 @@ def test_relu(self): a = np.random.uniform(-1, 1, self.shape).astype(self.dtype) x = fluid.dygraph.to_variable(a) - res1 = layers.relu(x) + res1 = F.relu(x) res2 = _legacy_C_ops.relu(x) np.testing.assert_array_equal(res1.numpy(), res2.numpy()) diff --git a/python/paddle/fluid/tests/unittests/test_paddle_imperative_double_grad.py b/python/paddle/fluid/tests/unittests/test_paddle_imperative_double_grad.py index 1547bd673db5f..ee6112b7d5397 100644 --- a/python/paddle/fluid/tests/unittests/test_paddle_imperative_double_grad.py +++ b/python/paddle/fluid/tests/unittests/test_paddle_imperative_double_grad.py @@ -19,6 +19,7 @@ import paddle import paddle.fluid as fluid +import paddle.nn.functional as F from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard from paddle.fluid.wrapped_decorator import wrap_decorator @@ -235,7 +236,7 @@ def func_example_with_gradient_accumulation_and_create_graph(self): numel = x_np.size x.stop_gradient = False - y = fluid.layers.relu(x) + y = F.relu(x) z = y + 1 w = z * z @@ -281,8 +282,8 @@ def func_example_with_gradient_accumulation_and_no_grad_vars(self): numel = x_np.size x.stop_gradient = False - y1 = fluid.layers.relu(x) - y2 = fluid.layers.relu(x) + y1 = F.relu(x) + y2 = F.relu(x) z = y1 + y2 w = z * z @@ -333,7 +334,7 @@ def func_example_with_gradient_accumulation_and_not_create_graph(self): numel = x_np.size x.stop_gradient = False - y = fluid.layers.relu(x) + y = F.relu(x) z = y + 1 w = z * z diff --git a/python/paddle/fluid/tests/unittests/test_parallel_executor_inference_feed_partial_data.py b/python/paddle/fluid/tests/unittests/test_parallel_executor_inference_feed_partial_data.py index 7d3823a07ee2e..675e39ececf6d 100644 --- a/python/paddle/fluid/tests/unittests/test_parallel_executor_inference_feed_partial_data.py +++ b/python/paddle/fluid/tests/unittests/test_parallel_executor_inference_feed_partial_data.py @@ -18,6 +18,7 @@ import paddle import paddle.fluid as fluid +import paddle.nn.functional as F class TestInferencePartialFeed(unittest.TestCase): @@ -38,9 +39,9 @@ def run_network(self, places, use_split, has_persistable): else: lr = fluid.data(name='lr', shape=[None], dtype='float32') - relu_x = fluid.layers.relu(x) - relu_y = fluid.layers.relu(y) - relu_lr = fluid.layers.relu(lr) + relu_x = F.relu(x) + relu_y = F.relu(y) + relu_lr = F.relu(lr) exe = fluid.Executor(places[0]) exe.run(startup_prog) diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index 6fe6583b52c93..e7fbd7bf81440 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -20,6 +20,7 @@ import paddle import paddle.fluid as fluid import paddle.fluid.core as core +import paddle.nn.functional as F from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard @@ -653,7 +654,7 @@ def func_test_backward(self): with fluid.dygraph.guard(): var = fluid.dygraph.to_variable(self.array) var.stop_gradient = False - loss = fluid.layers.relu(var) + loss = F.relu(var) loss.backward() grad_var = var._grad_ivar() self.assertEqual(grad_var.shape, self.shape) @@ -667,7 +668,7 @@ def func_test_gradient(self): with fluid.dygraph.guard(): var = fluid.dygraph.to_variable(self.array) var.stop_gradient = False - loss = fluid.layers.relu(var) + loss = F.relu(var) loss.backward() grad_var = var.gradient() self.assertEqual(grad_var.shape, self.array.shape) diff --git a/python/paddle/fluid/tests/unittests/test_while_loop_op.py b/python/paddle/fluid/tests/unittests/test_while_loop_op.py index 3bee6eef63950..89ca943a057b8 100644 --- a/python/paddle/fluid/tests/unittests/test_while_loop_op.py +++ b/python/paddle/fluid/tests/unittests/test_while_loop_op.py @@ -20,6 +20,7 @@ import paddle.fluid as fluid import paddle.fluid.core as core import paddle.fluid.layers as layers +import paddle.nn.functional as F from paddle.fluid.backward import append_backward from paddle.fluid.framework import Program, program_guard @@ -96,7 +97,7 @@ def body(i, ten, test_dict, test_list, test_list_dict): test_list[0] = paddle.reshape(test_list[0], [2, -1]) + 1 test_list_dict[0]["test_key"] += 1 - test_list_dict[0]["test_key"] = fluid.layers.relu( + test_list_dict[0]["test_key"] = F.relu( test_list_dict[0]["test_key"] )