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 c0637bc7de480..744bd3690309b 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 1b692bcaafb0e..ed4b4f2c43747 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 0e0fbd752b287..cbf5b30976d8c 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 @@ def func_qat(self): 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 d01fc2e63cfd1..ea0a38ea5519a 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 @@ def model_train(self, model, batch_num=-1, batch_size=32, use_amp=False): 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 @@ def model_train(self, model, batch_num=-1, batch_size=32, use_amp=False): 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 2b06ee5bf06d8..caf9648b3c336 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 @@ def func_qat(self): 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 a89042c0b5959..fa9f5d97ae5f0 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 f49d019bc1752..fd50040b436ac 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 conv_bn_layer( 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 conv_bn_layer( ) 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 d19b62a376279..49393b5a89f12 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 3b487ae1fae2b..0c5c957f77644 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 fc5d18227b92a..fad7ce76a2e34 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 b2f166def0798..b890d995a3ec7 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 conv_bn_layer( 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 7b40d513f2e5c..4f32eaffc3e52 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 3c034d60f3753..e4f3cc959f314 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 3809e797f3c7c..1d952bffc4d33 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 77a59bc037037..dbfbe00f2ffb6 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 b96ff9940985b..b799dc74679bd 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 @@ 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 ab89e79f60a89..640a33751a474 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 __network__(words): 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 f6e1122e04562..dcfe477a76b3e 100644 --- a/python/paddle/fluid/tests/test_error_clip.py +++ b/python/paddle/fluid/tests/test_error_clip.py @@ -31,7 +31,9 @@ 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()