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 5) #48920

Merged
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
7 changes: 6 additions & 1 deletion python/paddle/fluid/tests/unittests/asp/asp_pruning_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ def run_inference_pruning_test(
def run_training_pruning_test(self, get_mask_gen_func, get_mask_check_func):
with fluid.program_guard(self.main_program, self.startup_program):
loss = paddle.mean(
fluid.layers.cross_entropy(input=self.predict, label=self.label)
paddle.nn.functional.cross_entropy(
input=self.predict,
label=self.label,
reduction='none',
use_softmax=False,
)
)
optimizer = paddle.incubate.asp.decorate(
fluid.optimizer.SGD(learning_rate=0.01)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ def test_inference_pruning(self):
def test_training_pruning(self):
with fluid.program_guard(self.main_program, self.startup_program):
loss = paddle.mean(
fluid.layers.cross_entropy(input=self.predict, label=self.label)
paddle.nn.functional.cross_entropy(
input=self.predict,
label=self.label,
reduction='none',
use_softmax=False,
)
)
optimizer = sparsity.decorate(
fluid.optimizer.SGD(learning_rate=0.01)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ def build_model():
with fluid.program_guard(self.main_program, self.startup_program):
self.img, self.label, predict = build_model()
self.loss = paddle.mean(
fluid.layers.cross_entropy(input=predict, label=self.label)
paddle.nn.functional.cross_entropy(
input=predict,
label=self.label,
reduction='none',
use_softmax=False,
)
)
self.optimizer = fluid.optimizer.SGD(learning_rate=0.01)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ def test_inference_pruning(self):
def test_training_pruning(self):
with fluid.program_guard(self.main_program, self.startup_program):
loss = paddle.mean(
fluid.layers.cross_entropy(input=self.predict, label=self.label)
paddle.nn.functional.cross_entropy(
input=self.predict,
label=self.label,
reduction='none',
use_softmax=False,
)
)
optimizer = paddle.incubate.asp.decorate(
fluid.optimizer.SGD(learning_rate=0.01)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ def build_model():
with fluid.program_guard(self.main_program, self.startup_program):
self.img, self.label, predict = build_model()
self.loss = paddle.mean(
fluid.layers.cross_entropy(input=predict, label=self.label)
paddle.nn.functional.cross_entropy(
input=predict,
label=self.label,
reduction='none',
use_softmax=False,
)
)
self.optimizer = fluid.optimizer.SGD(learning_rate=0.01)
self.optimizer = paddle.incubate.asp.decorate(self.optimizer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ def net(self, main_prog, startup_prog):
fc_3 = fluid.layers.fc(input=fc_2, size=64, act='tanh')
fc_4 = fluid.layers.fc(input=fc_3, size=64, act='tanh')
prediction = fluid.layers.fc(input=fc_4, size=2, act='softmax')
cost = fluid.layers.cross_entropy(input=prediction, label=input_y)
cost = paddle.nn.functional.cross_entropy(
input=prediction,
label=input_y,
reduction='none',
use_softmax=False,
)
avg_cost = paddle.mean(x=cost)

dist_strategy = paddle.distributed.fleet.DistributedStrategy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def net(self, main_prog, startup_prog):

fc_1 = fluid.layers.fc(input=input_x, size=64, act='tanh')
prediction = fluid.layers.fc(input=fc_1, size=2, act='softmax')
cost = fluid.layers.cross_entropy(input=prediction, label=input_y)
cost = paddle.nn.functional.cross_entropy(
input=prediction,
label=input_y,
reduction='none',
use_softmax=False,
)
avg_cost = paddle.mean(x=cost)

strategy = paddle.distributed.fleet.DistributedStrategy()
Expand Down Expand Up @@ -122,7 +127,12 @@ def net(self, main_prog, startup_prog):

fc_1 = fluid.layers.fc(input=input_x, size=64, act='tanh')
prediction = fluid.layers.fc(input=fc_1, size=2, act='softmax')
cost = fluid.layers.cross_entropy(input=prediction, label=input_y)
cost = paddle.nn.functional.cross_entropy(
input=prediction,
label=input_y,
reduction='none',
use_softmax=False,
)
avg_cost = paddle.mean(x=cost)

strategy = paddle.distributed.fleet.DistributedStrategy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def add_fn(x):


def loss_fn(x, lable):
loss = fluid.layers.cross_entropy(x, lable)
loss = paddle.nn.functional.cross_entropy(
x, lable, reduction='none', use_softmax=False
)
return loss


Expand All @@ -45,7 +47,9 @@ def dyfunc_with_if_else(x_v, label=None):
x_v = x_v + 1
# plain if in python
if label is not None:
loss = fluid.layers.cross_entropy(x_v, label)
loss = paddle.nn.functional.cross_entropy(
x_v, label, reduction='none', use_softmax=False
)
return loss
return x_v

Expand Down Expand Up @@ -302,7 +306,9 @@ def if_with_and_or(x_v, label=None):
x_v = x_v + 1

if label is not None:
loss = fluid.layers.cross_entropy(x_v, label)
loss = paddle.nn.functional.cross_entropy(
x_v, label, reduction='none', use_softmax=False
)
return loss
return x_v

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def forward(self, inputs, label=None):
x = self.inference(inputs)
if label is not None:
acc = paddle.static.accuracy(input=x, label=label)
loss = fluid.layers.cross_entropy(x, label)
loss = paddle.nn.functional.cross_entropy(
x, label, reduction='none', use_softmax=False
)
avg_loss = paddle.mean(loss)

return x, acc, avg_loss
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def set_args_1(__args):

def true_fn_1():
nonlocal __return_0, __return_1, __return_value_0, loss
loss = fluid.layers.cross_entropy(x_v, label)
loss = paddle.nn.functional.cross_entropy(
x_v, label, reduction='none', use_softmax=False
)
__return_0 = _jst.create_bool_as_type(label is not None, True)
__return_value_0 = loss
return
Expand Down Expand Up @@ -178,7 +180,9 @@ def set_args_3(__args):

def true_fn_3():
nonlocal __return_2, __return_3, __return_value_1, loss
loss = fluid.layers.cross_entropy(x_v, label)
loss = paddle.nn.functional.cross_entropy(
x_v, label, reduction='none', use_softmax=False
)
__return_2 = _jst.create_bool_as_type(label is not None, True)
__return_value_1 = loss
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ def train(self, to_static, build_strategy=None):
img, label = data

pred = resnet(img)
loss = fluid.layers.cross_entropy(input=pred, label=label)
loss = paddle.nn.functional.cross_entropy(
input=pred,
label=label,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(x=loss)
acc_top1 = paddle.static.accuracy(
input=pred, label=label, k=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ def train(to_static, build_strategy=None):
# FIXME(Aurelius84): The followding cross_entropy seems to bring out a
# precision problem, need to figure out the underlying reason.
# If we remove it, the loss between dygraph and dy2stat is exactly same.
loss = fluid.layers.cross_entropy(input=pred, label=label)
loss = paddle.nn.functional.cross_entropy(
input=pred,
label=label,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(x=pred)
acc_top1 = paddle.static.accuracy(input=pred, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=pred, label=label, k=5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def train(to_static, build_strategy=None):
level='O2',
):
pred = resnet(img)
loss = fluid.layers.cross_entropy(input=pred, label=label)
loss = paddle.nn.functional.cross_entropy(
input=pred, label=label, reduction='none', use_softmax=False
)
avg_loss = paddle.mean(x=pred)
acc_top1 = paddle.static.accuracy(input=pred, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=pred, label=label, k=5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ def forward(self, inputs, label):
out = self.out(y)

softmax_out = paddle.nn.functional.softmax(out)
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)

acc_top1 = paddle.static.accuracy(input=softmax_out, label=label, k=1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def forward(self, inputs, label=None):
prediction = self._fc_prediction(fc_1)
prediction = self._fc1_act(prediction)

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)
acc = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc
Expand Down Expand Up @@ -150,7 +152,9 @@ def forward(self, inputs, label=None):
prediction = self._fc_prediction(fc_2)
prediction = paddle.nn.functional.softmax(prediction)

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)
acc = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc
Expand Down Expand Up @@ -196,7 +200,9 @@ def forward(self, inputs, label=None):
fc_2 = paddle.tanh(fc_2)
prediction = self._fc_prediction(fc_2)
prediction = paddle.nn.functional.softmax(prediction)
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)
acc = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc
Expand Down Expand Up @@ -255,7 +261,9 @@ def forward(self, inputs, label=None):
prediction = paddle.nn.functional.softmax(prediction)
# TODO(Aurelius84): Uncomment the following codes when we support return variable-length vars.
# if label is not None:
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)
acc = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def forward(self, x, target=None):
x = paddle.flatten(x, 1, -1)
if target is not None:
x = paddle.nn.functional.softmax(x)
loss = paddle.fluid.layers.cross_entropy(x, target)
loss = paddle.paddle.nn.functional.cross_entropy(
x, target, reduction='none', use_softmax=False
)
if self.use_ipu:
loss = paddle.incubate.identity_loss(loss, 1)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def forward(self, x, target=None):
if self.loss_op:
loss = self.loss_op(x, target)
else:
loss = paddle.fluid.layers.cross_entropy(x, target)
loss = paddle.paddle.nn.functional.cross_entropy(
x, target, reduction='none', use_softmax=False
)
if self.use_reduction:
loss = paddle.mean(loss)
if self.use_identity_loss:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def forward(self, x, target=None):
x = paddle.flatten(x, 1, -1)
if target is not None:
x = paddle.nn.functional.softmax(x)
loss = paddle.fluid.layers.cross_entropy(x, target)
loss = paddle.paddle.nn.functional.cross_entropy(
x, target, reduction='none', use_softmax=False
)
return x, loss
return x

Expand Down
4 changes: 3 additions & 1 deletion python/paddle/fluid/tests/unittests/ipu/test_print_op_ipu.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ def forward(self, x, target=None):
x = paddle.flatten(x, 1, -1)
if target is not None:
x = paddle.nn.functional.softmax(x)
loss = paddle.fluid.layers.cross_entropy(x, target)
loss = paddle.paddle.nn.functional.cross_entropy(
x, target, reduction='none', use_softmax=False
)
loss = paddle.incubate.identity_loss(loss, 1)
return x, loss
return x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def _test(self, run_mlu=True):
fc_1 = fluid.layers.fc(input=z, size=128)
prediction = fluid.layers.fc(input=fc_1, size=2, 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)
loss = paddle.mean(cost)
adam = fluid.optimizer.Adam(learning_rate=0.01)
adam.minimize(loss)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def _test(self, run_mlu=True):
fc_1 = fluid.layers.fc(input=z, size=128)
prediction = fluid.layers.fc(input=fc_1, size=2, 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)
loss = paddle.mean(cost)
adam = paddle.optimizer.AdamW(learning_rate=0.01, weight_decay=0.02)
adam.minimize(loss)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def _test(self, run_mlu=True):
fc_1 = fluid.layers.fc(input=c, size=128)
prediction = fluid.layers.fc(input=fc_1, size=2, 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)
loss = paddle.mean(cost)
sgd = fluid.optimizer.SGD(learning_rate=0.01)
sgd.minimize(loss)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _test(self, run_mlu=True):
fc_1 = fluid.layers.fc(input=c, size=128)
prediction = fluid.layers.fc(input=fc_1, size=2, 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)
loss = paddle.mean(cost)
sgd = fluid.optimizer.SGD(learning_rate=0.01)
sgd.minimize(loss)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _test(self, run_mlu=True):
fc_1_gelu = paddle.nn.functional.gelu(fc_1)
prediction = fluid.layers.fc(input=fc_1_gelu, size=2, 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)
loss = paddle.mean(cost)
sgd = fluid.optimizer.SGD(learning_rate=0.01)
sgd.minimize(loss)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _test(self, run_mlu=True):
fc_1 = fluid.layers.fc(input=y, size=128)
prediction = fluid.layers.fc(input=fc_1, size=2, 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)
loss = paddle.mean(cost)
sgd = fluid.optimizer.SGD(learning_rate=0.01)
sgd.minimize(loss)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _test(self, run_mlu=True):
fc_1 = fluid.layers.fc(input=z, size=128)
prediction = fluid.layers.fc(input=fc_1, size=2, 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)
loss = paddle.mean(cost)
sgd = fluid.optimizer.SGD(learning_rate=0.01)
sgd.minimize(loss)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _test(self, run_mlu=True):
fc_1 = fluid.layers.fc(input=z, size=128)
prediction = fluid.layers.fc(input=fc_1, size=2, 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)
loss = paddle.mean(cost)
sgd = fluid.optimizer.SGD(learning_rate=0.01)
sgd.minimize(loss)
Expand Down
Loading