未验证 提交 529e74e4 编写于 作者: R Roc 提交者: GitHub

[Clean Fluid] replace accuracy and auc and remove get_places, distributions(#48554)

* mv accuracy and auc

* rm distributions

* rm get_places

* replace metric
上级 d3f8ede0
......@@ -148,8 +148,8 @@ def auc(stat_pos, stat_neg, scope=None, util=None):
distributed auc in fleet
Args:
stat_pos(numpy.array|Variable|string): stat_pos in output of fluid.layers.auc
stat_neg(numpy.array|Variable|string): stat_neg in output of fluid.layers.auc
stat_pos(numpy.array|Variable|string): stat_pos in output of paddle.static.auc
stat_neg(numpy.array|Variable|string): stat_neg in output of paddle.static.auc
scope(Scope): specific scope
Returns:
......@@ -163,7 +163,7 @@ def auc(stat_pos, stat_neg, scope=None, util=None):
binary_predict = fluid.layers.concat(
input=[fluid.layers.elementwise_sub(fluid.layers.ceil(similarity_norm), similarity_norm), similarity_norm], axis=1)
self.auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, stat_neg] =
fluid.layers.auc(input=binary_predict, label=label, curve='ROC', num_thresholds=4096)
paddle.static.auc(input=binary_predict, label=label, curve='ROC', num_thresholds=4096)
# in train.py, after train or infer
pos = np.array(scope.find_var(stat_pos.name).get_tensor())
......
......@@ -150,8 +150,8 @@ class TestImperativePTQ(unittest.TestCase):
label = paddle.to_tensor(y_data)
out = model(img)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=out, label=label, k=5)
eval_acc_top1_list.append(float(acc_top1.numpy()))
if batch_id % 50 == 0:
......
......@@ -129,7 +129,7 @@ class TestImperativeQat(unittest.TestCase):
img = fluid.dygraph.to_variable(x_data)
label = fluid.dygraph.to_variable(y_data)
out = lenet(img)
acc = fluid.layers.accuracy(out, label)
acc = paddle.static.accuracy(out, label)
loss = fluid.layers.cross_entropy(out, label)
avg_loss = paddle.mean(loss)
avg_loss.backward()
......@@ -160,10 +160,10 @@ class TestImperativeQat(unittest.TestCase):
label = fluid.dygraph.to_variable(y_data)
out = lenet(img)
acc_top1 = fluid.layers.accuracy(
acc_top1 = paddle.static.accuracy(
input=out, label=label, k=1
)
acc_top5 = fluid.layers.accuracy(
acc_top5 = paddle.static.accuracy(
input=out, label=label, k=5
)
......@@ -200,7 +200,7 @@ class TestImperativeQat(unittest.TestCase):
label = fluid.dygraph.to_variable(y_data)
lenet.eval()
fp32_out = lenet(test_img)
fp32_acc = fluid.layers.accuracy(fp32_out, label).numpy()
fp32_acc = paddle.static.accuracy(fp32_out, label).numpy()
with tempfile.TemporaryDirectory(prefix="qat_save_path_") as tmpdir:
# save inference quantized model
......@@ -237,7 +237,7 @@ class TestImperativeQat(unittest.TestCase):
)
paddle.disable_static()
quant_out = fluid.dygraph.to_variable(quant_out)
quant_acc = fluid.layers.accuracy(quant_out, label).numpy()
quant_acc = paddle.static.accuracy(quant_out, label).numpy()
paddle.enable_static()
delta_value = fp32_acc - quant_acc
self.assertLessEqual(delta_value, self.diff_threshold)
......
......@@ -118,7 +118,7 @@ class TestImperativeQatAmp(unittest.TestCase):
if use_amp:
with paddle.amp.auto_cast():
out = model(img)
acc = fluid.layers.accuracy(out, label)
acc = paddle.static.accuracy(out, label)
loss = fluid.layers.cross_entropy(out, label)
avg_loss = paddle.mean(loss)
scaled_loss = scaler.scale(avg_loss)
......@@ -128,7 +128,7 @@ class TestImperativeQatAmp(unittest.TestCase):
adam.clear_gradients()
else:
out = model(img)
acc = fluid.layers.accuracy(out, label)
acc = paddle.static.accuracy(out, label)
loss = fluid.layers.cross_entropy(out, label)
avg_loss = paddle.mean(loss)
avg_loss.backward()
......@@ -167,8 +167,8 @@ class TestImperativeQatAmp(unittest.TestCase):
with paddle.amp.auto_cast(use_amp):
out = model(img)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=out, label=label, k=5)
acc_top1_list.append(float(acc_top1.numpy()))
if batch_id % 100 == 0:
......
......@@ -170,7 +170,7 @@ class TestImperativeQatLSQ(unittest.TestCase):
img = fluid.dygraph.to_variable(x_data)
label = fluid.dygraph.to_variable(y_data)
out = lenet(img)
acc = fluid.layers.accuracy(out, label)
acc = paddle.static.accuracy(out, label)
loss = fluid.layers.cross_entropy(out, label)
avg_loss = paddle.mean(loss)
......@@ -202,10 +202,10 @@ class TestImperativeQatLSQ(unittest.TestCase):
label = fluid.dygraph.to_variable(y_data)
out = lenet(img)
acc_top1 = fluid.layers.accuracy(
acc_top1 = paddle.static.accuracy(
input=out, label=label, k=1
)
acc_top5 = fluid.layers.accuracy(
acc_top5 = paddle.static.accuracy(
input=out, label=label, k=5
)
......
......@@ -131,7 +131,7 @@ def train(net_type, use_cuda, save_dirname, is_local):
logits, label, return_softmax=True
)
avg_cost = paddle.mean(cost)
acc = fluid.layers.accuracy(input=predict, label=label)
acc = paddle.static.accuracy(input=predict, label=label)
# Test program
test_program = train_program.clone(for_test=True)
......
......@@ -146,8 +146,8 @@ def model():
merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)
predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax')
acc = fluid.layers.accuracy(input=predict, label=label)
auc_var, batch_auc_var, auc_states = fluid.layers.auc(
acc = paddle.static.accuracy(input=predict, label=label)
auc_var, batch_auc_var, auc_states = paddle.static.auc(
input=predict, label=label
)
cost = fluid.layers.cross_entropy(input=predict, label=label)
......
......@@ -192,7 +192,7 @@ class FleetUtil:
fluid.layers.ceil(similarity_norm), similarity_norm),\
similarity_norm], axis=1)
auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \
stat_neg] = fluid.layers.auc(input=binary_predict,\
stat_neg] = paddle.static.auc(input=binary_predict,\
label=label, curve='ROC',\
num_thresholds=4096)
......@@ -1381,7 +1381,7 @@ class FleetUtil:
fluid.layers.ceil(similarity_norm), similarity_norm),\
similarity_norm], axis=1)
auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \
stat_neg] = fluid.layers.auc(input=binary_predict,\
stat_neg] = paddle.static.auc(input=binary_predict,\
label=label, curve='ROC',\
num_thresholds=4096)
local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins,\
......@@ -1581,7 +1581,7 @@ class FleetUtil:
fluid.layers.ceil(similarity_norm), similarity_norm),\
similarity_norm], axis=1)
auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \
stat_neg] = fluid.layers.auc(input=binary_predict,\
stat_neg] = paddle.static.auc(input=binary_predict,\
label=label, curve='ROC',\
num_thresholds=4096)
local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins, \
......
......@@ -20,19 +20,14 @@ from . import tensor
from .tensor import *
from . import control_flow
from .control_flow import *
from . import device
from .device import *
from . import math_op_patch
from .math_op_patch import *
from . import loss
from .loss import *
from . import detection
from .detection import *
from . import metric_op
from .metric_op import *
from .learning_rate_scheduler import *
from .collective import *
from .distributions import *
from .sequence_lod import *
from . import rnn
......@@ -41,11 +36,8 @@ __all__ += nn.__all__
__all__ += io.__all__
__all__ += tensor.__all__
__all__ += control_flow.__all__
__all__ += device.__all__
__all__ += detection.__all__
__all__ += metric_op.__all__
__all__ += learning_rate_scheduler.__all__
__all__ += distributions.__all__
__all__ += sequence_lod.__all__
__all__ += loss.__all__
__all__ += rnn.__all__
......
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
All util layers.
"""
from .layer_function_generator import autodoc
from ..framework import unique_name
from ..layer_helper import LayerHelper
from paddle.utils import deprecated
__all__ = []
@deprecated(since='0.15.0', update_to="paddle.fluid.ParallelExecutor")
@autodoc()
def get_places(device_count=None, device_type=None):
helper = LayerHelper('get_places', **locals())
out_places = helper.create_variable(
name=unique_name.generate_with_ignorable_key(helper.name + ".out")
)
attrs = dict()
if device_count is not None:
attrs['device_count'] = int(device_count)
if device_type is not None:
attrs['device_type'] = str(device_type)
helper.append_op(
type='get_places', outputs={"Out": [out_places]}, attrs=attrs
)
return out_places
此差异已折叠。
......@@ -714,7 +714,7 @@ class Auc(MetricBase):
The auc metric is for binary classification.
Refer to https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve.
Please notice that the auc metric is implemented with python, which may be a little bit slow.
If you concern the speed, please use the fluid.layers.auc instead.
If you concern the speed, please use the paddle.static.auc instead.
The `auc` function creates four local variables, `true_positives`,
`true_negatives`, `false_positives` and `false_negatives` that are used to
......
......@@ -49,7 +49,7 @@ def convolution_net(
)
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(cost)
accuracy = fluid.layers.accuracy(input=prediction, label=label)
accuracy = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, accuracy, prediction
......@@ -84,7 +84,7 @@ def stacked_lstm_net(
)
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(cost)
accuracy = fluid.layers.accuracy(input=prediction, label=label)
accuracy = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, accuracy, prediction
......
......@@ -121,7 +121,7 @@ def train(net_type, use_cuda, save_dirname, is_local):
predict = fluid.layers.fc(input=net, size=classdim, act='softmax')
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = paddle.mean(cost)
acc = fluid.layers.accuracy(input=predict, label=label)
acc = paddle.static.accuracy(input=predict, label=label)
# Test program
test_program = fluid.default_main_program().clone(for_test=True)
......
......@@ -32,7 +32,7 @@ def loss_net(hidden, label):
prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label)
avg_loss = paddle.mean(loss)
acc = fluid.layers.accuracy(input=prediction, label=label)
acc = paddle.static.accuracy(input=prediction, label=label)
return prediction, avg_loss, acc
......
......@@ -66,7 +66,7 @@ def net():
cost, y_predict = fluid.layers.softmax_with_cross_entropy(
hidden, y, return_softmax=True
)
acc_top1 = fluid.layers.accuracy(input=y_predict, label=y, k=1)
acc_top1 = paddle.static.accuracy(input=y_predict, label=y, k=1)
avg_cost = paddle.mean(cost)
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.05)
......
......@@ -39,7 +39,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -106,7 +106,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
with fluid.device_guard("gpu:1"):
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -106,7 +106,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
with fluid.device_guard("gpu:1"):
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -98,7 +98,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
with fluid.device_guard("gpu:0"):
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -82,7 +82,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -99,8 +99,8 @@ class TestDistCTR2x2(TestDistRunnerBase):
merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)
predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax')
acc = fluid.layers.accuracy(input=predict, label=label)
auc_var, batch_auc_var, auc_states = fluid.layers.auc(
acc = paddle.static.accuracy(input=predict, label=label)
auc_var, batch_auc_var, auc_states = paddle.static.auc(
input=predict, label=label
)
cost = fluid.layers.cross_entropy(input=predict, label=label)
......
......@@ -148,9 +148,9 @@ class TestDistCTR2x2(FleetDistRunnerBase):
merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)
predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax')
acc = fluid.layers.accuracy(input=predict, label=label)
acc = paddle.static.accuracy(input=predict, label=label)
auc_var, batch_auc_var, auc_states = fluid.layers.auc(
auc_var, batch_auc_var, auc_states = paddle.static.auc(
input=predict, label=label
)
......
......@@ -84,7 +84,7 @@ class TestFleetMetaOptimizerPrecision(TestDistRunnerBase):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -84,7 +84,7 @@ class TestFleetMetaOptimizerFuseAllReducePrecision(TestDistRunnerBase):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -138,8 +138,8 @@ class TestDistCTR2x2(FleetDistRunnerBase):
merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)
predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax')
acc = fluid.layers.accuracy(input=predict, label=label)
auc_var, _, _ = fluid.layers.auc(input=predict, label=label)
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)
avg_cost = paddle.mean(x=cost)
......
......@@ -83,7 +83,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -48,7 +48,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -42,7 +42,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -39,7 +39,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
......@@ -224,8 +224,8 @@ class DistSeResneXt2x2(TestDistRunnerBase):
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=out, label=label, k=5)
# Evaluator
test_program = fluid.default_main_program().clone(for_test=True)
......
......@@ -134,7 +134,7 @@ class TestDistTextClassification2x2(TestDistRunnerBase):
predict = conv_net(data, dict_dim)
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = paddle.mean(x=cost)
acc = fluid.layers.accuracy(input=predict, label=label)
acc = paddle.static.accuracy(input=predict, label=label)
inference_program = fluid.default_main_program().clone()
# Optimization
......
......@@ -12,18 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import config
import numpy as np
import parameterize
import paddle
from paddle.distribution import Categorical, Normal, Uniform
from paddle.fluid import layers
paddle.enable_static()
class DistributionNumpy:
def sample(self):
......@@ -40,153 +28,3 @@ class DistributionNumpy:
def probs(self, value):
raise NotImplementedError
class DistributionTestName(unittest.TestCase):
def get_prefix(self, string):
return string.split('.')[0]
def test_normal_name(self):
name = 'test_normal'
normal1 = Normal(0.0, 1.0, name=name)
self.assertEqual(normal1.name, name)
normal2 = Normal(0.0, 1.0)
self.assertEqual(normal2.name, 'Normal')
paddle.enable_static()
sample = normal1.sample([2])
self.assertEqual(self.get_prefix(sample.name), name + '_sample')
entropy = normal1.entropy()
self.assertEqual(self.get_prefix(entropy.name), name + '_entropy')
value_npdata = np.array([0.8], dtype="float32")
value_tensor = layers.create_tensor(dtype="float32")
layers.assign(value_npdata, value_tensor)
lp = normal1.log_prob(value_tensor)
self.assertEqual(self.get_prefix(lp.name), name + '_log_prob')
p = normal1.probs(value_tensor)
self.assertEqual(self.get_prefix(p.name), name + '_probs')
kl = normal1.kl_divergence(normal2)
self.assertEqual(self.get_prefix(kl.name), name + '_kl_divergence')
def test_uniform_name(self):
name = 'test_uniform'
uniform1 = Uniform(0.0, 1.0, name=name)
self.assertEqual(uniform1.name, name)
uniform2 = Uniform(0.0, 1.0)
self.assertEqual(uniform2.name, 'Uniform')
paddle.enable_static()
sample = uniform1.sample([2])
self.assertEqual(self.get_prefix(sample.name), name + '_sample')
entropy = uniform1.entropy()
self.assertEqual(self.get_prefix(entropy.name), name + '_entropy')
value_npdata = np.array([0.8], dtype="float32")
value_tensor = layers.create_tensor(dtype="float32")
layers.assign(value_npdata, value_tensor)
lp = uniform1.log_prob(value_tensor)
self.assertEqual(self.get_prefix(lp.name), name + '_log_prob')
p = uniform1.probs(value_tensor)
self.assertEqual(self.get_prefix(p.name), name + '_probs')
def test_categorical_name(self):
name = 'test_categorical'
categorical1 = Categorical([0.4, 0.6], name=name)
self.assertEqual(categorical1.name, name)
categorical2 = Categorical([0.5, 0.5])
self.assertEqual(categorical2.name, 'Categorical')
paddle.enable_static()
sample = categorical1.sample([2])
self.assertEqual(self.get_prefix(sample.name), name + '_sample')
entropy = categorical1.entropy()
self.assertEqual(self.get_prefix(entropy.name), name + '_entropy')
kl = categorical1.kl_divergence(categorical2)
self.assertEqual(self.get_prefix(kl.name), name + '_kl_divergence')
value_npdata = np.array([0], dtype="int64")
value_tensor = layers.create_tensor(dtype="int64")
layers.assign(value_npdata, value_tensor)
p = categorical1.probs(value_tensor)
self.assertEqual(self.get_prefix(p.name), name + '_probs')
lp = categorical1.log_prob(value_tensor)
self.assertEqual(self.get_prefix(lp.name), name + '_log_prob')
@parameterize.place(config.DEVICES)
@parameterize.parameterize_cls(
(parameterize.TEST_CASE_NAME, 'batch_shape', 'event_shape'),
[
('test-tuple', (10, 20), (10, 20)),
('test-list', [100, 100], [100, 200, 300]),
('test-null-eventshape', (100, 100), ()),
],
)
class TestDistributionShape(unittest.TestCase):
def setUp(self):
paddle.disable_static()
self.dist = paddle.distribution.Distribution(
batch_shape=self.batch_shape, event_shape=self.event_shape
)
def tearDown(self):
paddle.enable_static()
def test_batch_shape(self):
self.assertTrue(isinstance(self.dist.batch_shape, tuple))
self.assertTrue(self.dist.batch_shape == tuple(self.batch_shape))
def test_event_shape(self):
self.assertTrue(isinstance(self.dist.event_shape, tuple))
self.assertTrue(self.dist.event_shape == tuple(self.event_shape))
def test_prob(self):
with self.assertRaises(NotImplementedError):
self.dist.prob(paddle.to_tensor(parameterize.xrand()))
def test_extend_shape(self):
shapes = [(34, 20), (56,), ()]
for shape in shapes:
self.assertTrue(
self.dist._extend_shape(shape),
shape + self.dist.batch_shape + self.dist.event_shape,
)
class TestDistributionException(unittest.TestCase):
def setUp(self):
self._d = paddle.distribution.Distribution()
def test_mean(self):
with self.assertRaises(NotImplementedError):
self._d.mean
def test_variance(self):
with self.assertRaises(NotImplementedError):
self._d.variance
def test_rsample(self):
with self.assertRaises(NotImplementedError):
self._d.rsample(())
if __name__ == '__main__':
unittest.main()
......@@ -424,7 +424,7 @@ class PretrainModelLayer(Layer):
logits=next_sent_fc_out, label=labels, return_softmax=True
)
next_sent_acc = fluid.layers.accuracy(
next_sent_acc = paddle.static.accuracy(
input=next_sent_softmax, label=labels
)
......
......@@ -110,7 +110,7 @@ class MNIST(fluid.dygraph.Layer):
def forward(self, inputs, label=None):
x = self.inference(inputs)
if label is not None:
acc = fluid.layers.accuracy(input=x, label=label)
acc = paddle.static.accuracy(input=x, label=label)
loss = fluid.layers.cross_entropy(x, label)
avg_loss = paddle.mean(loss)
......
......@@ -540,8 +540,8 @@ def train_mobilenet(args, to_static):
input=softmax_out, label=label
)
avg_loss = paddle.mean(x=loss)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=out, label=label, k=5)
t_start_back = time.time()
loss_data.append(avg_loss.numpy())
......
......@@ -276,10 +276,10 @@ class ResNetHelper:
pred = resnet(img)
loss = fluid.layers.cross_entropy(input=pred, label=label)
avg_loss = paddle.mean(x=loss)
acc_top1 = fluid.layers.accuracy(
acc_top1 = paddle.static.accuracy(
input=pred, label=label, k=1
)
acc_top5 = fluid.layers.accuracy(
acc_top5 = paddle.static.accuracy(
input=pred, label=label, k=5
)
......
......@@ -76,8 +76,8 @@ def train(to_static, build_strategy=None):
# If we remove it, the loss between dygraph and dy2stat is exactly same.
loss = fluid.layers.cross_entropy(input=pred, label=label)
avg_loss = paddle.mean(x=pred)
acc_top1 = fluid.layers.accuracy(input=pred, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=pred, label=label, k=5)
acc_top1 = paddle.static.accuracy(input=pred, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=pred, label=label, k=5)
scaled = scaler.scale(avg_loss)
scaled.backward()
......
......@@ -77,8 +77,8 @@ def train(to_static, build_strategy=None):
pred = resnet(img)
loss = fluid.layers.cross_entropy(input=pred, label=label)
avg_loss = paddle.mean(x=pred)
acc_top1 = fluid.layers.accuracy(input=pred, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=pred, label=label, k=5)
acc_top1 = paddle.static.accuracy(input=pred, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=pred, label=label, k=5)
scaled = scaler.scale(avg_loss)
scaled.backward()
......
......@@ -347,8 +347,8 @@ class SeResNeXt(fluid.dygraph.Layer):
loss = fluid.layers.cross_entropy(input=softmax_out, label=label)
avg_loss = paddle.mean(x=loss)
acc_top1 = fluid.layers.accuracy(input=softmax_out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=softmax_out, label=label, k=5)
acc_top1 = paddle.static.accuracy(input=softmax_out, label=label, k=1)
acc_top5 = paddle.static.accuracy(input=softmax_out, label=label, k=5)
return out, avg_loss, acc_top1, acc_top5
......
......@@ -109,7 +109,7 @@ class CNN(fluid.dygraph.Layer):
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost)
acc = fluid.layers.accuracy(input=prediction, label=label)
acc = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc
......@@ -152,7 +152,7 @@ class BOW(fluid.dygraph.Layer):
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost)
acc = fluid.layers.accuracy(input=prediction, label=label)
acc = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc
......@@ -198,7 +198,7 @@ class GRU(fluid.dygraph.Layer):
prediction = paddle.nn.functional.softmax(prediction)
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost)
acc = fluid.layers.accuracy(input=prediction, label=label)
acc = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc
......@@ -257,7 +257,7 @@ class BiGRU(fluid.dygraph.Layer):
# if label is not None:
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost)
acc = fluid.layers.accuracy(input=prediction, label=label)
acc = paddle.static.accuracy(input=prediction, label=label)
return avg_cost, prediction, acc
# else:
# return prediction
......
......@@ -334,10 +334,10 @@ def train(args, fake_data_reader, to_static):
input=outputs, label=labels, ignore_index=-1
)
avg_loss = paddle.mean(loss)
acc_top1 = fluid.layers.accuracy(
acc_top1 = paddle.static.accuracy(
input=outputs, label=labels, k=1
)
acc_top5 = fluid.layers.accuracy(
acc_top5 = paddle.static.accuracy(
input=outputs, label=labels, k=5
)
......
......@@ -74,14 +74,14 @@ class TestAccuracyOpError(unittest.TestCase):
label = fluid.layers.data(
name='label', shape=[-1, 1], dtype="int32"
)
self.assertRaises(TypeError, fluid.layers.accuracy, x1, label)
self.assertRaises(TypeError, paddle.static.accuracy, x1, label)
self.assertRaises(TypeError, paddle.metric.accuracy, x1, label)
# The input dtype of accuracy_op must be float32 or float64.
x2 = fluid.layers.data(name='x2', shape=[4], dtype="int32")
self.assertRaises(TypeError, fluid.layers.accuracy, x2, label)
self.assertRaises(TypeError, paddle.static.accuracy, x2, label)
self.assertRaises(TypeError, paddle.metric.accuracy, x2, label)
x3 = fluid.layers.data(name='input', shape=[-1, 2], dtype="float16")
fluid.layers.accuracy(input=x3, label=label)
paddle.static.accuracy(input=x3, label=label)
paddle.metric.accuracy(input=x3, label=label)
......
......@@ -69,14 +69,14 @@ class TestAccuracyOpError(unittest.TestCase):
label = fluid.layers.data(
name='label', shape=[-1, 1], dtype="int32"
)
self.assertRaises(TypeError, fluid.layers.accuracy, x1, label)
self.assertRaises(TypeError, paddle.static.accuracy, x1, label)
self.assertRaises(TypeError, paddle.metric.accuracy, x1, label)
# The input dtype of accuracy_op must be float32 or float64.
x2 = fluid.layers.data(name='x2', shape=[4], dtype="int32")
self.assertRaises(TypeError, fluid.layers.accuracy, x2, label)
self.assertRaises(TypeError, paddle.static.accuracy, x2, label)
self.assertRaises(TypeError, paddle.metric.accuracy, x2, label)
x3 = fluid.layers.data(name='input', shape=[-1, 2], dtype="float16")
fluid.layers.accuracy(input=x3, label=label)
paddle.static.accuracy(input=x3, label=label)
paddle.metric.accuracy(input=x3, label=label)
......
......@@ -59,7 +59,7 @@ def convolutional_neural_network(use_py_reader):
prediction = fluid.layers.fc(input=conv_pool_2, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label)
avg_loss = paddle.mean(loss)
acc = fluid.layers.accuracy(input=prediction, label=label)
acc = paddle.static.accuracy(input=prediction, label=label)
i = fluid.layers.zeros(shape=[1], dtype='int64')
array = fluid.layers.array_write(x=prediction, i=i)
fluid.layers.increment(i)
......
......@@ -158,7 +158,7 @@ class TestAucOpError(unittest.TestCase):
name="input2", shape=[-1, 2], dtype="float32"
)
label2 = fluid.data(name="label2", shape=[-1], dtype="float32")
result2 = fluid.layers.auc(input=data2, label=label2)
result2 = paddle.static.auc(input=data2, label=label2)
self.assertRaises(TypeError, test_type2)
......
......@@ -78,7 +78,7 @@ def get_model(batch_size):
# Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size_tensor
)
......
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from decorator_helper import prog_scope
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.layers.device import get_places
class TestGetPlaces(unittest.TestCase):
@prog_scope()
def check_get_cpu_places(self):
places = get_places()
cpu = fluid.CPUPlace()
exe = fluid.Executor(cpu)
exe.run(fluid.default_main_program())
self.assertEqual(places.type, fluid.core.VarDesc.VarType.PLACE_LIST)
@prog_scope()
def check_get_gpu_places(self):
places = get_places(device_type='CUDA')
gpu = fluid.CUDAPlace(0)
exe = fluid.Executor(gpu)
exe.run(fluid.default_main_program())
self.assertEqual(places.type, fluid.core.VarDesc.VarType.PLACE_LIST)
def test_main(self):
if core.is_compiled_with_cuda():
self.check_get_gpu_places()
self.check_get_cpu_places()
if __name__ == '__main__':
unittest.main()
......@@ -174,8 +174,8 @@ class TestSaveInferenceModel(unittest.TestCase):
x = layers.data(name='x', shape=[2], dtype='float32')
y = layers.data(name='y', shape=[1], dtype='int32')
predict = fluid.layers.fc(input=x, size=2, act='softmax')
acc = fluid.layers.accuracy(input=predict, label=y)
auc_var, batch_auc_var, auc_states = fluid.layers.auc(
acc = paddle.static.accuracy(input=predict, label=y)
auc_var, batch_auc_var, auc_states = paddle.static.auc(
input=predict, label=y
)
cost = fluid.layers.cross_entropy(input=predict, label=y)
......
......@@ -34,7 +34,6 @@ from paddle.fluid.framework import (
program_guard,
)
from paddle.fluid.initializer import Constant
from paddle.fluid.layers.device import get_places
from paddle.fluid.param_attr import ParamAttr
from paddle.tensor import random
......@@ -2895,7 +2894,7 @@ class TestLayer(LayerTest):
label = fluid.data(name="label", shape=[-1, 1], dtype="int")
fc_out = fluid.layers.fc(input=data, size=10)
predict = fluid.layers.softmax(input=fc_out)
result = fluid.layers.accuracy(input=predict, label=label, k=5)
result = paddle.static.accuracy(input=predict, label=label, k=5)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
......@@ -2911,7 +2910,9 @@ class TestLayer(LayerTest):
label = base.to_variable(y)
fc_out = fluid.layers.fc(data, size=10)
predict = fluid.layers.softmax(fc_out)
dynamic_out = fluid.layers.accuracy(input=predict, label=label, k=5)
dynamic_out = paddle.static.accuracy(
input=predict, label=label, k=5
)
np.testing.assert_array_equal(static_out[0], dynamic_out.numpy())
......@@ -2954,7 +2955,6 @@ class TestBook(LayerTest):
)
else:
assert method.__name__ in ('make_get_places')
continue
if method.__name__ in self.only_static_set:
continue
......@@ -3201,12 +3201,6 @@ class TestBook(LayerTest):
hid = layers.fc(input=data, size=20)
return layers.softmax(hid, axis=1)
def make_get_places(self):
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
get_places(device_count=1)
@prog_scope()
def make_nce(self):
window_size = 5
......
......@@ -60,7 +60,7 @@ class TestProfiler(unittest.TestCase):
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = paddle.mean(cost)
batch_size = fluid.layers.create_tensor(dtype='int64')
batch_acc = fluid.layers.accuracy(
batch_acc = paddle.static.accuracy(
input=predict, label=label, total=batch_size
)
......
......@@ -76,7 +76,7 @@ def simple_fc_net_with_accuracy(use_feed):
prediction = fluid.layers.fc(hidden, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label)
loss = paddle.mean(loss)
accuracy_out = fluid.layers.accuracy(input=prediction, label=label, k=5)
accuracy_out = paddle.static.accuracy(input=prediction, label=label, k=5)
return loss
......
......@@ -68,11 +68,12 @@ from ..fluid.io import batch # noqa: F401
from ..fluid.layers import create_parameter # noqa: F401
from ..fluid.layers import create_global_var # noqa: F401
from ..fluid.layers.metric_op import auc # noqa: F401
from ..fluid.layers.metric_op import accuracy # noqa: F401
from ..fluid.contrib.layers import ctr_metric_bundle # noqa: F401
from ..fluid.layers import exponential_decay # noqa: F401
from paddle.static.nn.metric import auc # noqa: F401
from paddle.static.nn.metric import accuracy # noqa: F401
__all__ = [ # noqa
'append_backward',
'gradients',
......
......@@ -15,22 +15,16 @@
All layers just related to metric.
"""
import warnings
from ..layer_helper import LayerHelper
from ..initializer import Normal, Constant
from ..framework import (
from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.initializer import Constant
from paddle.fluid.framework import (
Variable,
_non_static_mode,
_varbase_creator,
_in_legacy_dygraph,
in_dygraph_mode,
)
from .. import core
from ..param_attr import ParamAttr
from . import nn
from . import tensor
from ..data_feeder import check_variable_and_dtype
from paddle import _C_ops, _legacy_C_ops
from paddle.fluid.layers import tensor
from paddle.fluid.data_feeder import check_variable_and_dtype
from paddle import _legacy_C_ops
__all__ = ['accuracy', 'auc']
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册