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 7) #49012

Merged
merged 1 commit into from
Dec 13, 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 @@ -173,7 +173,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 @@ -42,8 +42,11 @@ def net(self, main_prog, startup_prog, dtype='float32'):
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ def node_func():
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down Expand Up @@ -135,8 +138,11 @@ def node_func():
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down Expand Up @@ -210,8 +216,11 @@ def node_func():
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down Expand Up @@ -272,8 +281,11 @@ def node_func():
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ def node_func():
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def net(self, main_prog, startup_prog):
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down Expand Up @@ -122,8 +125,8 @@ def test_lamb_apply_with_amp(self):
fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh')
fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh')
prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax')
cost = paddle.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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def net(self, main_prog, startup_prog):
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down Expand Up @@ -127,8 +130,8 @@ def test_lars_apply_with_amp(self):
fc_1 = paddle.fluid.layers.fc(input=input_x, size=64, act='tanh')
fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh')
prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax')
cost = paddle.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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ def net(main_prog, startup_prog):
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ def net(self):
prediction = paddle.fluid.layers.fc(
input=[fc_2], size=2, act='softmax'
)
cost = paddle.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)
return avg_cost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ def test_pipeline_optimizer(self):
prediction = paddle.fluid.layers.fc(
input=[fc_7], size=2, act='softmax'
)
cost = paddle.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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def test_pipeline_optimizer(self):

fc_2 = paddle.fluid.layers.fc(input=fc_1, size=64, act='tanh')
prediction = paddle.fluid.layers.fc(input=[fc_2], size=2, act='softmax')
cost = paddle.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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,11 @@ def train_mobilenet(args, to_static):

t_end = time.time()
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=out, label=label, k=1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,12 @@ def train(args, fake_data_reader, to_static):
labels = to_variable(y_data)
labels.stop_gradient = True
outputs = video_model(imgs)
loss = fluid.layers.cross_entropy(
input=outputs, label=labels, ignore_index=-1
loss = paddle.nn.functional.cross_entropy(
input=outputs,
label=labels,
ignore_index=-1,
reduction='none',
use_softmax=False,
)
avg_loss = paddle.mean(loss)
acc_top1 = paddle.static.accuracy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ def build_model(self, on_ipu):
label = paddle.static.data(
name=self.feed_list[1], shape=self.feed_shape[1], dtype='int64'
)
out = paddle.fluid.layers.cross_entropy(
input=x, label=label, **self.attrs
out = paddle.nn.functional.cross_entropy(
input=x,
label=label,
reduction='none',
use_softmax=False,
**self.attrs
)
self.fetch_list = [out.name]

Expand Down
11 changes: 4 additions & 7 deletions python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,9 @@ def forward(self, x, target=None):
if target is not None:
if self.use_softmax:
x = paddle.nn.functional.softmax(x)
if self.loss_op:
loss = self.loss_op(x, target)
else:
loss = paddle.paddle.nn.functional.cross_entropy(
x, target, reduction='none', use_softmax=False
)
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 All @@ -69,7 +66,7 @@ def setUp(self):
self.set_data_feed()

def set_op_attrs(self):
self.loss_op = paddle.fluid.layers.cross_entropy
pass

def set_data_feed(self):
self.data = paddle.uniform((8, 3, 10, 10), dtype='float32')
Expand Down
24 changes: 20 additions & 4 deletions python/paddle/fluid/tests/unittests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,9 @@ def make_recognize_digits_mlp(self):
act='softmax',
param_attr=["sftmax.w1", "sftmax.w2"],
)
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(cost)
return avg_cost

Expand Down Expand Up @@ -2061,7 +2063,9 @@ def make_recognize_digits_conv(self):
)

predict = layers.fc(input=conv_pool_2, size=10, act="softmax")
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(cost)
return avg_cost

Expand Down Expand Up @@ -2114,7 +2118,12 @@ def make_word_embedding(self):
predict_word = layers.fc(
input=hidden1, size=dict_size, act='softmax'
)
cost = layers.cross_entropy(input=predict_word, label=next_word)
cost = paddle.nn.functional.cross_entropy(
input=predict_word,
label=next_word,
reduction='none',
use_softmax=False,
)
avg_cost = paddle.mean(cost)
return avg_cost

Expand Down Expand Up @@ -2346,7 +2355,14 @@ def make_cross_entropy(self):
x = self._get_data(name="x", shape=[30, 10], dtype="float32")
label = self._get_data(name="label", shape=[30, 1], dtype="int64")
mode = 'channel'
out = layers.cross_entropy(x, label, False, 4)
out = paddle.nn.functional.cross_entropy(
x,
label,
soft_label=False,
ignore_index=4,
reduction='none',
use_softmax=False,
)
return out

def make_uniform_random_batch_size_like(self):
Expand Down