From b01f979b68fa87bd12c6af8b1de01a043f88cfa4 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Fri, 9 Dec 2022 17:06:57 +0800 Subject: [PATCH] replace cross_entropy in python/paddle/fluid/tests/unittests/*.py except test*.py (#48919) --- .../fluid/tests/unittests/dist_allreduce_op.py | 4 +++- python/paddle/fluid/tests/unittests/dist_ctr.py | 4 +++- .../paddle/fluid/tests/unittests/dist_fleet_ctr.py | 4 +++- .../tests/unittests/dist_fleet_heter_pipeline_ctr.py | 4 +++- .../unittests/dist_fleet_raw_program_optimizer.py | 4 +++- ...ist_fleet_raw_program_optimizer_fuse_allreduce.py | 4 +++- .../unittests/dist_fleet_sparse_embedding_ctr.py | 4 +++- python/paddle/fluid/tests/unittests/dist_mnist.py | 4 +++- .../fluid/tests/unittests/dist_mnist_batch_merge.py | 4 +++- .../tests/unittests/dist_mnist_fp16_allreduce.py | 4 +++- .../paddle/fluid/tests/unittests/dist_mnist_lars.py | 4 +++- .../paddle/fluid/tests/unittests/dist_se_resnext.py | 4 +++- .../tests/unittests/dist_text_classification.py | 4 +++- .../fluid/tests/unittests/fleet_heter_ps_training.py | 4 +++- .../fluid/tests/unittests/parallel_dygraph_mnist.py | 4 +++- python/paddle/fluid/tests/unittests/seresnext_net.py | 4 +++- python/paddle/fluid/tests/unittests/simple_nets.py | 12 +++++++++--- .../fluid/tests/unittests/transformer_model.py | 4 +++- 18 files changed, 60 insertions(+), 20 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dist_allreduce_op.py b/python/paddle/fluid/tests/unittests/dist_allreduce_op.py index c956f287d7b..a4e41147811 100644 --- a/python/paddle/fluid/tests/unittests/dist_allreduce_op.py +++ b/python/paddle/fluid/tests/unittests/dist_allreduce_op.py @@ -77,7 +77,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/dist_ctr.py b/python/paddle/fluid/tests/unittests/dist_ctr.py index 73e4f7fffc8..8b910514202 100644 --- a/python/paddle/fluid/tests/unittests/dist_ctr.py +++ b/python/paddle/fluid/tests/unittests/dist_ctr.py @@ -103,7 +103,9 @@ class TestDistCTR2x2(TestDistRunnerBase): auc_var, batch_auc_var, auc_states = paddle.static.auc( input=predict, label=label ) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) inference_program = paddle.fluid.default_main_program().clone() diff --git a/python/paddle/fluid/tests/unittests/dist_fleet_ctr.py b/python/paddle/fluid/tests/unittests/dist_fleet_ctr.py index 3826bbae5ca..59f8f67aca8 100644 --- a/python/paddle/fluid/tests/unittests/dist_fleet_ctr.py +++ b/python/paddle/fluid/tests/unittests/dist_fleet_ctr.py @@ -154,7 +154,9 @@ class TestDistCTR2x2(FleetDistRunnerBase): input=predict, label=label ) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) self.feeds = datas diff --git a/python/paddle/fluid/tests/unittests/dist_fleet_heter_pipeline_ctr.py b/python/paddle/fluid/tests/unittests/dist_fleet_heter_pipeline_ctr.py index 07feafdf77f..5e909773a61 100644 --- a/python/paddle/fluid/tests/unittests/dist_fleet_heter_pipeline_ctr.py +++ b/python/paddle/fluid/tests/unittests/dist_fleet_heter_pipeline_ctr.py @@ -123,7 +123,9 @@ class TestHeterPipelinePsCTR2x2(FleetDistHeterRunnerBase): label = fluid.layers.cast(label, dtype="int64") predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax') - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) fluid.layers.Print(avg_cost, message="avg_cost") diff --git a/python/paddle/fluid/tests/unittests/dist_fleet_raw_program_optimizer.py b/python/paddle/fluid/tests/unittests/dist_fleet_raw_program_optimizer.py index 7c98169433b..e45d2c93a86 100644 --- a/python/paddle/fluid/tests/unittests/dist_fleet_raw_program_optimizer.py +++ b/python/paddle/fluid/tests/unittests/dist_fleet_raw_program_optimizer.py @@ -79,7 +79,9 @@ class TestFleetMetaOptimizerPrecision(TestDistRunnerBase): # Train program predict = cnn_model(images) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/dist_fleet_raw_program_optimizer_fuse_allreduce.py b/python/paddle/fluid/tests/unittests/dist_fleet_raw_program_optimizer_fuse_allreduce.py index e46173735a9..1a23b86549e 100644 --- a/python/paddle/fluid/tests/unittests/dist_fleet_raw_program_optimizer_fuse_allreduce.py +++ b/python/paddle/fluid/tests/unittests/dist_fleet_raw_program_optimizer_fuse_allreduce.py @@ -79,7 +79,9 @@ class TestFleetMetaOptimizerFuseAllReducePrecision(TestDistRunnerBase): # Train program predict = cnn_model(images) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/dist_fleet_sparse_embedding_ctr.py b/python/paddle/fluid/tests/unittests/dist_fleet_sparse_embedding_ctr.py index 1868f81dd12..b22403a6fa8 100644 --- a/python/paddle/fluid/tests/unittests/dist_fleet_sparse_embedding_ctr.py +++ b/python/paddle/fluid/tests/unittests/dist_fleet_sparse_embedding_ctr.py @@ -140,7 +140,9 @@ class TestDistCTR2x2(FleetDistRunnerBase): acc = paddle.static.accuracy(input=predict, label=label) auc_var, _, _ = paddle.static.auc(input=predict, label=label) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) self.feeds = datas diff --git a/python/paddle/fluid/tests/unittests/dist_mnist.py b/python/paddle/fluid/tests/unittests/dist_mnist.py index 819b959a1fa..4510b57391b 100644 --- a/python/paddle/fluid/tests/unittests/dist_mnist.py +++ b/python/paddle/fluid/tests/unittests/dist_mnist.py @@ -78,7 +78,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/dist_mnist_batch_merge.py b/python/paddle/fluid/tests/unittests/dist_mnist_batch_merge.py index aa963ab012b..cac46996edd 100644 --- a/python/paddle/fluid/tests/unittests/dist_mnist_batch_merge.py +++ b/python/paddle/fluid/tests/unittests/dist_mnist_batch_merge.py @@ -43,7 +43,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/dist_mnist_fp16_allreduce.py b/python/paddle/fluid/tests/unittests/dist_mnist_fp16_allreduce.py index ad0b25e8ea1..e4af13fa89b 100644 --- a/python/paddle/fluid/tests/unittests/dist_mnist_fp16_allreduce.py +++ b/python/paddle/fluid/tests/unittests/dist_mnist_fp16_allreduce.py @@ -37,7 +37,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/dist_mnist_lars.py b/python/paddle/fluid/tests/unittests/dist_mnist_lars.py index b886ad89534..dc2fc10450b 100644 --- a/python/paddle/fluid/tests/unittests/dist_mnist_lars.py +++ b/python/paddle/fluid/tests/unittests/dist_mnist_lars.py @@ -34,7 +34,9 @@ class TestDistMnist2x2(TestDistRunnerBase): # Train program predict = cnn_model(images) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) # Evaluator diff --git a/python/paddle/fluid/tests/unittests/dist_se_resnext.py b/python/paddle/fluid/tests/unittests/dist_se_resnext.py index c4dd1de92d2..2b8ee5c62ac 100644 --- a/python/paddle/fluid/tests/unittests/dist_se_resnext.py +++ b/python/paddle/fluid/tests/unittests/dist_se_resnext.py @@ -214,7 +214,9 @@ class DistSeResneXt2x2(TestDistRunnerBase): # Train program model = SE_ResNeXt(layers=50) out = model.net(input=image, class_dim=102) - cost = fluid.layers.cross_entropy(input=out, label=label) + cost = paddle.nn.functional.cross_entropy( + input=out, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) acc_top1 = paddle.static.accuracy(input=out, label=label, k=1) diff --git a/python/paddle/fluid/tests/unittests/dist_text_classification.py b/python/paddle/fluid/tests/unittests/dist_text_classification.py index 73e153164b3..591106ab1ff 100644 --- a/python/paddle/fluid/tests/unittests/dist_text_classification.py +++ b/python/paddle/fluid/tests/unittests/dist_text_classification.py @@ -132,7 +132,9 @@ class TestDistTextClassification2x2(TestDistRunnerBase): # Train program predict = conv_net(data, dict_dim) - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) acc = paddle.static.accuracy(input=predict, label=label) inference_program = fluid.default_main_program().clone() diff --git a/python/paddle/fluid/tests/unittests/fleet_heter_ps_training.py b/python/paddle/fluid/tests/unittests/fleet_heter_ps_training.py index 7c93d710c9a..1c7ecd1036c 100644 --- a/python/paddle/fluid/tests/unittests/fleet_heter_ps_training.py +++ b/python/paddle/fluid/tests/unittests/fleet_heter_ps_training.py @@ -112,7 +112,9 @@ def net(batch_size=4, lr=0.01): label = fluid.layers.cast(label, dtype="int64") predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax') - cost = fluid.layers.cross_entropy(input=predict, label=label) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) return datas, avg_cost diff --git a/python/paddle/fluid/tests/unittests/parallel_dygraph_mnist.py b/python/paddle/fluid/tests/unittests/parallel_dygraph_mnist.py index dd9e9956065..d12d07f6944 100644 --- a/python/paddle/fluid/tests/unittests/parallel_dygraph_mnist.py +++ b/python/paddle/fluid/tests/unittests/parallel_dygraph_mnist.py @@ -95,7 +95,9 @@ class MNIST(fluid.dygraph.Layer): x = self._simple_img_conv_pool_2(x) x = paddle.reshape(x, shape=[-1, self.pool_2_shape]) cost = self._fc(x) - loss = fluid.layers.cross_entropy(self.act(cost), label) + loss = paddle.nn.functional.cross_entropy( + self.act(cost), label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) return avg_loss diff --git a/python/paddle/fluid/tests/unittests/seresnext_net.py b/python/paddle/fluid/tests/unittests/seresnext_net.py index 8dcccfa643d..5df8d39ed1b 100644 --- a/python/paddle/fluid/tests/unittests/seresnext_net.py +++ b/python/paddle/fluid/tests/unittests/seresnext_net.py @@ -170,7 +170,9 @@ def SE_ResNeXt50Small(use_feed): ) # Classifier layer: prediction = fluid.layers.fc(input=dropout, size=1000, act='softmax') - loss = fluid.layers.cross_entropy(input=prediction, label=label) + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss diff --git a/python/paddle/fluid/tests/unittests/simple_nets.py b/python/paddle/fluid/tests/unittests/simple_nets.py index 9d124ee5092..747cde08274 100644 --- a/python/paddle/fluid/tests/unittests/simple_nets.py +++ b/python/paddle/fluid/tests/unittests/simple_nets.py @@ -30,7 +30,9 @@ def simple_fc_net_with_inputs(img, label, class_num=10): ), ) prediction = fluid.layers.fc(hidden, size=class_num, act='softmax') - loss = fluid.layers.cross_entropy(input=prediction, label=label) + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss @@ -56,7 +58,9 @@ def batchnorm_fc_with_inputs(img, label, class_num=10): hidden = paddle.static.nn.batch_norm(input=hidden) prediction = fluid.layers.fc(hidden, size=class_num, act='softmax') - loss = fluid.layers.cross_entropy(input=prediction, label=label) + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss @@ -93,7 +97,9 @@ def bow_net( fc_1 = fluid.layers.fc(input=bow_tanh, size=hid_dim, act="tanh") fc_2 = fluid.layers.fc(input=fc_1, size=hid_dim2, act="tanh") prediction = fluid.layers.fc(input=[fc_2], size=class_dim, act="softmax") - cost = fluid.layers.cross_entropy(input=prediction, label=label) + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=label, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) return avg_cost diff --git a/python/paddle/fluid/tests/unittests/transformer_model.py b/python/paddle/fluid/tests/unittests/transformer_model.py index a338d31f78f..b51d20eb7e7 100644 --- a/python/paddle/fluid/tests/unittests/transformer_model.py +++ b/python/paddle/fluid/tests/unittests/transformer_model.py @@ -594,6 +594,8 @@ def transformer( ) predict = paddle.nn.functional.softmax(predict) - cost = layers.cross_entropy(input=predict, label=gold) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=gold, reduction='none', use_softmax=False + ) weighted_cost = cost * weights return paddle.sum(weighted_cost) -- GitLab