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 c0637bc7de480e60bc30b40dfaa2cde4f67a5876..744bd3690309b287b65192ac3b795b111c5b9b89 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 1b692bcaafb0ea59f21a6c55cbe09d15dcf4baeb..ed4b4f2c437475f8f702a768b273d15e7e0fb5ee 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 0e0fbd752b2874341f4cb7bded5244aca353fead..cbf5b30976d8c73a4c298224e11c5a67da48ed64 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 d01fc2e63cfd104d2d4d5c90150e7c649f30b28e..ea0a38ea5519a287f78e1091d03a58ee021578c1 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 2b06ee5bf06d87d56503970d6af6c8b4bd010e3e..caf9648b3c3369f966e41f7e68f60c5f77618c04 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 a89042c0b5959e42d236e3ef11d346a6cdba3d0c..fa9f5d97ae5f04b5b40fc7a89e8c6e229b184670 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 f49d019bc17526273f8548079845973504567608..fd50040b436accca0c289edc60d8060c012f2def 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 d19b62a376279dabf450919e15ea5696411c2778..49393b5a89f1268e0861d23a4d104b1ced670a2f 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 3b487ae1fae2b0d3dfb985555738b08ff3180b68..0c5c957f77644118819bff05928f251b174aa578 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 fc5d18227b92a974053aeec09d564627bec8b349..fad7ce76a2e344c2f6ec23cdffed6246a51510a8 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 b2f166def0798655b7a95a08f29035265e23eabe..b890d995a3ec742a91a674b248b8afd76b18a3ae 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 7b40d513f2e5c38ca5d7747fcc0f71a6a70a80a1..4f32eaffc3e52b9d6ab03a57cacb3fc3f2c624af 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 3c034d60f375371eb13af55c3e95b8d2cef0c723..e4f3cc959f314f11ce6dac693ff9ecb1cb68fa3e 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 3809e797f3c7ca5c100043f80a020e54c7b0c361..1d952bffc4d33d586824ec94bca9f04240fa37b9 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 77a59bc0370372e5225ab33f10ca83e98fa4a55d..dbfbe00f2ffb60f3e3f657804fa65cfe66cbf839 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 b96ff9940985bfc25a995356c55e2b8199baef5c..b799dc74679bd862d88337d79be647575e3dad51 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 ab89e79f60a8970671e8412245c1c3d715ea4e34..640a33751a4740969e77bcd4577bc4faa498aa73 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 f6e1122e04562b36e806a01c3da587dbe438cd97..dcfe477a76b3e24ec8df2e02d3fe07121f16d9cf 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()