Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[remove fluid.layers.cross_entropy] remove unit tests (part 1) #48726

Merged
merged 5 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion python/paddle/fluid/contrib/slim/tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 12 additions & 4 deletions python/paddle/fluid/contrib/slim/tests/test_quantization_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions python/paddle/fluid/contrib/tests/test_quantize_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion python/paddle/fluid/incubate/fleet/tests/fleet_deep_ctr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions python/paddle/fluid/tests/book/notest_understand_sentiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion python/paddle/fluid/tests/book/test_image_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion python/paddle/fluid/tests/book/test_recognize_digits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion python/paddle/fluid/tests/book/test_word2vec_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion python/paddle/fluid/tests/test_error_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down