From 7dcefc4067ae147fa8c462e20b7bea0692400097 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 8 Dec 2022 16:39:52 +0800 Subject: [PATCH] [remove fluid.layers.cross_entropy] remove unit tests (part 1) (#48726) * replace layers.cross_entropy with paddle.entropy * fix args * fix codestyle --- .../contrib/slim/tests/imperative_test_utils.py | 4 +++- .../fluid/contrib/slim/tests/test_graph.py | 4 +++- .../contrib/slim/tests/test_imperative_qat.py | 4 +++- .../slim/tests/test_imperative_qat_amp.py | 8 ++++++-- .../slim/tests/test_imperative_qat_lsq.py | 4 +++- .../slim/tests/test_quantization_mkldnn_pass.py | 4 +++- .../contrib/slim/tests/test_quantization_pass.py | 16 ++++++++++++---- .../slim/tests/test_quantization_scale_pass.py | 4 +++- .../slim/tests/test_quantize_transpiler_v2.py | 4 +++- .../slim/tests/test_user_defined_quantization.py | 4 +++- .../contrib/tests/test_quantize_transpiler.py | 12 +++++++++--- .../contrib/tests/test_weight_decay_extend.py | 4 +++- .../fluid/incubate/fleet/tests/fleet_deep_ctr.py | 4 +++- .../tests/book/notest_understand_sentiment.py | 8 ++++++-- .../tests/book/test_image_classification.py | 4 +++- .../fluid/tests/book/test_recognize_digits.py | 4 +++- .../fluid/tests/book/test_word2vec_book.py | 7 ++++++- python/paddle/fluid/tests/test_error_clip.py | 4 +++- 18 files changed, 78 insertions(+), 25 deletions(-) diff --git a/python/paddle/fluid/contrib/slim/tests/imperative_test_utils.py b/python/paddle/fluid/contrib/slim/tests/imperative_test_utils.py index c0637bc7de4..744bd369030 100644 --- a/python/paddle/fluid/contrib/slim/tests/imperative_test_utils.py +++ b/python/paddle/fluid/contrib/slim/tests/imperative_test_utils.py @@ -70,7 +70,9 @@ def train_lenet(lenet, reader, optimizer): label = paddle.to_tensor(y_data) out = lenet(img) - 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() diff --git a/python/paddle/fluid/contrib/slim/tests/test_graph.py b/python/paddle/fluid/contrib/slim/tests/test_graph.py index 1b692bcaafb..ed4b4f2c437 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_graph.py +++ b/python/paddle/fluid/contrib/slim/tests/test_graph.py @@ -47,7 +47,9 @@ def conv_block(): act="relu", ) 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) return [img, label], avg_loss diff --git a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat.py b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat.py index 0e0fbd752b2..cbf5b30976d 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat.py +++ b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat.py @@ -130,7 +130,9 @@ class TestImperativeQat(unittest.TestCase): label = fluid.dygraph.to_variable(y_data) out = lenet(img) acc = paddle.static.accuracy(out, label) - 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() adam.minimize(avg_loss) diff --git a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_amp.py b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_amp.py index d01fc2e63cf..ea0a38ea551 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_amp.py +++ b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_amp.py @@ -119,7 +119,9 @@ class TestImperativeQatAmp(unittest.TestCase): with paddle.amp.auto_cast(): out = model(img) acc = paddle.static.accuracy(out, label) - 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) scaled_loss = scaler.scale(avg_loss) scaled_loss.backward() @@ -129,7 +131,9 @@ class TestImperativeQatAmp(unittest.TestCase): else: out = model(img) acc = paddle.static.accuracy(out, label) - 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() diff --git a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_lsq.py b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_lsq.py index 2b06ee5bf06..caf9648b3c3 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_lsq.py +++ b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_lsq.py @@ -171,7 +171,9 @@ class TestImperativeQatLSQ(unittest.TestCase): label = fluid.dygraph.to_variable(y_data) out = lenet(img) acc = paddle.static.accuracy(out, label) - 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() diff --git a/python/paddle/fluid/contrib/slim/tests/test_quantization_mkldnn_pass.py b/python/paddle/fluid/contrib/slim/tests/test_quantization_mkldnn_pass.py index a89042c0b59..fa9f5d97ae5 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_quantization_mkldnn_pass.py +++ b/python/paddle/fluid/contrib/slim/tests/test_quantization_mkldnn_pass.py @@ -47,7 +47,9 @@ def conv_net(img, label): act="relu", ) 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) return avg_loss diff --git a/python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py b/python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py index f49d019bc17..fd50040b436 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py +++ b/python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py @@ -39,7 +39,9 @@ def linear_fc(num): hidden = data for _ in range(num): hidden = fluid.layers.fc(hidden, size=128, act='relu') - loss = fluid.layers.cross_entropy(input=hidden, label=label) + loss = paddle.nn.functional.cross_entropy( + input=hidden, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss @@ -87,7 +89,9 @@ def residual_block(num, quant_skip_pattern=None): input=hidden, pool_size=2, pool_type='avg', pool_stride=2 ) fc = fluid.layers.fc(input=pool, size=10) - loss = fluid.layers.cross_entropy(input=fc, label=label) + loss = paddle.nn.functional.cross_entropy( + input=fc, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss @@ -115,7 +119,9 @@ def conv_net(img, label, quant_skip_pattern): hidden = fluid.layers.fc(input=conv_pool_2, size=100, act='relu') with fluid.name_scope(quant_skip_pattern): 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 @@ -756,7 +762,9 @@ def quant_dequant_residual_block(num, quant_skip_pattern=None): ) pool_add = paddle.nn.functional.relu(paddle.add(x=pool1, y=pool2)) fc = fluid.layers.fc(input=pool_add, size=10) - loss = fluid.layers.cross_entropy(input=fc, label=label) + loss = paddle.nn.functional.cross_entropy( + input=fc, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss diff --git a/python/paddle/fluid/contrib/slim/tests/test_quantization_scale_pass.py b/python/paddle/fluid/contrib/slim/tests/test_quantization_scale_pass.py index d19b62a3762..49393b5a89f 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_quantization_scale_pass.py +++ b/python/paddle/fluid/contrib/slim/tests/test_quantization_scale_pass.py @@ -55,7 +55,9 @@ def conv_net(img, label): ) hidden = fluid.layers.fc(input=conv_pool_2, size=100, 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 diff --git a/python/paddle/fluid/contrib/slim/tests/test_quantize_transpiler_v2.py b/python/paddle/fluid/contrib/slim/tests/test_quantize_transpiler_v2.py index 3b487ae1fae..0c5c957f776 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_quantize_transpiler_v2.py +++ b/python/paddle/fluid/contrib/slim/tests/test_quantize_transpiler_v2.py @@ -52,7 +52,9 @@ def conv_net(img, label): with fluid.name_scope("skip_quant"): hidden = fluid.layers.fc(input=conv_pool_1, size=100, 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 diff --git a/python/paddle/fluid/contrib/slim/tests/test_user_defined_quantization.py b/python/paddle/fluid/contrib/slim/tests/test_user_defined_quantization.py index fc5d18227b9..fad7ce76a2e 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_user_defined_quantization.py +++ b/python/paddle/fluid/contrib/slim/tests/test_user_defined_quantization.py @@ -57,7 +57,9 @@ def conv_net(img, label): ) hidden = fluid.layers.fc(input=conv_pool_2, size=100, 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 diff --git a/python/paddle/fluid/contrib/tests/test_quantize_transpiler.py b/python/paddle/fluid/contrib/tests/test_quantize_transpiler.py index b2f166def07..b890d995a3e 100644 --- a/python/paddle/fluid/contrib/tests/test_quantize_transpiler.py +++ b/python/paddle/fluid/contrib/tests/test_quantize_transpiler.py @@ -30,7 +30,9 @@ def linear_fc(num): hidden = data for _ in range(num): hidden = fluid.layers.fc(hidden, size=128, act='relu') - loss = fluid.layers.cross_entropy(input=hidden, label=label) + loss = paddle.nn.functional.cross_entropy( + input=hidden, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss @@ -58,7 +60,9 @@ def residual_block(num): short = conv_bn_layer(hidden, 16, 1, 1, 0, act=None) hidden = paddle.nn.functional.relu(paddle.add(x=conv, y=short)) fc = fluid.layers.fc(input=hidden, size=10) - loss = fluid.layers.cross_entropy(input=fc, label=label) + loss = paddle.nn.functional.cross_entropy( + input=fc, label=label, reduction='none', use_softmax=False + ) loss = paddle.mean(loss) return loss @@ -82,7 +86,9 @@ def conv_net(img, label): act="relu", ) 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) return avg_loss diff --git a/python/paddle/fluid/contrib/tests/test_weight_decay_extend.py b/python/paddle/fluid/contrib/tests/test_weight_decay_extend.py index 7b40d513f2e..4f32eaffc3e 100644 --- a/python/paddle/fluid/contrib/tests/test_weight_decay_extend.py +++ b/python/paddle/fluid/contrib/tests/test_weight_decay_extend.py @@ -86,7 +86,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/incubate/fleet/tests/fleet_deep_ctr.py b/python/paddle/fluid/incubate/fleet/tests/fleet_deep_ctr.py index 3c034d60f37..e4f3cc959f3 100644 --- a/python/paddle/fluid/incubate/fleet/tests/fleet_deep_ctr.py +++ b/python/paddle/fluid/incubate/fleet/tests/fleet_deep_ctr.py @@ -150,7 +150,9 @@ def model(): 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) return datas, avg_cost, predict, train_file_path diff --git a/python/paddle/fluid/tests/book/notest_understand_sentiment.py b/python/paddle/fluid/tests/book/notest_understand_sentiment.py index 3809e797f3c..1d952bffc4d 100644 --- a/python/paddle/fluid/tests/book/notest_understand_sentiment.py +++ b/python/paddle/fluid/tests/book/notest_understand_sentiment.py @@ -47,7 +47,9 @@ def convolution_net( prediction = fluid.layers.fc( input=[conv_3, conv_4], 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(cost) accuracy = paddle.static.accuracy(input=prediction, label=label) return avg_cost, accuracy, prediction @@ -82,7 +84,9 @@ def stacked_lstm_net( prediction = fluid.layers.fc( input=[fc_last, lstm_last], 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(cost) accuracy = paddle.static.accuracy(input=prediction, label=label) return avg_cost, accuracy, prediction diff --git a/python/paddle/fluid/tests/book/test_image_classification.py b/python/paddle/fluid/tests/book/test_image_classification.py index 77a59bc0370..dbfbe00f2ff 100644 --- a/python/paddle/fluid/tests/book/test_image_classification.py +++ b/python/paddle/fluid/tests/book/test_image_classification.py @@ -119,7 +119,9 @@ def train(net_type, use_cuda, save_dirname, is_local): raise ValueError("%s network is not supported" % net_type) predict = fluid.layers.fc(input=net, size=classdim, 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) acc = paddle.static.accuracy(input=predict, label=label) diff --git a/python/paddle/fluid/tests/book/test_recognize_digits.py b/python/paddle/fluid/tests/book/test_recognize_digits.py index b96ff994098..b799dc74679 100644 --- a/python/paddle/fluid/tests/book/test_recognize_digits.py +++ b/python/paddle/fluid/tests/book/test_recognize_digits.py @@ -30,7 +30,9 @@ BATCH_SIZE = 64 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) acc = paddle.static.accuracy(input=prediction, label=label) return prediction, avg_loss, acc diff --git a/python/paddle/fluid/tests/book/test_word2vec_book.py b/python/paddle/fluid/tests/book/test_word2vec_book.py index ab89e79f60a..640a33751a4 100644 --- a/python/paddle/fluid/tests/book/test_word2vec_book.py +++ b/python/paddle/fluid/tests/book/test_word2vec_book.py @@ -96,7 +96,12 @@ def train( predict_word = fluid.layers.fc( input=hidden1, size=dict_size, act='softmax' ) - cost = fluid.layers.cross_entropy(input=predict_word, label=words[4]) + cost = paddle.nn.functional.cross_entropy( + input=predict_word, + label=words[4], + reduction='none', + use_softmax=False, + ) avg_cost = paddle.mean(cost) return avg_cost, predict_word diff --git a/python/paddle/fluid/tests/test_error_clip.py b/python/paddle/fluid/tests/test_error_clip.py index f6e1122e045..dcfe477a76b 100644 --- a/python/paddle/fluid/tests/test_error_clip.py +++ b/python/paddle/fluid/tests/test_error_clip.py @@ -31,7 +31,9 @@ with fluid.program_guard(main_program=prog): label = fluid.layers.data(name='y', shape=[1], dtype='int64') - 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() -- GitLab