Skip to content

Commit

Permalink
replace cross_entropy except in python/paddle/fluid/tests/unittests/*…
Browse files Browse the repository at this point in the history
….py && unittests/*/*.py (PaddlePaddle#48922)
  • Loading branch information
kangguangli authored Dec 9, 2022
1 parent 59ddcb5 commit 424c612
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import paddle.utils as utils
from paddle.distributed import fleet
from paddle.distributed.fleet import auto
from paddle.fluid import layers

paddle.enable_static()
_global_parallel_strategy = None
Expand Down Expand Up @@ -93,7 +92,9 @@ def mlp_pretrain_forward(train_program, start_program):

predict = mlp(input)

cost = 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 avg_cost, train_program, start_program
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def get_model(self, batch_size=2):

# 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ def run_one_loop(self, model, opt, data):

out = model(img)
softmax_out = paddle.nn.functional.softmax(out, use_cudnn=False)
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)
return avg_loss

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None):
# Train program
predict = cnn_model(images)
with fluid.device_guard("gpu:1"):
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None):
# Train program
predict = cnn_model(images)
with fluid.device_guard("gpu:1"):
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None):
# Train program
predict = cnn_model(images)
with fluid.device_guard("gpu:0"):
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def _test_checkpoint(self, fs, dir_path):
feed_list=[image, label], place=fluid.CPUPlace()
)
predict = fluid.layers.fc(input=image, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=predict, label=label)
loss = paddle.nn.functional.cross_entropy(
input=predict, label=label, reduction='none', use_softmax=False
)
avg_loss = paddle.mean(loss)
optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.001)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,9 @@ def train_resnet(self, enable_amp=True, level='O1'):
):
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,9 @@ def train_resnet(self, enable_amp=True, level='O1'):
):
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ def network():
elif self.conv_groups == 4:
cout = paddle.reshape(conv_out, shape=[1, 1, 10816])
result = fluid.layers.relu(cout)
loss = fluid.layers.cross_entropy(input=result, label=label_shape)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=label_shape,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down Expand Up @@ -156,7 +161,12 @@ def network():
)
cout = paddle.reshape(conv_out, shape=[1, 1, 10816])
result = fluid.layers.relu(cout)
loss = fluid.layers.cross_entropy(input=result, label=label_shape)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=label_shape,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down Expand Up @@ -257,7 +267,12 @@ def network():
elif self.conv_groups == 4:
cout = paddle.reshape(conv_out, shape=[1, 1, 10816])
result = fluid.layers.relu(cout)
loss = fluid.layers.cross_entropy(input=result, label=label_shape)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=label_shape,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def network():
act="relu",
)
result = fluid.layers.relu(fc_out)
loss = fluid.layers.cross_entropy(input=result, label=self.label)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=self.label,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down Expand Up @@ -105,7 +110,12 @@ def network():
)
c_out = paddle.reshape(fc_out, shape=[0, 784])
result = fluid.layers.relu(c_out)
loss = fluid.layers.cross_entropy(input=result, label=self.label)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=self.label,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down Expand Up @@ -174,7 +184,12 @@ def network():
)
c_out = paddle.reshape(fc_out, shape=[1, 1, 2744])
result = fluid.layers.relu(c_out)
loss = fluid.layers.cross_entropy(input=result, label=label_shape)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=label_shape,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ def network():
act=None,
)
result = fluid.layers.relu(fc_out)
loss = fluid.layers.cross_entropy(input=result, label=self.label)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=self.label,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down Expand Up @@ -144,7 +149,12 @@ def network():
act=None,
)
result = fluid.layers.relu(fc_out)
loss = fluid.layers.cross_entropy(input=result, label=self.label)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=self.label,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down Expand Up @@ -240,7 +250,12 @@ def network():
act=None,
)
result = fluid.layers.relu(fc_out)
loss = fluid.layers.cross_entropy(input=result, label=self.label)
loss = paddle.nn.functional.cross_entropy(
input=result,
label=self.label,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
return avg_loss, result

Expand Down

0 comments on commit 424c612

Please sign in to comment.