From fcdf633f373d479b1198291671f1d0749a5a5bf1 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Fri, 9 Dec 2022 13:14:01 +0800 Subject: [PATCH] replace cross_entropy in python/paddle/fluid/tests/unittests/test_[a-n]*.py except test_dist_transpiler.py (#48913) --- .../paddle/fluid/tests/unittests/test_adam_op.py | 11 +++++++++-- .../test_async_ssa_graph_executor_mnist.py | 4 +++- .../tests/unittests/test_compiled_program.py | 4 +++- .../tests/unittests/test_cross_entropy_op.py | 9 +++++++-- .../tests/unittests/test_decoupled_py_reader.py | 7 ++++++- .../fluid/tests/unittests/test_desc_clone.py | 16 ++++++++++++---- .../test_dist_fleet_a_sync_optimizer_auto.py | 4 ++-- ...est_dist_fleet_a_sync_optimizer_auto_async.py | 4 ++-- .../test_dist_fleet_a_sync_optimizer_auto_geo.py | 4 ++-- .../test_dist_fleet_a_sync_optimizer_geo.py | 8 ++++---- .../unittests/test_dist_fleet_heter_program.py | 4 +++- .../tests/unittests/test_dygraph_mnist_fp16.py | 4 +++- .../unittests/test_dygraph_multi_forward.py | 8 ++++++-- .../unittests/test_eager_deletion_delete_vars.py | 4 +++- .../unittests/test_eager_deletion_gru_net.py | 4 +++- .../unittests/test_eager_deletion_lstm_net.py | 4 +++- .../unittests/test_feed_data_check_shape_type.py | 7 ++++++- .../fluid/tests/unittests/test_fetch_unmerged.py | 4 +++- .../fluid/tests/unittests/test_fleet_auto.py | 4 ++-- .../fluid/tests/unittests/test_fleet_base.py | 7 ++++++- .../fluid/tests/unittests/test_fleet_base_2.py | 4 ++-- .../fluid/tests/unittests/test_fleet_base_3.py | 8 ++++---- .../tests/unittests/test_fleet_base_single.py | 8 ++++++-- .../tests/unittests/test_fuse_bn_act_pass.py | 4 +++- .../tests/unittests/test_fuse_bn_add_act_pass.py | 8 ++++++-- .../test_fuse_relu_depthwise_conv_pass.py | 4 +++- .../tests/unittests/test_generator_dataloader.py | 7 ++++++- .../fluid/tests/unittests/test_gradient_clip.py | 8 ++++++-- .../unittests/test_imperative_auto_prune.py | 10 +++++++--- .../tests/unittests/test_imperative_mnist.py | 8 ++++++-- .../test_imperative_mnist_sorted_gradient.py | 8 ++++++-- .../test_imperative_ocr_attention_model.py | 14 ++++++++++---- .../tests/unittests/test_imperative_resnet.py | 8 ++++++-- .../test_imperative_resnet_sorted_gradient.py | 8 ++++++-- .../unittests/test_imperative_se_resnext.py | 14 +++++++++++--- .../test_imperative_static_runner_mnist.py | 8 ++++++-- .../test_imperative_static_runner_while.py | 12 +++++++++--- .../tests/unittests/test_inference_model_io.py | 4 +++- .../tests/unittests/test_ir_inplace_pass.py | 4 +++- .../unittests/test_ir_memory_optimize_nlp.py | 4 +++- .../unittests/test_ir_memory_optimize_pass.py | 8 ++++++-- .../fluid/tests/unittests/test_jit_save_load.py | 12 +++++++++--- .../test_load_state_dict_from_old_format.py | 4 +++- .../test_mix_precision_all_reduce_fuse.py | 4 +++- .../test_multiprocess_dataloader_dynamic.py | 8 ++++++-- ...rocess_dataloader_iterable_dataset_dynamic.py | 8 ++++++-- ...process_dataloader_iterable_dataset_static.py | 7 ++++++- .../test_multiprocess_dataloader_static.py | 7 ++++++- 48 files changed, 245 insertions(+), 87 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_adam_op.py b/python/paddle/fluid/tests/unittests/test_adam_op.py index 6298c923a2..5514d3aa25 100644 --- a/python/paddle/fluid/tests/unittests/test_adam_op.py +++ b/python/paddle/fluid/tests/unittests/test_adam_op.py @@ -806,7 +806,12 @@ class TestAdamOptimizer(unittest.TestCase): input=fc_1, size=2, param_attr=weight_attr2, 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, + ) loss = paddle.mean(cost) beta1_init = 0.9 beta2_init = 0.999 @@ -966,7 +971,9 @@ class TestAdamOptimizer(unittest.TestCase): fc_1 = fluid.layers.fc(input=z, size=128) prediction = fluid.layers.fc(input=fc_1, size=2, 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 + ) loss = paddle.mean(cost) adam = fluid.optimizer.Adam(use_global_beta_pow=True) adam.minimize(loss) diff --git a/python/paddle/fluid/tests/unittests/test_async_ssa_graph_executor_mnist.py b/python/paddle/fluid/tests/unittests/test_async_ssa_graph_executor_mnist.py index 54e74ade09..b5f3ada246 100644 --- a/python/paddle/fluid/tests/unittests/test_async_ssa_graph_executor_mnist.py +++ b/python/paddle/fluid/tests/unittests/test_async_ssa_graph_executor_mnist.py @@ -57,7 +57,9 @@ def convolutional_neural_network(use_py_reader): ) prediction = fluid.layers.fc(input=conv_pool_2, size=10, 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 + ) avg_loss = paddle.mean(loss) acc = paddle.static.accuracy(input=prediction, label=label) i = fluid.layers.zeros(shape=[1], dtype='int64') diff --git a/python/paddle/fluid/tests/unittests/test_compiled_program.py b/python/paddle/fluid/tests/unittests/test_compiled_program.py index 70edcc5358..729a7e3e10 100644 --- a/python/paddle/fluid/tests/unittests/test_compiled_program.py +++ b/python/paddle/fluid/tests/unittests/test_compiled_program.py @@ -107,7 +107,9 @@ class TestCompiledProgramError(unittest.TestCase): ) label = fluid.layers.data(name='label', shape=[1], dtype='int64') prediction = fluid.layers.fc(input=img, size=10, 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 + ) avg_loss = paddle.mean(loss) def compile_program_not_compiled(self): diff --git a/python/paddle/fluid/tests/unittests/test_cross_entropy_op.py b/python/paddle/fluid/tests/unittests/test_cross_entropy_op.py index 4e139b1bef..a1f650dc63 100644 --- a/python/paddle/fluid/tests/unittests/test_cross_entropy_op.py +++ b/python/paddle/fluid/tests/unittests/test_cross_entropy_op.py @@ -17,6 +17,7 @@ import unittest import numpy as np from op_test import OpTest, randomize_probability +import paddle import paddle.fluid as fluid import paddle.fluid.core as core from paddle.fluid import Program, program_guard @@ -419,7 +420,9 @@ class TestCrossEntropyOpError(unittest.TestCase): lab1 = fluid.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() ) - fluid.layers.cross_entropy(x1, lab1) + paddle.nn.functional.cross_entropy( + x1, lab1, reduction='none', use_softmax=False + ) self.assertRaises(TypeError, test_Variable) @@ -432,7 +435,9 @@ class TestCrossEntropyOpError(unittest.TestCase): lab2 = fluid.layers.data( name='lab2', shape=[3, 4, 5, 6], dtype="int32" ) - fluid.layers.cross_entropy(x2, lab2) + paddle.nn.functional.cross_entropy( + x2, lab2, reduction='none', use_softmax=False + ) self.assertRaises(TypeError, test_dtype) diff --git a/python/paddle/fluid/tests/unittests/test_decoupled_py_reader.py b/python/paddle/fluid/tests/unittests/test_decoupled_py_reader.py index 72ec58aa2d..f3913b7f3a 100644 --- a/python/paddle/fluid/tests/unittests/test_decoupled_py_reader.py +++ b/python/paddle/fluid/tests/unittests/test_decoupled_py_reader.py @@ -67,7 +67,12 @@ def simple_fc_net(places, use_legacy_py_reader, use_double_buffer): hidden, size=CLASS_NUM, act='softmax' ) loss = paddle.mean( - fluid.layers.cross_entropy(input=predict_label, label=label) + paddle.nn.functional.cross_entropy( + input=predict_label, + label=label, + reduction='none', + use_softmax=False, + ) ) optimizer = fluid.optimizer.Adam() diff --git a/python/paddle/fluid/tests/unittests/test_desc_clone.py b/python/paddle/fluid/tests/unittests/test_desc_clone.py index 477910f53d..52ee114ae8 100644 --- a/python/paddle/fluid/tests/unittests/test_desc_clone.py +++ b/python/paddle/fluid/tests/unittests/test_desc_clone.py @@ -73,7 +73,9 @@ def get_model(batch_size): # 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 @@ -188,9 +190,11 @@ class TestCloneWithStopGradient(unittest.TestCase): hidden1 = fluid.layers.fc(input=img, size=200, act='relu') hidden1.stop_gradient = True hidden2 = fluid.layers.dropout(hidden1, dropout_prob=0.5) - loss = fluid.layers.cross_entropy( + loss = paddle.nn.functional.cross_entropy( input=fluid.layers.fc(hidden2, size=10, act='softmax'), label=fluid.layers.data(name='label', shape=[1], dtype='int64'), + reduction='none', + use_softmax=False, ) avg_loss = paddle.mean(loss) test_program = train_program.clone(for_test=False) @@ -226,9 +230,11 @@ class TestCloneWithStopGradientInSubBlock(unittest.TestCase): hidden2 = fluid.layers.cond(cond, true_fn, false_fn) - loss = fluid.layers.cross_entropy( + loss = paddle.nn.functional.cross_entropy( input=fluid.layers.fc(hidden2, size=10, act='softmax'), label=fluid.layers.data(name='label', shape=[1], dtype='int64'), + reduction='none', + use_softmax=False, ) avg_loss = paddle.mean(loss) test_program = train_program.clone(for_test=False) @@ -266,9 +272,11 @@ class TestCloneWithRaise(unittest.TestCase): return hidden2 hidden2 = fluid.layers.cond(cond, true_fn, false_fn) - loss = fluid.layers.cross_entropy( + loss = paddle.nn.functional.cross_entropy( input=fluid.layers.fc(hidden2, size=10, act='softmax'), label=fluid.layers.data(name='label', shape=[1], dtype='int64'), + reduction='none', + use_softmax=False, ) avg_loss = paddle.mean(loss) test_program = train_program.clone(for_test=False) diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto.py index e2f08591d7..967028f02d 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto.py @@ -52,8 +52,8 @@ class TestFleetGradientMergeMetaOptimizer(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto_async.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto_async.py index aa46502b4d..6ffea5df32 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto_async.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto_async.py @@ -68,8 +68,8 @@ class TestFleetGradientMergeMetaOptimizer(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=x_embedding, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto_geo.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto_geo.py index 88f3769c83..3bb5b669c8 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto_geo.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_auto_geo.py @@ -56,8 +56,8 @@ class TestFleetGradientMergeMetaOptimizer(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=emb, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) os.environ["FLAGS_LAUNCH_BARRIER"] = "0" diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_geo.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_geo.py index 789f7ee863..6556e19079 100755 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_geo.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_a_sync_optimizer_geo.py @@ -51,8 +51,8 @@ class TestFleetGradientMergeMetaOptimizer(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) @@ -84,8 +84,8 @@ class TestFleetGradientMergeMetaOptimizer(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) diff --git a/python/paddle/fluid/tests/unittests/test_dist_fleet_heter_program.py b/python/paddle/fluid/tests/unittests/test_dist_fleet_heter_program.py index 477d9091d7..dc77545cc1 100644 --- a/python/paddle/fluid/tests/unittests/test_dist_fleet_heter_program.py +++ b/python/paddle/fluid/tests/unittests/test_dist_fleet_heter_program.py @@ -150,7 +150,9 @@ class TestDistFleetHeterProgram(unittest.TestCase): with fluid.device_guard("gpu"): labels = fluid.layers.cast(inputs[-1], dtype="int64") - cost = fluid.layers.cross_entropy(input=predict, label=labels) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=labels, reduction='none', use_softmax=False + ) avg_cost = paddle.sum(cost) return avg_cost diff --git a/python/paddle/fluid/tests/unittests/test_dygraph_mnist_fp16.py b/python/paddle/fluid/tests/unittests/test_dygraph_mnist_fp16.py index ef7059887b..862b386802 100644 --- a/python/paddle/fluid/tests/unittests/test_dygraph_mnist_fp16.py +++ b/python/paddle/fluid/tests/unittests/test_dygraph_mnist_fp16.py @@ -112,7 +112,9 @@ class MNIST(fluid.dygraph.Layer): x = paddle.reshape(x, shape=[-1, self.pool_2_shape]) cost = self._linear(x) cost = paddle.nn.functional.softmax(cost) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) return avg_loss diff --git a/python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py b/python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py index e4fd9766a2..e79773e8d4 100644 --- a/python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py +++ b/python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py @@ -138,7 +138,9 @@ class TestDygraphMultiForward(unittest.TestCase): label.stop_gradient = True cost = mnist(img) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) dy_out = avg_loss.numpy() @@ -167,7 +169,9 @@ class TestDygraphMultiForward(unittest.TestCase): ) label = fluid.layers.data(name='label', shape=[1], dtype='int64') cost = mnist(img) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) # initialize params and fetch them diff --git a/python/paddle/fluid/tests/unittests/test_eager_deletion_delete_vars.py b/python/paddle/fluid/tests/unittests/test_eager_deletion_delete_vars.py index d3ea6fd4f4..9895f5b248 100644 --- a/python/paddle/fluid/tests/unittests/test_eager_deletion_delete_vars.py +++ b/python/paddle/fluid/tests/unittests/test_eager_deletion_delete_vars.py @@ -45,7 +45,9 @@ def simple_fc_net(): ), ) prediction = fluid.layers.fc(hidden, size=10, 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) optimizer = fluid.optimizer.Adam(learning_rate=1e-3) optimizer.minimize(loss) diff --git a/python/paddle/fluid/tests/unittests/test_eager_deletion_gru_net.py b/python/paddle/fluid/tests/unittests/test_eager_deletion_gru_net.py index b5dff40cb3..1ac689d8df 100644 --- a/python/paddle/fluid/tests/unittests/test_eager_deletion_gru_net.py +++ b/python/paddle/fluid/tests/unittests/test_eager_deletion_gru_net.py @@ -43,7 +43,9 @@ def gru_net( gru_max_tanh = paddle.tanh(gru_max) fc1 = fluid.layers.fc(input=gru_max_tanh, size=hid_dim2, act='tanh') prediction = fluid.layers.fc(input=fc1, 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/test_eager_deletion_lstm_net.py b/python/paddle/fluid/tests/unittests/test_eager_deletion_lstm_net.py index 3cd7c681ad..09fc60e6b1 100644 --- a/python/paddle/fluid/tests/unittests/test_eager_deletion_lstm_net.py +++ b/python/paddle/fluid/tests/unittests/test_eager_deletion_lstm_net.py @@ -45,7 +45,9 @@ def lstm_net( lstm_max_tanh = paddle.tanh(lstm_max) fc1 = fluid.layers.fc(input=lstm_max_tanh, size=hid_dim2, act='tanh') prediction = fluid.layers.fc(input=fc1, 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/test_feed_data_check_shape_type.py b/python/paddle/fluid/tests/unittests/test_feed_data_check_shape_type.py index f1a1fb5f3b..0e08b14e32 100644 --- a/python/paddle/fluid/tests/unittests/test_feed_data_check_shape_type.py +++ b/python/paddle/fluid/tests/unittests/test_feed_data_check_shape_type.py @@ -67,7 +67,12 @@ class TestFeedData(unittest.TestCase): predict_label = fluid.layers.fc(hidden, size=class_num, act='softmax') loss = paddle.mean( - fluid.layers.cross_entropy(input=predict_label, label=label) + paddle.nn.functional.cross_entropy( + input=predict_label, + label=label, + reduction='none', + use_softmax=False, + ) ) optimizer = fluid.optimizer.Adam() diff --git a/python/paddle/fluid/tests/unittests/test_fetch_unmerged.py b/python/paddle/fluid/tests/unittests/test_fetch_unmerged.py index 028954d22f..c1b8046c69 100644 --- a/python/paddle/fluid/tests/unittests/test_fetch_unmerged.py +++ b/python/paddle/fluid/tests/unittests/test_fetch_unmerged.py @@ -46,7 +46,9 @@ class TestFetchUnmerged(unittest.TestCase): ) hidden = fluid.layers.fc(input=conv_pool_2, size=32, act='relu') prediction = fluid.layers.fc(input=hidden, size=10, 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 + ) avg_loss = paddle.mean(loss) return avg_loss, prediction diff --git a/python/paddle/fluid/tests/unittests/test_fleet_auto.py b/python/paddle/fluid/tests/unittests/test_fleet_auto.py index 22abaaf4d1..28f05a6e03 100644 --- a/python/paddle/fluid/tests/unittests/test_fleet_auto.py +++ b/python/paddle/fluid/tests/unittests/test_fleet_auto.py @@ -40,8 +40,8 @@ class TestDistributedStrategyAuto(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) diff --git a/python/paddle/fluid/tests/unittests/test_fleet_base.py b/python/paddle/fluid/tests/unittests/test_fleet_base.py index 74886d9a9c..1f5ea94296 100644 --- a/python/paddle/fluid/tests/unittests/test_fleet_base.py +++ b/python/paddle/fluid/tests/unittests/test_fleet_base.py @@ -203,7 +203,12 @@ class TestFleetBaseSingleError(unittest.TestCase): fc_1 = fluid.layers.fc(input=input_x, size=64, act='tanh') prediction = fluid.layers.fc(input=fc_1, size=2, act='softmax') - cost = fluid.layers.cross_entropy(input=prediction, label=input_y) + cost = paddle.nn.functional.cross_entropy( + input=prediction, + label=input_y, + reduction='none', + use_softmax=False, + ) avg_cost = paddle.mean(x=cost) fleet.init(is_collective=True) diff --git a/python/paddle/fluid/tests/unittests/test_fleet_base_2.py b/python/paddle/fluid/tests/unittests/test_fleet_base_2.py index 64cd2df903..fdffc388ea 100644 --- a/python/paddle/fluid/tests/unittests/test_fleet_base_2.py +++ b/python/paddle/fluid/tests/unittests/test_fleet_base_2.py @@ -54,8 +54,8 @@ class TestFleetBase(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) diff --git a/python/paddle/fluid/tests/unittests/test_fleet_base_3.py b/python/paddle/fluid/tests/unittests/test_fleet_base_3.py index 7d1672064d..33d970f109 100644 --- a/python/paddle/fluid/tests/unittests/test_fleet_base_3.py +++ b/python/paddle/fluid/tests/unittests/test_fleet_base_3.py @@ -40,8 +40,8 @@ class TestFleetBase_1(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) @@ -71,8 +71,8 @@ class TestFleetBase(unittest.TestCase): fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh') fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh') prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax') - cost = paddle.fluid.layers.cross_entropy( - input=prediction, label=input_y + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) diff --git a/python/paddle/fluid/tests/unittests/test_fleet_base_single.py b/python/paddle/fluid/tests/unittests/test_fleet_base_single.py index 39825686b3..4040e10594 100644 --- a/python/paddle/fluid/tests/unittests/test_fleet_base_single.py +++ b/python/paddle/fluid/tests/unittests/test_fleet_base_single.py @@ -85,7 +85,9 @@ class TestFleetBaseSingleRunCollective(unittest.TestCase): fc_1 = fluid.layers.fc(input=input_x, size=64, act='tanh') prediction = fluid.layers.fc(input=fc_1, size=2, act='softmax') - cost = fluid.layers.cross_entropy(input=prediction, label=input_y) + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) fleet.init(is_collective=True) @@ -124,7 +126,9 @@ class TestFleetBaseSingleRunPS(unittest.TestCase): fc_1 = fluid.layers.fc(input=input_x, size=64, act='tanh') prediction = fluid.layers.fc(input=fc_1, size=2, act='softmax') - cost = fluid.layers.cross_entropy(input=prediction, label=input_y) + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=input_y, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) fleet.init() diff --git a/python/paddle/fluid/tests/unittests/test_fuse_bn_act_pass.py b/python/paddle/fluid/tests/unittests/test_fuse_bn_act_pass.py index 9a7a907321..066443ebcf 100644 --- a/python/paddle/fluid/tests/unittests/test_fuse_bn_act_pass.py +++ b/python/paddle/fluid/tests/unittests/test_fuse_bn_act_pass.py @@ -53,7 +53,9 @@ class TestFuseBatchNormActPass(unittest.TestCase): input=hidden3, act='relu', data_layout='NHWC' ) prediction = fluid.layers.fc(input=hidden4, size=10, act='softmax') - loss = fluid.layers.cross_entropy(input=prediction, label=y) + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=y, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) sgd = fluid.optimizer.SGD(learning_rate=0.001) if use_cuda: 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 1b83dfa2b0..c644391eee 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 @@ -106,7 +106,9 @@ class TestFusedBnAddActAPI(unittest.TestCase): act='softmax', param_attr=self.fc_param_attr, ) - loss = fluid.layers.cross_entropy(input=prediction, label=y) + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=y, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) sgd = fluid.optimizer.SGD(learning_rate=0.001) sgd = fluid.contrib.mixed_precision.decorate( @@ -162,7 +164,9 @@ class TestFusedBnAddActAPI(unittest.TestCase): prediction = fluid.layers.fc( input=out, size=10, act='softmax', param_attr=self.fc_param_attr ) - loss = fluid.layers.cross_entropy(input=prediction, label=y) + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=y, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) sgd = fluid.optimizer.SGD(learning_rate=0.001) sgd = fluid.contrib.mixed_precision.decorate( 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 025e12c02c..bb2b22ff18 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 @@ -61,7 +61,9 @@ def simple_depthwise_net(use_feed): hidden = sep_conv(hidden, channel=200, stride=2, filter=5) hidden = fluid.layers.relu(hidden) prediction = fluid.layers.fc(hidden, size=10, 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/test_generator_dataloader.py b/python/paddle/fluid/tests/unittests/test_generator_dataloader.py index 22114853e9..02a34401e7 100644 --- a/python/paddle/fluid/tests/unittests/test_generator_dataloader.py +++ b/python/paddle/fluid/tests/unittests/test_generator_dataloader.py @@ -68,7 +68,12 @@ def simple_fc_net(places, use_legacy_py_reader, use_double_buffer): hidden, size=CLASS_NUM, act='softmax' ) loss = paddle.mean( - fluid.layers.cross_entropy(input=predict_label, label=label) + paddle.nn.functional.cross_entropy( + input=predict_label, + label=label, + reduction='none', + use_softmax=False, + ) ) optimizer = fluid.optimizer.Adam() diff --git a/python/paddle/fluid/tests/unittests/test_gradient_clip.py b/python/paddle/fluid/tests/unittests/test_gradient_clip.py index 0c89e00053..2243ae8c45 100644 --- a/python/paddle/fluid/tests/unittests/test_gradient_clip.py +++ b/python/paddle/fluid/tests/unittests/test_gradient_clip.py @@ -41,7 +41,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 @@ -83,7 +85,9 @@ class TestGradientClip(unittest.TestCase): hidden = fluid.layers.fc(input=image, size=32, act='relu') predict = fluid.layers.fc(input=hidden, size=10, 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(cost) prog_clip = prog.clone() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py b/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py index 5dbb1ac0a2..5a301d3f0a 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_auto_prune.py @@ -90,8 +90,10 @@ class AutoPruneLayer2(fluid.Layer): label = self.linear2(label) label = fluid.layers.cast(label, dtype="float32") label = fluid.layers.cast(label, dtype='int64') - # Note that the label is not persistable in fluid.layers.cross_entropy. - loss = fluid.layers.cross_entropy(input=feature, label=label) + # Note that the label is not persistable in paddle.nn.functional.cross_entropy. + loss = paddle.nn.functional.cross_entropy( + input=feature, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss @@ -107,7 +109,9 @@ class AutoPruneLayer3(fluid.Layer): feature, num_or_sections=[10, 10], dim=1 ) # Note that: part2 is not used. - loss = fluid.layers.cross_entropy(input=part1, label=label) + loss = paddle.nn.functional.cross_entropy( + input=part1, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) if test_num == 1: return loss, part2 diff --git a/python/paddle/fluid/tests/unittests/test_imperative_mnist.py b/python/paddle/fluid/tests/unittests/test_imperative_mnist.py index 7c8977ee18..8ee54bab6b 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_mnist.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_mnist.py @@ -159,7 +159,9 @@ class TestImperativeMnist(unittest.TestCase): cost_static = traced_layer([img]) helper.assertEachVar(cost, cost_static) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) dy_out = avg_loss.numpy() @@ -199,7 +201,9 @@ class TestImperativeMnist(unittest.TestCase): ) label = fluid.layers.data(name='label', shape=[1], dtype='int64') cost = mnist(img) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) sgd.minimize(avg_loss) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_mnist_sorted_gradient.py b/python/paddle/fluid/tests/unittests/test_imperative_mnist_sorted_gradient.py index 2ed75cb777..80f7162a64 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_mnist_sorted_gradient.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_mnist_sorted_gradient.py @@ -62,7 +62,9 @@ class TestImperativeMnistSortGradient(unittest.TestCase): label2.stop_gradient = True cost2 = mnist2(img2) - loss2 = fluid.layers.cross_entropy(cost2, label2) + loss2 = paddle.nn.functional.cross_entropy( + cost2, label2, reduction='none', use_softmax=False + ) avg_loss2 = paddle.mean(loss2) dy_out2 = avg_loss2.numpy() @@ -102,7 +104,9 @@ class TestImperativeMnistSortGradient(unittest.TestCase): ) label = fluid.layers.data(name='label', shape=[1], dtype='int64') cost = mnist(img) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) sgd.minimize(avg_loss) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py b/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py index 4e4c8aa435..1df0a4148c 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py @@ -477,8 +477,11 @@ class TestDygraphOCRAttention(unittest.TestCase): dy_prediction = paddle.reshape( dy_prediction, [label_out.shape[0], -1] ) - loss = fluid.layers.cross_entropy( - input=dy_prediction, label=label_out + loss = paddle.nn.functional.cross_entropy( + input=dy_prediction, + label=label_out, + reduction='none', + use_softmax=False, ) avg_loss = paddle.sum(loss) @@ -555,8 +558,11 @@ class TestDygraphOCRAttention(unittest.TestCase): static_prediction, shape=[-1, Config.num_classes + 2] ) - cost = fluid.layers.cross_entropy( - input=static_prediction, label=static_label_out + cost = paddle.nn.functional.cross_entropy( + input=static_prediction, + label=static_label_out, + reduction='none', + use_softmax=False, ) static_avg_loss = paddle.sum(cost) # param_grad_list = fluid.backward.append_backward(static_avg_loss) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_resnet.py b/python/paddle/fluid/tests/unittests/test_imperative_resnet.py index 559ea6ff71..16951a8743 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_resnet.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_resnet.py @@ -311,7 +311,9 @@ class TestDygraphResnet(unittest.TestCase): helper.assertEachVar(out_dygraph, out_static) resnet.train() - loss = fluid.layers.cross_entropy(input=out, label=label) + loss = paddle.nn.functional.cross_entropy( + input=out, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(x=loss) dy_out = avg_loss.numpy() @@ -364,7 +366,9 @@ class TestDygraphResnet(unittest.TestCase): ) label = fluid.layers.data(name='label', shape=[1], dtype='int64') out = resnet(img) - loss = fluid.layers.cross_entropy(input=out, label=label) + loss = paddle.nn.functional.cross_entropy( + input=out, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(x=loss) optimizer.minimize(avg_loss) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_resnet_sorted_gradient.py b/python/paddle/fluid/tests/unittests/test_imperative_resnet_sorted_gradient.py index 50afad1b5c..f28631d0ad 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_resnet_sorted_gradient.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_resnet_sorted_gradient.py @@ -118,7 +118,9 @@ class TestDygraphResnetSortGradient(unittest.TestCase): label.stop_gradient = True out = resnet(img) - loss = fluid.layers.cross_entropy(input=out, label=label) + loss = paddle.nn.functional.cross_entropy( + input=out, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(x=loss) dy_out = avg_loss.numpy() @@ -174,7 +176,9 @@ class TestDygraphResnetSortGradient(unittest.TestCase): ) label = fluid.layers.data(name='label', shape=[1], dtype='int64') out = resnet(img) - loss = fluid.layers.cross_entropy(input=out, label=label) + loss = paddle.nn.functional.cross_entropy( + input=out, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(x=loss) optimizer.minimize(avg_loss) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py b/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py index f2c8d285a0..6180d1c664 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py @@ -373,8 +373,11 @@ class TestImperativeResneXt(unittest.TestCase): out = se_resnext(img) softmax_out = paddle.nn.functional.softmax(out) - loss = fluid.layers.cross_entropy( - input=softmax_out, label=label + loss = paddle.nn.functional.cross_entropy( + input=softmax_out, + label=label, + reduction='none', + use_softmax=False, ) avg_loss = paddle.mean(x=loss) @@ -453,7 +456,12 @@ class TestImperativeResneXt(unittest.TestCase): label = fluid.layers.data(name='label', shape=[1], dtype='int64') out = se_resnext(img) softmax_out = paddle.nn.function.softmax(out) - loss = fluid.layers.cross_entropy(input=softmax_out, label=label) + loss = paddle.nn.functional.cross_entropy( + input=softmax_out, + label=label, + reduction='none', + use_softmax=False, + ) avg_loss = paddle.mean(x=loss) optimizer.minimize(avg_loss) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_static_runner_mnist.py b/python/paddle/fluid/tests/unittests/test_imperative_static_runner_mnist.py index ee2cc13d6a..6963be3379 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_static_runner_mnist.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_static_runner_mnist.py @@ -49,7 +49,9 @@ def convolutional_neural_network(img): def static_train_net(img, label): prediction = convolutional_neural_network(img) - loss = fluid.layers.cross_entropy(input=prediction, label=label) + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) optimizer = fluid.optimizer.SGD(learning_rate=0.001) @@ -172,7 +174,9 @@ class TestImperativeStaticModelRunnerMnist(unittest.TestCase): cost = mnist(img) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) avg_loss.backward() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_static_runner_while.py b/python/paddle/fluid/tests/unittests/test_imperative_static_runner_while.py index aac9152195..ff7644e61d 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_static_runner_while.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_static_runner_while.py @@ -78,7 +78,9 @@ class TestImperativeStaticModelRunnerWhile(unittest.TestCase): pred = while_softmax_regression(img) - loss = fluid.layers.cross_entropy(input=pred, label=label) + loss = paddle.nn.functional.cross_entropy( + input=pred, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) optimizer = fluid.optimizer.SGD(learning_rate=0.001) @@ -149,7 +151,9 @@ class TestImperativeStaticModelRunnerWhile(unittest.TestCase): cost = while_net(img) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) avg_loss.backward() @@ -174,7 +178,9 @@ class TestImperativeStaticModelRunnerWhile(unittest.TestCase): pred = while_softmax_regression(img) - loss = fluid.layers.cross_entropy(input=pred, label=label) + loss = paddle.nn.functional.cross_entropy( + input=pred, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) optimizer = fluid.optimizer.SGD(learning_rate=0.001) diff --git a/python/paddle/fluid/tests/unittests/test_inference_model_io.py b/python/paddle/fluid/tests/unittests/test_inference_model_io.py index daeae8e472..aad4a72648 100644 --- a/python/paddle/fluid/tests/unittests/test_inference_model_io.py +++ b/python/paddle/fluid/tests/unittests/test_inference_model_io.py @@ -182,7 +182,9 @@ class TestSaveInferenceModel(unittest.TestCase): auc_var, batch_auc_var, auc_states = paddle.static.auc( input=predict, label=y ) - cost = fluid.layers.cross_entropy(input=predict, label=y) + cost = paddle.nn.functional.cross_entropy( + input=predict, label=y, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(x=cost) place = core.CPUPlace() diff --git a/python/paddle/fluid/tests/unittests/test_ir_inplace_pass.py b/python/paddle/fluid/tests/unittests/test_ir_inplace_pass.py index 3c5f2edc4f..5d3efb8230 100644 --- a/python/paddle/fluid/tests/unittests/test_ir_inplace_pass.py +++ b/python/paddle/fluid/tests/unittests/test_ir_inplace_pass.py @@ -40,7 +40,9 @@ def fc_with_batchnorm(use_feed): hidden = paddle.static.nn.batch_norm(input=hidden) prediction = fluid.layers.fc(hidden, size=10, 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/test_ir_memory_optimize_nlp.py b/python/paddle/fluid/tests/unittests/test_ir_memory_optimize_nlp.py index 53d01d9df6..04afd37c26 100644 --- a/python/paddle/fluid/tests/unittests/test_ir_memory_optimize_nlp.py +++ b/python/paddle/fluid/tests/unittests/test_ir_memory_optimize_nlp.py @@ -46,7 +46,9 @@ def lstm_net( lstm_max_tanh = paddle.tanh(lstm_max) fc1 = fluid.layers.fc(input=lstm_max_tanh, size=hid_dim2, act='tanh') prediction = fluid.layers.fc(input=fc1, 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/test_ir_memory_optimize_pass.py b/python/paddle/fluid/tests/unittests/test_ir_memory_optimize_pass.py index 6cf5691583..f2de46ea46 100644 --- a/python/paddle/fluid/tests/unittests/test_ir_memory_optimize_pass.py +++ b/python/paddle/fluid/tests/unittests/test_ir_memory_optimize_pass.py @@ -35,7 +35,9 @@ def simple_fc_net(use_feed): for _ in range(hidden_layer): x = fluid.layers.fc(input=x, size=20, act='relu') y_predict = fluid.layers.fc(input=x, size=10, act='softmax') - cost = fluid.layers.cross_entropy(input=y_predict, label=y) + cost = paddle.nn.functional.cross_entropy( + input=y_predict, label=y, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(cost) return avg_cost @@ -48,7 +50,9 @@ def fc_with_inplace_net(use_feed): reshape = paddle.reshape(x=fc, shape=[-1, 2, 5]) reshape = paddle.reshape(x=reshape, shape=[-1, 5, 2]) y_predict = fluid.layers.fc(input=reshape, size=10, act='softmax') - cost = fluid.layers.cross_entropy(input=y_predict, label=y) + cost = paddle.nn.functional.cross_entropy( + input=y_predict, label=y, reduction='none', use_softmax=False + ) avg_cost = paddle.mean(cost) return avg_cost diff --git a/python/paddle/fluid/tests/unittests/test_jit_save_load.py b/python/paddle/fluid/tests/unittests/test_jit_save_load.py index acb8d08598..65138482a8 100644 --- a/python/paddle/fluid/tests/unittests/test_jit_save_load.py +++ b/python/paddle/fluid/tests/unittests/test_jit_save_load.py @@ -94,7 +94,9 @@ class LinerNetWithLabel(paddle.nn.Layer): ) def forward(self, x, label): out = self._linear(x) - loss = fluid.layers.cross_entropy(out, label) + loss = paddle.nn.functional.cross_entropy( + out, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) return out, avg_loss @@ -112,7 +114,9 @@ class LinerNetWithPruneInput(paddle.nn.Layer): ) def forward(self, x, label): out = self._linear(x) - loss = fluid.layers.cross_entropy(out, label) + loss = paddle.nn.functional.cross_entropy( + out, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) return out @@ -312,7 +316,9 @@ def train(layer, input_size=784, label_size=1): cost = layer(img) - loss = fluid.layers.cross_entropy(cost, label) + loss = paddle.nn.functional.cross_entropy( + cost, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) avg_loss.backward() diff --git a/python/paddle/fluid/tests/unittests/test_load_state_dict_from_old_format.py b/python/paddle/fluid/tests/unittests/test_load_state_dict_from_old_format.py index db4af74fc3..16ba749c9b 100644 --- a/python/paddle/fluid/tests/unittests/test_load_state_dict_from_old_format.py +++ b/python/paddle/fluid/tests/unittests/test_load_state_dict_from_old_format.py @@ -49,7 +49,9 @@ def convolutional_neural_network(img): def static_train_net(img, label): prediction = convolutional_neural_network(img) - loss = fluid.layers.cross_entropy(input=prediction, label=label) + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) optimizer = fluid.optimizer.SGD(learning_rate=0.001) diff --git a/python/paddle/fluid/tests/unittests/test_mix_precision_all_reduce_fuse.py b/python/paddle/fluid/tests/unittests/test_mix_precision_all_reduce_fuse.py index e024917a30..08bc72ffd7 100644 --- a/python/paddle/fluid/tests/unittests/test_mix_precision_all_reduce_fuse.py +++ b/python/paddle/fluid/tests/unittests/test_mix_precision_all_reduce_fuse.py @@ -28,7 +28,9 @@ img_shape = [1, 28, 28] def loss_net(hidden, label): prediction = fluid.layers.fc(input=hidden, size=10, 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 + ) avg_loss = paddle.mean(loss) return avg_loss diff --git a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_dynamic.py b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_dynamic.py index 34d89ec89b..46ad749d02 100644 --- a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_dynamic.py +++ b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_dynamic.py @@ -99,7 +99,9 @@ class TestDygraphDataLoader(unittest.TestCase): step = 0 for image, label in dataloader(): out = fc_net(image) - loss = fluid.layers.cross_entropy(out, label) + loss = paddle.nn.functional.cross_entropy( + out, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) avg_loss.backward() optimizer.minimize(avg_loss) @@ -169,7 +171,9 @@ class TestDygraphDataLoaderWithBatchedDataset(TestDygraphDataLoader): step = 0 for image, label in dataloader(): out = fc_net(image) - loss = fluid.layers.cross_entropy(out, label) + loss = paddle.nn.functional.cross_entropy( + out, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) avg_loss.backward() optimizer.minimize(avg_loss) diff --git a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_dynamic.py b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_dynamic.py index c4b59ef96e..29e2098567 100644 --- a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_dynamic.py +++ b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_dynamic.py @@ -99,7 +99,9 @@ class TestDygraphDataLoader(unittest.TestCase): step = 0 for image, label in dataloader(): out = fc_net(image) - loss = fluid.layers.cross_entropy(out, label) + loss = paddle.nn.functional.cross_entropy( + out, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) avg_loss.backward() optimizer.minimize(avg_loss) @@ -167,7 +169,9 @@ class TestDygraphDataLoaderWithBatchedDataset(TestDygraphDataLoader): step = 0 for image, label in dataloader(): out = fc_net(image) - loss = fluid.layers.cross_entropy(out, label) + loss = paddle.nn.functional.cross_entropy( + out, label, reduction='none', use_softmax=False + ) avg_loss = paddle.mean(loss) avg_loss.backward() optimizer.minimize(avg_loss) diff --git a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_static.py b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_static.py index f9fcb6f77d..beca81e704 100644 --- a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_static.py +++ b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_iterable_dataset_static.py @@ -80,7 +80,12 @@ def simple_fc_net_static(): bias_attr=bias_attr, ) loss = paddle.mean( - fluid.layers.cross_entropy(input=predict_label, label=label) + paddle.nn.functional.cross_entropy( + input=predict_label, + label=label, + reduction='none', + use_softmax=False, + ) ) optimizer = fluid.optimizer.Adam() diff --git a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_static.py b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_static.py index 7321e4d137..e63cf6694a 100644 --- a/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_static.py +++ b/python/paddle/fluid/tests/unittests/test_multiprocess_dataloader_static.py @@ -80,7 +80,12 @@ def simple_fc_net_static(): bias_attr=bias_attr, ) loss = paddle.mean( - fluid.layers.cross_entropy(input=predict_label, label=label) + paddle.nn.functional.cross_entropy( + input=predict_label, + label=label, + reduction='none', + use_softmax=False, + ) ) optimizer = fluid.optimizer.Adam() -- GitLab