未验证 提交 b01f979b 编写于 作者: K kangguangli 提交者: GitHub

replace cross_entropy in python/paddle/fluid/tests/unittests/*.py except test*.py (#48919)

上级 8b10773b
......@@ -77,7 +77,9 @@ class TestDistMnist2x2(TestDistRunnerBase):
# 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
......
......@@ -103,7 +103,9 @@ class TestDistCTR2x2(TestDistRunnerBase):
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)
inference_program = paddle.fluid.default_main_program().clone()
......
......@@ -154,7 +154,9 @@ class TestDistCTR2x2(FleetDistRunnerBase):
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)
self.feeds = datas
......
......@@ -123,7 +123,9 @@ class TestHeterPipelinePsCTR2x2(FleetDistHeterRunnerBase):
label = fluid.layers.cast(label, dtype="int64")
predict = fluid.layers.fc(input=merge_layer, size=2, 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(x=cost)
fluid.layers.Print(avg_cost, message="avg_cost")
......
......@@ -79,7 +79,9 @@ class TestFleetMetaOptimizerPrecision(TestDistRunnerBase):
# 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
......
......@@ -79,7 +79,9 @@ class TestFleetMetaOptimizerFuseAllReducePrecision(TestDistRunnerBase):
# 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
......
......@@ -140,7 +140,9 @@ class TestDistCTR2x2(FleetDistRunnerBase):
acc = paddle.static.accuracy(input=predict, label=label)
auc_var, _, _ = 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)
self.feeds = datas
......
......@@ -78,7 +78,9 @@ class TestDistMnist2x2(TestDistRunnerBase):
# 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
......
......@@ -43,7 +43,9 @@ class TestDistMnist2x2(TestDistRunnerBase):
# 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
......
......@@ -37,7 +37,9 @@ class TestDistMnist2x2(TestDistRunnerBase):
# 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
......
......@@ -34,7 +34,9 @@ class TestDistMnist2x2(TestDistRunnerBase):
# 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
......
......@@ -214,7 +214,9 @@ class DistSeResneXt2x2(TestDistRunnerBase):
# Train program
model = SE_ResNeXt(layers=50)
out = model.net(input=image, class_dim=102)
cost = fluid.layers.cross_entropy(input=out, label=label)
cost = paddle.nn.functional.cross_entropy(
input=out, label=label, reduction='none', use_softmax=False
)
avg_cost = paddle.mean(x=cost)
acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
......
......@@ -132,7 +132,9 @@ class TestDistTextClassification2x2(TestDistRunnerBase):
# Train program
predict = conv_net(data, dict_dim)
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)
acc = paddle.static.accuracy(input=predict, label=label)
inference_program = fluid.default_main_program().clone()
......
......@@ -112,7 +112,9 @@ def net(batch_size=4, lr=0.01):
label = fluid.layers.cast(label, dtype="int64")
predict = fluid.layers.fc(input=merge_layer, size=2, 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(x=cost)
return datas, avg_cost
......
......@@ -95,7 +95,9 @@ class MNIST(fluid.dygraph.Layer):
x = self._simple_img_conv_pool_2(x)
x = paddle.reshape(x, shape=[-1, self.pool_2_shape])
cost = self._fc(x)
loss = fluid.layers.cross_entropy(self.act(cost), label)
loss = paddle.nn.functional.cross_entropy(
self.act(cost), label, reduction='none', use_softmax=False
)
avg_loss = paddle.mean(loss)
return avg_loss
......
......@@ -170,7 +170,9 @@ def SE_ResNeXt50Small(use_feed):
)
# Classifier layer:
prediction = fluid.layers.fc(input=dropout, size=1000, 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
)
loss = paddle.mean(loss)
return loss
......
......@@ -30,7 +30,9 @@ def simple_fc_net_with_inputs(img, label, class_num=10):
),
)
prediction = fluid.layers.fc(hidden, size=class_num, 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
)
loss = paddle.mean(loss)
return loss
......@@ -56,7 +58,9 @@ def batchnorm_fc_with_inputs(img, label, class_num=10):
hidden = paddle.static.nn.batch_norm(input=hidden)
prediction = fluid.layers.fc(hidden, size=class_num, 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
)
loss = paddle.mean(loss)
return loss
......@@ -93,7 +97,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
......
......@@ -594,6 +594,8 @@ def transformer(
)
predict = paddle.nn.functional.softmax(predict)
cost = layers.cross_entropy(input=predict, label=gold)
cost = paddle.nn.functional.cross_entropy(
input=predict, label=gold, reduction='none', use_softmax=False
)
weighted_cost = cost * weights
return paddle.sum(weighted_cost)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册