You need to sign in or sign up before continuing.
未验证 提交 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): ...@@ -148,8 +148,8 @@ def auc(stat_pos, stat_neg, scope=None, util=None):
distributed auc in fleet distributed auc in fleet
Args: Args:
stat_pos(numpy.array|Variable|string): stat_pos 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 fluid.layers.auc stat_neg(numpy.array|Variable|string): stat_neg in output of paddle.static.auc
scope(Scope): specific scope scope(Scope): specific scope
Returns: Returns:
...@@ -163,7 +163,7 @@ def auc(stat_pos, stat_neg, scope=None, util=None): ...@@ -163,7 +163,7 @@ def auc(stat_pos, stat_neg, scope=None, util=None):
binary_predict = fluid.layers.concat( binary_predict = fluid.layers.concat(
input=[fluid.layers.elementwise_sub(fluid.layers.ceil(similarity_norm), similarity_norm), similarity_norm], axis=1) 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] = 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 # in train.py, after train or infer
pos = np.array(scope.find_var(stat_pos.name).get_tensor()) pos = np.array(scope.find_var(stat_pos.name).get_tensor())
......
...@@ -150,8 +150,8 @@ class TestImperativePTQ(unittest.TestCase): ...@@ -150,8 +150,8 @@ class TestImperativePTQ(unittest.TestCase):
label = paddle.to_tensor(y_data) label = paddle.to_tensor(y_data)
out = model(img) out = model(img)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1) acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5) acc_top5 = paddle.static.accuracy(input=out, label=label, k=5)
eval_acc_top1_list.append(float(acc_top1.numpy())) eval_acc_top1_list.append(float(acc_top1.numpy()))
if batch_id % 50 == 0: if batch_id % 50 == 0:
......
...@@ -129,7 +129,7 @@ class TestImperativeQat(unittest.TestCase): ...@@ -129,7 +129,7 @@ class TestImperativeQat(unittest.TestCase):
img = fluid.dygraph.to_variable(x_data) img = fluid.dygraph.to_variable(x_data)
label = fluid.dygraph.to_variable(y_data) label = fluid.dygraph.to_variable(y_data)
out = lenet(img) out = lenet(img)
acc = fluid.layers.accuracy(out, label) acc = paddle.static.accuracy(out, label)
loss = fluid.layers.cross_entropy(out, label) loss = fluid.layers.cross_entropy(out, label)
avg_loss = paddle.mean(loss) avg_loss = paddle.mean(loss)
avg_loss.backward() avg_loss.backward()
...@@ -160,10 +160,10 @@ class TestImperativeQat(unittest.TestCase): ...@@ -160,10 +160,10 @@ class TestImperativeQat(unittest.TestCase):
label = fluid.dygraph.to_variable(y_data) label = fluid.dygraph.to_variable(y_data)
out = lenet(img) out = lenet(img)
acc_top1 = fluid.layers.accuracy( acc_top1 = paddle.static.accuracy(
input=out, label=label, k=1 input=out, label=label, k=1
) )
acc_top5 = fluid.layers.accuracy( acc_top5 = paddle.static.accuracy(
input=out, label=label, k=5 input=out, label=label, k=5
) )
...@@ -200,7 +200,7 @@ class TestImperativeQat(unittest.TestCase): ...@@ -200,7 +200,7 @@ class TestImperativeQat(unittest.TestCase):
label = fluid.dygraph.to_variable(y_data) label = fluid.dygraph.to_variable(y_data)
lenet.eval() lenet.eval()
fp32_out = lenet(test_img) 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: with tempfile.TemporaryDirectory(prefix="qat_save_path_") as tmpdir:
# save inference quantized model # save inference quantized model
...@@ -237,7 +237,7 @@ class TestImperativeQat(unittest.TestCase): ...@@ -237,7 +237,7 @@ class TestImperativeQat(unittest.TestCase):
) )
paddle.disable_static() paddle.disable_static()
quant_out = fluid.dygraph.to_variable(quant_out) 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() paddle.enable_static()
delta_value = fp32_acc - quant_acc delta_value = fp32_acc - quant_acc
self.assertLessEqual(delta_value, self.diff_threshold) self.assertLessEqual(delta_value, self.diff_threshold)
......
...@@ -118,7 +118,7 @@ class TestImperativeQatAmp(unittest.TestCase): ...@@ -118,7 +118,7 @@ class TestImperativeQatAmp(unittest.TestCase):
if use_amp: if use_amp:
with paddle.amp.auto_cast(): with paddle.amp.auto_cast():
out = model(img) out = model(img)
acc = fluid.layers.accuracy(out, label) acc = paddle.static.accuracy(out, label)
loss = fluid.layers.cross_entropy(out, label) loss = fluid.layers.cross_entropy(out, label)
avg_loss = paddle.mean(loss) avg_loss = paddle.mean(loss)
scaled_loss = scaler.scale(avg_loss) scaled_loss = scaler.scale(avg_loss)
...@@ -128,7 +128,7 @@ class TestImperativeQatAmp(unittest.TestCase): ...@@ -128,7 +128,7 @@ class TestImperativeQatAmp(unittest.TestCase):
adam.clear_gradients() adam.clear_gradients()
else: else:
out = model(img) out = model(img)
acc = fluid.layers.accuracy(out, label) acc = paddle.static.accuracy(out, label)
loss = fluid.layers.cross_entropy(out, label) loss = fluid.layers.cross_entropy(out, label)
avg_loss = paddle.mean(loss) avg_loss = paddle.mean(loss)
avg_loss.backward() avg_loss.backward()
...@@ -167,8 +167,8 @@ class TestImperativeQatAmp(unittest.TestCase): ...@@ -167,8 +167,8 @@ class TestImperativeQatAmp(unittest.TestCase):
with paddle.amp.auto_cast(use_amp): with paddle.amp.auto_cast(use_amp):
out = model(img) out = model(img)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1) acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5) acc_top5 = paddle.static.accuracy(input=out, label=label, k=5)
acc_top1_list.append(float(acc_top1.numpy())) acc_top1_list.append(float(acc_top1.numpy()))
if batch_id % 100 == 0: if batch_id % 100 == 0:
......
...@@ -170,7 +170,7 @@ class TestImperativeQatLSQ(unittest.TestCase): ...@@ -170,7 +170,7 @@ class TestImperativeQatLSQ(unittest.TestCase):
img = fluid.dygraph.to_variable(x_data) img = fluid.dygraph.to_variable(x_data)
label = fluid.dygraph.to_variable(y_data) label = fluid.dygraph.to_variable(y_data)
out = lenet(img) out = lenet(img)
acc = fluid.layers.accuracy(out, label) acc = paddle.static.accuracy(out, label)
loss = fluid.layers.cross_entropy(out, label) loss = fluid.layers.cross_entropy(out, label)
avg_loss = paddle.mean(loss) avg_loss = paddle.mean(loss)
...@@ -202,10 +202,10 @@ class TestImperativeQatLSQ(unittest.TestCase): ...@@ -202,10 +202,10 @@ class TestImperativeQatLSQ(unittest.TestCase):
label = fluid.dygraph.to_variable(y_data) label = fluid.dygraph.to_variable(y_data)
out = lenet(img) out = lenet(img)
acc_top1 = fluid.layers.accuracy( acc_top1 = paddle.static.accuracy(
input=out, label=label, k=1 input=out, label=label, k=1
) )
acc_top5 = fluid.layers.accuracy( acc_top5 = paddle.static.accuracy(
input=out, label=label, k=5 input=out, label=label, k=5
) )
......
...@@ -131,7 +131,7 @@ def train(net_type, use_cuda, save_dirname, is_local): ...@@ -131,7 +131,7 @@ def train(net_type, use_cuda, save_dirname, is_local):
logits, label, return_softmax=True logits, label, return_softmax=True
) )
avg_cost = paddle.mean(cost) 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
test_program = train_program.clone(for_test=True) test_program = train_program.clone(for_test=True)
......
...@@ -146,8 +146,8 @@ def model(): ...@@ -146,8 +146,8 @@ def model():
merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1) merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)
predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax') 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 input=predict, label=label
) )
cost = fluid.layers.cross_entropy(input=predict, label=label) cost = fluid.layers.cross_entropy(input=predict, label=label)
......
...@@ -192,7 +192,7 @@ class FleetUtil: ...@@ -192,7 +192,7 @@ class FleetUtil:
fluid.layers.ceil(similarity_norm), similarity_norm),\ fluid.layers.ceil(similarity_norm), similarity_norm),\
similarity_norm], axis=1) similarity_norm], axis=1)
auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ 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',\ label=label, curve='ROC',\
num_thresholds=4096) num_thresholds=4096)
...@@ -1381,7 +1381,7 @@ class FleetUtil: ...@@ -1381,7 +1381,7 @@ class FleetUtil:
fluid.layers.ceil(similarity_norm), similarity_norm),\ fluid.layers.ceil(similarity_norm), similarity_norm),\
similarity_norm], axis=1) similarity_norm], axis=1)
auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ 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',\ label=label, curve='ROC',\
num_thresholds=4096) num_thresholds=4096)
local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins,\ local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins,\
...@@ -1581,7 +1581,7 @@ class FleetUtil: ...@@ -1581,7 +1581,7 @@ class FleetUtil:
fluid.layers.ceil(similarity_norm), similarity_norm),\ fluid.layers.ceil(similarity_norm), similarity_norm),\
similarity_norm], axis=1) similarity_norm], axis=1)
auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ 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',\ label=label, curve='ROC',\
num_thresholds=4096) num_thresholds=4096)
local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins, \ local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins, \
......
...@@ -20,19 +20,14 @@ from . import tensor ...@@ -20,19 +20,14 @@ from . import tensor
from .tensor import * from .tensor import *
from . import control_flow from . import control_flow
from .control_flow import * from .control_flow import *
from . import device
from .device import *
from . import math_op_patch from . import math_op_patch
from .math_op_patch import * from .math_op_patch import *
from . import loss from . import loss
from .loss import * from .loss import *
from . import detection from . import detection
from .detection import * from .detection import *
from . import metric_op
from .metric_op import *
from .learning_rate_scheduler import * from .learning_rate_scheduler import *
from .collective import * from .collective import *
from .distributions import *
from .sequence_lod import * from .sequence_lod import *
from . import rnn from . import rnn
...@@ -41,11 +36,8 @@ __all__ += nn.__all__ ...@@ -41,11 +36,8 @@ __all__ += nn.__all__
__all__ += io.__all__ __all__ += io.__all__
__all__ += tensor.__all__ __all__ += tensor.__all__
__all__ += control_flow.__all__ __all__ += control_flow.__all__
__all__ += device.__all__
__all__ += detection.__all__ __all__ += detection.__all__
__all__ += metric_op.__all__
__all__ += learning_rate_scheduler.__all__ __all__ += learning_rate_scheduler.__all__
__all__ += distributions.__all__
__all__ += sequence_lod.__all__ __all__ += sequence_lod.__all__
__all__ += loss.__all__ __all__ += loss.__all__
__all__ += rnn.__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
# Copyright (c) 2019 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.
from . import control_flow
from . import tensor
from . import nn
import math
import numpy as np
import warnings
import paddle
from ..data_feeder import (
convert_dtype,
check_variable_and_dtype,
check_type,
check_dtype,
)
__all__ = ['Uniform', 'Normal', 'Categorical', 'MultivariateNormalDiag']
class Distribution:
"""
Distribution is the abstract base class for probability distributions.
"""
def sample(self):
"""Sampling from the distribution."""
raise NotImplementedError
def entropy(self):
"""The entropy of the distribution."""
raise NotImplementedError
def kl_divergence(self, other):
"""The KL-divergence between self distributions and other."""
raise NotImplementedError
def log_prob(self, value):
"""Log probability density/mass function."""
raise NotImplementedError
def _validate_args(self, *args):
"""
Argument validation for distribution args
Args:
value (float, list, numpy.ndarray, Variable)
Raises
ValueError: if one argument is Variable, all arguments should be Variable
"""
is_variable = False
is_number = False
for arg in args:
if isinstance(arg, tensor.Variable):
is_variable = True
else:
is_number = True
if is_variable and is_number:
raise ValueError(
'if one argument is Variable, all arguments should be Variable'
)
return is_variable
def _to_variable(self, *args):
"""
Argument convert args to Variable
Args:
value (float, list, numpy.ndarray, Variable)
Returns:
Variable of args.
"""
numpy_args = []
variable_args = []
tmp = 0.0
for arg in args:
valid_arg = False
for cls in [float, list, np.ndarray, tensor.Variable]:
if isinstance(arg, cls):
valid_arg = True
break
assert (
valid_arg
), "type of input args must be float, list, numpy.ndarray or Variable."
if isinstance(arg, float):
arg = np.zeros(1) + arg
arg_np = np.array(arg)
arg_dtype = arg_np.dtype
if str(arg_dtype) not in ['float32']:
warnings.warn(
"data type of argument only support float32, your argument will be convert to float32."
)
arg_np = arg_np.astype('float32')
tmp = tmp + arg_np
numpy_args.append(arg_np)
dtype = tmp.dtype
for arg in numpy_args:
arg_broadcasted, _ = np.broadcast_arrays(arg, tmp)
arg_variable = tensor.create_tensor(dtype=dtype)
tensor.assign(arg_broadcasted, arg_variable)
variable_args.append(arg_variable)
return tuple(variable_args)
class Uniform(Distribution):
r"""Uniform distribution with `low` and `high` parameters.
Mathematical Details
The probability density function (pdf) is,
.. math::
pdf(x; a, b) = \\frac{1}{Z}, \ a <=x <b
.. math::
Z = b - a
In the above equation:
* :math:`low = a`,
* :math:`high = b`,
* :math:`Z`: is the normalizing constant.
The parameters `low` and `high` must be shaped in a way that supports
broadcasting (e.g., `high - low` is a valid operation).
Args:
low(float|list|numpy.ndarray|Variable): The lower boundary of uniform distribution.The data type is float32
high(float|list|numpy.ndarray|Variable): The higher boundary of uniform distribution.The data type is float32
Examples:
.. code-block:: python
import numpy as np
from paddle.fluid import layers
from paddle.fluid.layers import Uniform
# Without broadcasting, a single uniform distribution [3, 4]:
u1 = Uniform(low=3.0, high=4.0)
# 2 distributions [1, 3], [2, 4]
u2 = Uniform(low=[1.0, 2.0],
high=[3.0, 4.0])
# 4 distributions
u3 = Uniform(low=[[1.0, 2.0],
[3.0, 4.0]],
high=[[1.5, 2.5],
[3.5, 4.5]])
# With broadcasting:
u4 = Uniform(low=3.0, high=[5.0, 6.0, 7.0])
# Complete example
value_npdata = np.array([0.8], dtype="float32")
value_tensor = layers.create_tensor(dtype="float32")
layers.assign(value_npdata, value_tensor)
uniform = Uniform([0.], [2.])
sample = uniform.sample([2])
# a random tensor created by uniform distribution with shape: [2, 1]
entropy = uniform.entropy()
# [0.6931472] with shape: [1]
lp = uniform.log_prob(value_tensor)
# [-0.6931472] with shape: [1]
"""
def __init__(self, low, high):
check_type(
low, 'low', (float, np.ndarray, tensor.Variable, list), 'Uniform'
)
check_type(
high, 'high', (float, np.ndarray, tensor.Variable, list), 'Uniform'
)
self.all_arg_is_float = False
self.batch_size_unknown = False
if self._validate_args(low, high):
self.batch_size_unknown = True
self.low = low
self.high = high
else:
if isinstance(low, float) and isinstance(high, float):
self.all_arg_is_float = True
self.low, self.high = self._to_variable(low, high)
def sample(self, shape, seed=0):
"""Generate samples of the specified shape.
Args:
shape (list): 1D `int32`. Shape of the generated samples.
seed (int): Python integer number.
Returns:
Variable: A tensor with prepended dimensions shape.The data type is float32.
"""
check_type(shape, 'shape', (list), 'sample')
check_type(seed, 'seed', (int), 'sample')
batch_shape = list((self.low + self.high).shape)
if self.batch_size_unknown:
output_shape = shape + batch_shape
zero_tmp = tensor.fill_constant_batch_size_like(
self.low + self.high, batch_shape + shape, self.low.dtype, 0.0
)
uniform_random_tmp = (
paddle.tensor.random.uniform_random_batch_size_like(
zero_tmp, zero_tmp.shape, min=0.0, max=1.0, seed=seed
)
)
output = (
uniform_random_tmp * (zero_tmp + self.high - self.low)
+ self.low
)
return paddle.reshape(output, output_shape)
else:
output_shape = shape + batch_shape
output = (
nn.uniform_random(output_shape, seed=seed)
* (
tensor.zeros(output_shape, dtype=self.low.dtype)
+ (self.high - self.low)
)
+ self.low
)
if self.all_arg_is_float:
return paddle.reshape(output, shape)
else:
return output
def log_prob(self, value):
"""Log probability density/mass function.
Args:
value (Variable): The input tensor.
Returns:
Variable: log probability.The data type is same with value.
"""
check_variable_and_dtype(
value, 'value', ['float32', 'float64'], 'log_prob'
)
lb_bool = control_flow.less_than(self.low, value)
ub_bool = control_flow.less_than(value, self.high)
lb = tensor.cast(lb_bool, dtype=value.dtype)
ub = tensor.cast(ub_bool, dtype=value.dtype)
return paddle.log(lb * ub) - paddle.log(self.high - self.low)
def entropy(self):
"""Shannon entropy in nats.
Returns:
Variable: Shannon entropy of uniform distribution.The data type is float32.
"""
return paddle.log(self.high - self.low)
class Normal(Distribution):
r"""The Normal distribution with location `loc` and `scale` parameters.
Mathematical details
The probability density function (pdf) is,
.. math::
pdf(x; \mu, \sigma) = \\frac{1}{Z}e^{\\frac {-0.5 (x - \mu)^2} {\sigma^2} }
.. math::
Z = (2 \pi \sigma^2)^{0.5}
In the above equation:
* :math:`loc = \mu`: is the mean.
* :math:`scale = \sigma`: is the std.
* :math:`Z`: is the normalization constant.
Args:
loc(float|list|numpy.ndarray|Variable): The mean of normal distribution.The data type is float32.
scale(float|list|numpy.ndarray|Variable): The std of normal distribution.The data type is float32.
Examples:
.. code-block:: python
import numpy as np
from paddle.fluid import layers
from paddle.fluid.layers import Normal
# Define a single scalar Normal distribution.
dist = Normal(loc=0., scale=3.)
# Define a batch of two scalar valued Normals.
# The first has mean 1 and standard deviation 11, the second 2 and 22.
dist = Normal(loc=[1., 2.], scale=[11., 22.])
# Get 3 samples, returning a 3 x 2 tensor.
dist.sample([3])
# Define a batch of two scalar valued Normals.
# Both have mean 1, but different standard deviations.
dist = Normal(loc=1., scale=[11., 22.])
# Complete example
value_npdata = np.array([0.8], dtype="float32")
value_tensor = layers.create_tensor(dtype="float32")
layers.assign(value_npdata, value_tensor)
normal_a = Normal([0.], [1.])
normal_b = Normal([0.5], [2.])
sample = normal_a.sample([2])
# a random tensor created by normal distribution with shape: [2, 1]
entropy = normal_a.entropy()
# [1.4189385] with shape: [1]
lp = normal_a.log_prob(value_tensor)
# [-1.2389386] with shape: [1]
kl = normal_a.kl_divergence(normal_b)
# [0.34939718] with shape: [1]
"""
def __init__(self, loc, scale):
check_type(
loc, 'loc', (float, np.ndarray, tensor.Variable, list), 'Normal'
)
check_type(
scale, 'scale', (float, np.ndarray, tensor.Variable, list), 'Normal'
)
self.batch_size_unknown = False
self.all_arg_is_float = False
if self._validate_args(loc, scale):
self.batch_size_unknown = True
self.loc = loc
self.scale = scale
else:
if isinstance(loc, float) and isinstance(scale, float):
self.all_arg_is_float = True
self.loc, self.scale = self._to_variable(loc, scale)
def sample(self, shape, seed=0):
"""Generate samples of the specified shape.
Args:
shape (list): 1D `int32`. Shape of the generated samples.
seed (int): Python integer number.
Returns:
Variable: A tensor with prepended dimensions shape.The data type is float32.
"""
check_type(shape, 'shape', (list), 'sample')
check_type(seed, 'seed', (int), 'sample')
batch_shape = list((self.loc + self.scale).shape)
if self.batch_size_unknown:
output_shape = shape + batch_shape
zero_tmp = tensor.fill_constant_batch_size_like(
self.loc + self.scale, batch_shape + shape, self.loc.dtype, 0.0
)
zero_tmp_shape = nn.shape(zero_tmp)
normal_random_tmp = nn.gaussian_random(
zero_tmp_shape, mean=0.0, std=1.0, seed=seed
)
output = normal_random_tmp * (zero_tmp + self.scale) + self.loc
return paddle.reshape(output, output_shape)
else:
output_shape = shape + batch_shape
output = (
nn.gaussian_random(output_shape, mean=0.0, std=1.0, seed=seed)
* (
tensor.zeros(output_shape, dtype=self.loc.dtype)
+ self.scale
)
+ self.loc
)
if self.all_arg_is_float:
return paddle.reshape(output, shape)
else:
return output
def entropy(self):
"""Shannon entropy in nats.
Returns:
Variable: Shannon entropy of normal distribution.The data type is float32.
"""
batch_shape = list((self.loc + self.scale).shape)
zero_tmp = tensor.fill_constant_batch_size_like(
self.loc + self.scale, batch_shape, self.loc.dtype, 0.0
)
return (
0.5
+ 0.5 * math.log(2 * math.pi)
+ paddle.log((self.scale + zero_tmp))
)
def log_prob(self, value):
"""Log probability density/mass function.
Args:
value (Variable): The input tensor.
Returns:
Variable: log probability.The data type is same with value.
"""
check_variable_and_dtype(
value, 'value', ['float32', 'float64'], 'log_prob'
)
var = self.scale * self.scale
log_scale = paddle.log(self.scale)
return (
-1.0 * ((value - self.loc) * (value - self.loc)) / (2.0 * var)
- log_scale
- math.log(math.sqrt(2.0 * math.pi))
)
def kl_divergence(self, other):
"""The KL-divergence between two normal distributions.
Args:
other (Normal): instance of Normal.
Returns:
Variable: kl-divergence between two normal distributions.The data type is float32.
"""
check_type(other, 'other', Normal, 'kl_divergence')
var_ratio = self.scale / other.scale
var_ratio = var_ratio * var_ratio
t1 = (self.loc - other.loc) / other.scale
t1 = t1 * t1
return 0.5 * (var_ratio + t1 - 1.0 - paddle.log(var_ratio))
class Categorical(Distribution):
r"""
Categorical distribution is a discrete probability distribution that
describes the possible results of a random variable that can take on
one of K possible categories, with the probability of each category
separately specified.
The probability mass function (pmf) is:
.. math::
pmf(k; p_i) = \prod_{i=1}^{k} p_i^{[x=i]}
In the above equation:
* :math:`[x=i]` : it evaluates to 1 if :math:`x==i` , 0 otherwise.
Args:
logits(list|numpy.ndarray|Variable): The logits input of categorical distribution. The data type is float32.
Examples:
.. code-block:: python
import numpy as np
from paddle.fluid import layers
from paddle.fluid.layers import Categorical
a_logits_npdata = np.array([-0.602,-0.602], dtype="float32")
a_logits_tensor = layers.create_tensor(dtype="float32")
layers.assign(a_logits_npdata, a_logits_tensor)
b_logits_npdata = np.array([-0.102,-0.112], dtype="float32")
b_logits_tensor = layers.create_tensor(dtype="float32")
layers.assign(b_logits_npdata, b_logits_tensor)
a = Categorical(a_logits_tensor)
b = Categorical(b_logits_tensor)
a.entropy()
# [0.6931472] with shape: [1]
b.entropy()
# [0.6931347] with shape: [1]
a.kl_divergence(b)
# [1.2516975e-05] with shape: [1]
"""
def __init__(self, logits):
"""
Args:
logits(list|numpy.ndarray|Variable): The logits input of categorical distribution. The data type is float32.
"""
check_type(
logits, 'logits', (np.ndarray, tensor.Variable, list), 'Categorical'
)
if self._validate_args(logits):
self.logits = logits
else:
self.logits = self._to_variable(logits)[0]
def kl_divergence(self, other):
"""The KL-divergence between two Categorical distributions.
Args:
other (Categorical): instance of Categorical. The data type is float32.
Returns:
Variable: kl-divergence between two Categorical distributions.
"""
check_type(other, 'other', Categorical, 'kl_divergence')
logits = self.logits - paddle.max(self.logits, axis=-1, keepdim=True)
other_logits = other.logits - paddle.max(
other.logits, axis=-1, keepdim=True
)
e_logits = paddle.exp(logits)
other_e_logits = paddle.exp(other_logits)
z = paddle.sum(e_logits, axis=-1, keepdim=True)
other_z = paddle.sum(other_e_logits, axis=-1, keepdim=True)
prob = e_logits / z
kl = paddle.sum(
prob
* (logits - paddle.log(z) - other_logits + paddle.log(other_z)),
axis=-1,
keepdim=True,
)
return kl
def entropy(self):
"""Shannon entropy in nats.
Returns:
Variable: Shannon entropy of Categorical distribution. The data type is float32.
"""
logits = self.logits - paddle.max(self.logits, axis=-1, keepdim=True)
e_logits = paddle.exp(logits)
z = paddle.sum(e_logits, axis=-1, keepdim=True)
prob = e_logits / z
entropy = -1.0 * paddle.sum(
prob * (logits - paddle.log(z)), axis=-1, keepdim=True
)
return entropy
class MultivariateNormalDiag(Distribution):
r"""
A multivariate normal (also called Gaussian) distribution parameterized by a mean vector
and a covariance matrix.
The probability density function (pdf) is:
.. math::
pdf(x; loc, scale) = \\frac{e^{-\\frac{||y||^2}{2}}}{Z}
where:
.. math::
y = inv(scale) @ (x - loc)
Z = (2\\pi)^{0.5k} |det(scale)|
In the above equation:
* :math:`inv` : denotes to take the inverse of the matrix.
* :math:`@` : denotes matrix multiplication.
* :math:`det` : denotes to evaluate the determinant.
Args:
loc(list|numpy.ndarray|Variable): The mean of multivariateNormal distribution with shape :math:`[k]` .
The data type is float32.
scale(list|numpy.ndarray|Variable): The positive definite diagonal covariance matrix of multivariateNormal
distribution with shape :math:`[k, k]` . All elements are 0 except diagonal elements. The data type is
float32.
Examples:
.. code-block:: python
import numpy as np
from paddle.fluid import layers
from paddle.fluid.layers import MultivariateNormalDiag
a_loc_npdata = np.array([0.3,0.5],dtype="float32")
a_loc_tensor = layers.create_tensor(dtype="float32")
layers.assign(a_loc_npdata, a_loc_tensor)
a_scale_npdata = np.array([[0.4,0],[0,0.5]],dtype="float32")
a_scale_tensor = layers.create_tensor(dtype="float32")
layers.assign(a_scale_npdata, a_scale_tensor)
b_loc_npdata = np.array([0.2,0.4],dtype="float32")
b_loc_tensor = layers.create_tensor(dtype="float32")
layers.assign(b_loc_npdata, b_loc_tensor)
b_scale_npdata = np.array([[0.3,0],[0,0.4]],dtype="float32")
b_scale_tensor = layers.create_tensor(dtype="float32")
layers.assign(b_scale_npdata, b_scale_tensor)
a = MultivariateNormalDiag(a_loc_tensor, a_scale_tensor)
b = MultivariateNormalDiag(b_loc_tensor, b_scale_tensor)
a.entropy()
# [2.033158] with shape: [1]
b.entropy()
# [1.7777451] with shape: [1]
a.kl_divergence(b)
# [0.06542051] with shape: [1]
"""
def __init__(self, loc, scale):
check_type(
loc,
'loc',
(np.ndarray, tensor.Variable, list),
'MultivariateNormalDiag',
)
check_type(
scale,
'scale',
(np.ndarray, tensor.Variable, list),
'MultivariateNormalDiag',
)
if self._validate_args(loc, scale):
self.loc = loc
self.scale = scale
else:
self.loc, self.scale = self._to_variable(loc, scale)
def _det(self, value):
batch_shape = list(value.shape)
one_all = paddle.ones(shape=batch_shape, dtype=self.loc.dtype)
one_diag = tensor.diag(
paddle.ones(shape=[batch_shape[0]], dtype=self.loc.dtype)
)
det_diag = paddle.prod(value + one_all - one_diag)
return det_diag
def _inv(self, value):
batch_shape = list(value.shape)
one_all = paddle.ones(shape=batch_shape, dtype=self.loc.dtype)
one_diag = tensor.diag(
paddle.ones(shape=[batch_shape[0]], dtype=self.loc.dtype)
)
inv_diag = paddle.pow(value, (one_all - 2 * one_diag))
return inv_diag
def entropy(self):
"""Shannon entropy in nats.
Returns:
Variable: Shannon entropy of Multivariate Normal distribution. The data type is float32.
"""
entropy = 0.5 * (
self.scale.shape[0] * (1.0 + math.log(2 * math.pi))
+ paddle.log(self._det(self.scale))
)
return entropy
def kl_divergence(self, other):
"""The KL-divergence between two Multivariate Normal distributions.
Args:
other (MultivariateNormalDiag): instance of Multivariate Normal.
Returns:
Variable: kl-divergence between two Multivariate Normal distributions. The data type is float32.
"""
check_type(other, 'other', MultivariateNormalDiag, 'kl_divergence')
tr_cov_matmul = paddle.sum(self._inv(other.scale) * self.scale)
loc_matmul_cov = nn.matmul(
(other.loc - self.loc), self._inv(other.scale)
)
tri_matmul = nn.matmul(loc_matmul_cov, (other.loc - self.loc))
k = list(self.scale.shape)[0]
ln_cov = paddle.log(self._det(other.scale)) - paddle.log(
self._det(self.scale)
)
kl = 0.5 * (tr_cov_matmul + tri_matmul - k + ln_cov)
return kl
...@@ -714,7 +714,7 @@ class Auc(MetricBase): ...@@ -714,7 +714,7 @@ class Auc(MetricBase):
The auc metric is for binary classification. The auc metric is for binary classification.
Refer to https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve. 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. 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`, The `auc` function creates four local variables, `true_positives`,
`true_negatives`, `false_positives` and `false_negatives` that are used to `true_negatives`, `false_positives` and `false_negatives` that are used to
......
...@@ -49,7 +49,7 @@ def convolution_net( ...@@ -49,7 +49,7 @@ def convolution_net(
) )
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(cost) 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 return avg_cost, accuracy, prediction
...@@ -84,7 +84,7 @@ def stacked_lstm_net( ...@@ -84,7 +84,7 @@ def stacked_lstm_net(
) )
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(cost) 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 return avg_cost, accuracy, prediction
......
...@@ -121,7 +121,7 @@ def train(net_type, use_cuda, save_dirname, is_local): ...@@ -121,7 +121,7 @@ def train(net_type, use_cuda, save_dirname, is_local):
predict = fluid.layers.fc(input=net, size=classdim, act='softmax') predict = fluid.layers.fc(input=net, size=classdim, act='softmax')
cost = fluid.layers.cross_entropy(input=predict, label=label) cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = paddle.mean(cost) 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
test_program = fluid.default_main_program().clone(for_test=True) test_program = fluid.default_main_program().clone(for_test=True)
......
...@@ -32,7 +32,7 @@ def loss_net(hidden, label): ...@@ -32,7 +32,7 @@ def loss_net(hidden, label):
prediction = fluid.layers.fc(input=hidden, size=10, act='softmax') prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label) loss = fluid.layers.cross_entropy(input=prediction, label=label)
avg_loss = paddle.mean(loss) 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 return prediction, avg_loss, acc
......
...@@ -66,7 +66,7 @@ def net(): ...@@ -66,7 +66,7 @@ def net():
cost, y_predict = fluid.layers.softmax_with_cross_entropy( cost, y_predict = fluid.layers.softmax_with_cross_entropy(
hidden, y, return_softmax=True 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) avg_cost = paddle.mean(cost)
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.05) sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.05)
......
...@@ -39,7 +39,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -39,7 +39,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -106,7 +106,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -106,7 +106,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
with fluid.device_guard("gpu:1"): with fluid.device_guard("gpu:1"):
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -106,7 +106,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -106,7 +106,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
with fluid.device_guard("gpu:1"): with fluid.device_guard("gpu:1"):
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -98,7 +98,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -98,7 +98,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
with fluid.device_guard("gpu:0"): with fluid.device_guard("gpu:0"):
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -82,7 +82,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -82,7 +82,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -99,8 +99,8 @@ class TestDistCTR2x2(TestDistRunnerBase): ...@@ -99,8 +99,8 @@ class TestDistCTR2x2(TestDistRunnerBase):
merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1) merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)
predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax') 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 input=predict, label=label
) )
cost = fluid.layers.cross_entropy(input=predict, label=label) cost = fluid.layers.cross_entropy(input=predict, label=label)
......
...@@ -148,9 +148,9 @@ class TestDistCTR2x2(FleetDistRunnerBase): ...@@ -148,9 +148,9 @@ class TestDistCTR2x2(FleetDistRunnerBase):
merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1) merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)
predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax') 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 input=predict, label=label
) )
......
...@@ -84,7 +84,7 @@ class TestFleetMetaOptimizerPrecision(TestDistRunnerBase): ...@@ -84,7 +84,7 @@ class TestFleetMetaOptimizerPrecision(TestDistRunnerBase):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -84,7 +84,7 @@ class TestFleetMetaOptimizerFuseAllReducePrecision(TestDistRunnerBase): ...@@ -84,7 +84,7 @@ class TestFleetMetaOptimizerFuseAllReducePrecision(TestDistRunnerBase):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -138,8 +138,8 @@ class TestDistCTR2x2(FleetDistRunnerBase): ...@@ -138,8 +138,8 @@ class TestDistCTR2x2(FleetDistRunnerBase):
merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1) merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)
predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax') 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, _, _ = fluid.layers.auc(input=predict, label=label) auc_var, _, _ = paddle.static.auc(input=predict, label=label)
cost = fluid.layers.cross_entropy(input=predict, label=label) cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = paddle.mean(x=cost) avg_cost = paddle.mean(x=cost)
......
...@@ -83,7 +83,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -83,7 +83,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -48,7 +48,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -48,7 +48,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -42,7 +42,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -42,7 +42,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -39,7 +39,7 @@ class TestDistMnist2x2(TestDistRunnerBase): ...@@ -39,7 +39,7 @@ class TestDistMnist2x2(TestDistRunnerBase):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -224,8 +224,8 @@ class DistSeResneXt2x2(TestDistRunnerBase): ...@@ -224,8 +224,8 @@ class DistSeResneXt2x2(TestDistRunnerBase):
cost = fluid.layers.cross_entropy(input=out, label=label) cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost) avg_cost = paddle.mean(x=cost)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1) acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5) acc_top5 = paddle.static.accuracy(input=out, label=label, k=5)
# Evaluator # Evaluator
test_program = fluid.default_main_program().clone(for_test=True) test_program = fluid.default_main_program().clone(for_test=True)
......
...@@ -134,7 +134,7 @@ class TestDistTextClassification2x2(TestDistRunnerBase): ...@@ -134,7 +134,7 @@ class TestDistTextClassification2x2(TestDistRunnerBase):
predict = conv_net(data, dict_dim) predict = conv_net(data, dict_dim)
cost = fluid.layers.cross_entropy(input=predict, label=label) cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = paddle.mean(x=cost) 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() inference_program = fluid.default_main_program().clone()
# Optimization # Optimization
......
...@@ -12,18 +12,6 @@ ...@@ -12,18 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # 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: class DistributionNumpy:
def sample(self): def sample(self):
...@@ -40,153 +28,3 @@ class DistributionNumpy: ...@@ -40,153 +28,3 @@ class DistributionNumpy:
def probs(self, value): def probs(self, value):
raise NotImplementedError 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): ...@@ -424,7 +424,7 @@ class PretrainModelLayer(Layer):
logits=next_sent_fc_out, label=labels, return_softmax=True 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 input=next_sent_softmax, label=labels
) )
......
...@@ -110,7 +110,7 @@ class MNIST(fluid.dygraph.Layer): ...@@ -110,7 +110,7 @@ class MNIST(fluid.dygraph.Layer):
def forward(self, inputs, label=None): def forward(self, inputs, label=None):
x = self.inference(inputs) x = self.inference(inputs)
if label is not None: 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) loss = fluid.layers.cross_entropy(x, label)
avg_loss = paddle.mean(loss) avg_loss = paddle.mean(loss)
......
...@@ -540,8 +540,8 @@ def train_mobilenet(args, to_static): ...@@ -540,8 +540,8 @@ def train_mobilenet(args, to_static):
input=softmax_out, label=label input=softmax_out, label=label
) )
avg_loss = paddle.mean(x=loss) avg_loss = paddle.mean(x=loss)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1) acc_top1 = paddle.static.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5) acc_top5 = paddle.static.accuracy(input=out, label=label, k=5)
t_start_back = time.time() t_start_back = time.time()
loss_data.append(avg_loss.numpy()) loss_data.append(avg_loss.numpy())
......
...@@ -276,10 +276,10 @@ class ResNetHelper: ...@@ -276,10 +276,10 @@ class ResNetHelper:
pred = resnet(img) pred = resnet(img)
loss = fluid.layers.cross_entropy(input=pred, label=label) loss = fluid.layers.cross_entropy(input=pred, label=label)
avg_loss = paddle.mean(x=loss) avg_loss = paddle.mean(x=loss)
acc_top1 = fluid.layers.accuracy( acc_top1 = paddle.static.accuracy(
input=pred, label=label, k=1 input=pred, label=label, k=1
) )
acc_top5 = fluid.layers.accuracy( acc_top5 = paddle.static.accuracy(
input=pred, label=label, k=5 input=pred, label=label, k=5
) )
......
...@@ -76,8 +76,8 @@ def train(to_static, build_strategy=None): ...@@ -76,8 +76,8 @@ def train(to_static, build_strategy=None):
# If we remove it, the loss between dygraph and dy2stat is exactly same. # If we remove it, the loss between dygraph and dy2stat is exactly same.
loss = fluid.layers.cross_entropy(input=pred, label=label) loss = fluid.layers.cross_entropy(input=pred, label=label)
avg_loss = paddle.mean(x=pred) avg_loss = paddle.mean(x=pred)
acc_top1 = fluid.layers.accuracy(input=pred, label=label, k=1) acc_top1 = paddle.static.accuracy(input=pred, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=pred, label=label, k=5) acc_top5 = paddle.static.accuracy(input=pred, label=label, k=5)
scaled = scaler.scale(avg_loss) scaled = scaler.scale(avg_loss)
scaled.backward() scaled.backward()
......
...@@ -77,8 +77,8 @@ def train(to_static, build_strategy=None): ...@@ -77,8 +77,8 @@ def train(to_static, build_strategy=None):
pred = resnet(img) pred = resnet(img)
loss = fluid.layers.cross_entropy(input=pred, label=label) loss = fluid.layers.cross_entropy(input=pred, label=label)
avg_loss = paddle.mean(x=pred) avg_loss = paddle.mean(x=pred)
acc_top1 = fluid.layers.accuracy(input=pred, label=label, k=1) acc_top1 = paddle.static.accuracy(input=pred, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=pred, label=label, k=5) acc_top5 = paddle.static.accuracy(input=pred, label=label, k=5)
scaled = scaler.scale(avg_loss) scaled = scaler.scale(avg_loss)
scaled.backward() scaled.backward()
......
...@@ -347,8 +347,8 @@ class SeResNeXt(fluid.dygraph.Layer): ...@@ -347,8 +347,8 @@ class SeResNeXt(fluid.dygraph.Layer):
loss = fluid.layers.cross_entropy(input=softmax_out, label=label) loss = fluid.layers.cross_entropy(input=softmax_out, label=label)
avg_loss = paddle.mean(x=loss) avg_loss = paddle.mean(x=loss)
acc_top1 = fluid.layers.accuracy(input=softmax_out, label=label, k=1) acc_top1 = paddle.static.accuracy(input=softmax_out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=softmax_out, label=label, k=5) acc_top5 = paddle.static.accuracy(input=softmax_out, label=label, k=5)
return out, avg_loss, acc_top1, acc_top5 return out, avg_loss, acc_top1, acc_top5
......
...@@ -109,7 +109,7 @@ class CNN(fluid.dygraph.Layer): ...@@ -109,7 +109,7 @@ class CNN(fluid.dygraph.Layer):
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost) 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 return avg_cost, prediction, acc
...@@ -152,7 +152,7 @@ class BOW(fluid.dygraph.Layer): ...@@ -152,7 +152,7 @@ class BOW(fluid.dygraph.Layer):
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost) 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 return avg_cost, prediction, acc
...@@ -198,7 +198,7 @@ class GRU(fluid.dygraph.Layer): ...@@ -198,7 +198,7 @@ class GRU(fluid.dygraph.Layer):
prediction = paddle.nn.functional.softmax(prediction) prediction = paddle.nn.functional.softmax(prediction)
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost) 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 return avg_cost, prediction, acc
...@@ -257,7 +257,7 @@ class BiGRU(fluid.dygraph.Layer): ...@@ -257,7 +257,7 @@ class BiGRU(fluid.dygraph.Layer):
# if label is not None: # if label is not None:
cost = fluid.layers.cross_entropy(input=prediction, label=label) cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost) 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 return avg_cost, prediction, acc
# else: # else:
# return prediction # return prediction
......
...@@ -334,10 +334,10 @@ def train(args, fake_data_reader, to_static): ...@@ -334,10 +334,10 @@ def train(args, fake_data_reader, to_static):
input=outputs, label=labels, ignore_index=-1 input=outputs, label=labels, ignore_index=-1
) )
avg_loss = paddle.mean(loss) avg_loss = paddle.mean(loss)
acc_top1 = fluid.layers.accuracy( acc_top1 = paddle.static.accuracy(
input=outputs, label=labels, k=1 input=outputs, label=labels, k=1
) )
acc_top5 = fluid.layers.accuracy( acc_top5 = paddle.static.accuracy(
input=outputs, label=labels, k=5 input=outputs, label=labels, k=5
) )
......
...@@ -74,14 +74,14 @@ class TestAccuracyOpError(unittest.TestCase): ...@@ -74,14 +74,14 @@ class TestAccuracyOpError(unittest.TestCase):
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=[-1, 1], dtype="int32" 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) self.assertRaises(TypeError, paddle.metric.accuracy, x1, label)
# The input dtype of accuracy_op must be float32 or float64. # The input dtype of accuracy_op must be float32 or float64.
x2 = fluid.layers.data(name='x2', shape=[4], dtype="int32") 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) self.assertRaises(TypeError, paddle.metric.accuracy, x2, label)
x3 = fluid.layers.data(name='input', shape=[-1, 2], dtype="float16") 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) paddle.metric.accuracy(input=x3, label=label)
......
...@@ -69,14 +69,14 @@ class TestAccuracyOpError(unittest.TestCase): ...@@ -69,14 +69,14 @@ class TestAccuracyOpError(unittest.TestCase):
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=[-1, 1], dtype="int32" 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) self.assertRaises(TypeError, paddle.metric.accuracy, x1, label)
# The input dtype of accuracy_op must be float32 or float64. # The input dtype of accuracy_op must be float32 or float64.
x2 = fluid.layers.data(name='x2', shape=[4], dtype="int32") 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) self.assertRaises(TypeError, paddle.metric.accuracy, x2, label)
x3 = fluid.layers.data(name='input', shape=[-1, 2], dtype="float16") 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) paddle.metric.accuracy(input=x3, label=label)
......
...@@ -59,7 +59,7 @@ def convolutional_neural_network(use_py_reader): ...@@ -59,7 +59,7 @@ def convolutional_neural_network(use_py_reader):
prediction = fluid.layers.fc(input=conv_pool_2, size=10, act='softmax') prediction = fluid.layers.fc(input=conv_pool_2, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label) loss = fluid.layers.cross_entropy(input=prediction, label=label)
avg_loss = paddle.mean(loss) 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') i = fluid.layers.zeros(shape=[1], dtype='int64')
array = fluid.layers.array_write(x=prediction, i=i) array = fluid.layers.array_write(x=prediction, i=i)
fluid.layers.increment(i) fluid.layers.increment(i)
......
...@@ -158,7 +158,7 @@ class TestAucOpError(unittest.TestCase): ...@@ -158,7 +158,7 @@ class TestAucOpError(unittest.TestCase):
name="input2", shape=[-1, 2], dtype="float32" name="input2", shape=[-1, 2], dtype="float32"
) )
label2 = fluid.data(name="label2", shape=[-1], 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) self.assertRaises(TypeError, test_type2)
......
...@@ -78,7 +78,7 @@ def get_model(batch_size): ...@@ -78,7 +78,7 @@ def get_model(batch_size):
# Evaluator # Evaluator
batch_size_tensor = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size_tensor
) )
......
...@@ -12,20 +12,6 @@ ...@@ -12,20 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import math
import unittest
import numpy as np
from paddle import fluid
from paddle.fluid import layers
from paddle.fluid.layers.distributions import (
Categorical,
MultivariateNormalDiag,
Normal,
Uniform,
)
class DistributionNumpy: class DistributionNumpy:
""" """
...@@ -47,788 +33,3 @@ class DistributionNumpy: ...@@ -47,788 +33,3 @@ class DistributionNumpy:
def log_prob(self, value): def log_prob(self, value):
"""Log probability density/mass function.""" """Log probability density/mass function."""
raise NotImplementedError raise NotImplementedError
class UniformNumpy(DistributionNumpy):
def __init__(self, low, high):
self.low = np.array(low).astype('float32')
self.high = np.array(high).astype('float32')
def sample(self, shape):
shape = tuple(shape) + (self.low + self.high).shape
return self.low + (
np.random.uniform(size=shape) * (self.high - self.low)
)
def log_prob(self, value):
lb = np.less(self.low, value).astype('float32')
ub = np.less(value, self.high).astype('float32')
return np.log(lb * ub) - np.log(self.high - self.low)
def entropy(self):
return np.log(self.high - self.low)
class NormalNumpy(DistributionNumpy):
def __init__(self, loc, scale):
self.loc = np.array(loc).astype('float32')
self.scale = np.array(scale).astype('float32')
def sample(self, shape):
shape = tuple(shape) + (self.loc + self.scale).shape
return self.loc + (np.random.randn(*shape) * self.scale)
def log_prob(self, value):
var = self.scale * self.scale
log_scale = np.log(self.scale)
return (
-((value - self.loc) * (value - self.loc)) / (2.0 * var)
- log_scale
- math.log(math.sqrt(2.0 * math.pi))
)
def entropy(self):
return (
0.5
+ 0.5 * np.log(np.array(2.0 * math.pi).astype('float32'))
+ np.log(self.scale)
)
def kl_divergence(self, other):
var_ratio = self.scale / other.scale
var_ratio = var_ratio * var_ratio
t1 = (self.loc - other.loc) / other.scale
t1 = t1 * t1
return 0.5 * (var_ratio + t1 - 1 - np.log(var_ratio))
class CategoricalNumpy(DistributionNumpy):
def __init__(self, logits):
self.logits = np.array(logits).astype('float32')
def entropy(self):
logits = self.logits - np.max(self.logits, axis=-1, keepdims=True)
e_logits = np.exp(logits)
z = np.sum(e_logits, axis=-1, keepdims=True)
prob = e_logits / z
return -1.0 * np.sum(
prob * (logits - np.log(z)), axis=-1, keepdims=True
)
def kl_divergence(self, other):
logits = self.logits - np.max(self.logits, axis=-1, keepdims=True)
other_logits = other.logits - np.max(
other.logits, axis=-1, keepdims=True
)
e_logits = np.exp(logits)
other_e_logits = np.exp(other_logits)
z = np.sum(e_logits, axis=-1, keepdims=True)
other_z = np.sum(other_e_logits, axis=-1, keepdims=True)
prob = e_logits / z
return np.sum(
prob * (logits - np.log(z) - other_logits + np.log(other_z)),
axis=-1,
keepdims=True,
)
class MultivariateNormalDiagNumpy(DistributionNumpy):
def __init__(self, loc, scale):
self.loc = np.array(loc).astype('float32')
self.scale = np.array(scale).astype('float32')
def _det(self, value):
batch_shape = list(value.shape)
one_all = np.ones(shape=batch_shape, dtype='float32')
one_diag = np.eye(batch_shape[0], dtype='float32')
det_diag = np.prod(value + one_all - one_diag)
return det_diag
def _inv(self, value):
batch_shape = list(value.shape)
one_all = np.ones(shape=batch_shape, dtype='float32')
one_diag = np.eye(batch_shape[0], dtype='float32')
inv_diag = np.power(value, (one_all - 2 * one_diag))
return inv_diag
def entropy(self):
return 0.5 * (
self.scale.shape[0]
* (1.0 + np.log(np.array(2 * math.pi).astype('float32')))
+ np.log(self._det(self.scale))
)
def kl_divergence(self, other):
tr_cov_matmul = np.sum(self._inv(other.scale) * self.scale)
loc_matmul_cov = np.matmul(
(other.loc - self.loc), self._inv(other.scale)
)
tri_matmul = np.matmul(loc_matmul_cov, (other.loc - self.loc))
k = list(self.scale.shape)[0]
ln_cov = np.log(self._det(other.scale)) - np.log(self._det(self.scale))
kl = 0.5 * (tr_cov_matmul + tri_matmul - k + ln_cov)
return kl
class DistributionTest(unittest.TestCase):
def setUp(self, use_gpu=False):
self.use_gpu = use_gpu
if not use_gpu:
place = fluid.CPUPlace()
self.gpu_id = -1
else:
place = fluid.CUDAPlace(0)
self.gpu_id = 0
self.executor = fluid.Executor(place)
def build_normal_program(
self,
test_program,
batch_size,
dims,
loc_float,
scale_float,
other_loc_float,
other_scale_float,
scale_np,
other_scale_np,
loc_np,
other_loc_np,
values_np,
):
with fluid.program_guard(test_program):
loc = layers.data(name='loc', shape=[dims], dtype='float32')
scale = layers.data(name='scale', shape=[dims], dtype='float32')
other_loc = layers.data(
name='other_loc', shape=[dims], dtype='float32'
)
other_scale = layers.data(
name='other_scale', shape=[dims], dtype='float32'
)
values = layers.data(name='values', shape=[dims], dtype='float32')
normal_float = Normal(loc_float, scale_float)
other_normal_float = Normal(other_loc_float, other_scale_float)
normal_float_np_broadcast = Normal(loc_float, scale_np)
other_normal_float_np_broadcast = Normal(
other_loc_float, other_scale_np
)
normal_np = Normal(loc_np, scale_np)
other_normal_np = Normal(other_loc_np, other_scale_np)
normal_variable = Normal(loc, scale)
other_normal_variable = Normal(other_loc, other_scale)
sample_float = normal_float.sample([batch_size, dims])
sample_float_np_broadcast = normal_float_np_broadcast.sample(
[batch_size, dims]
)
sample_np = normal_np.sample([batch_size, dims])
sample_variable = normal_variable.sample([batch_size, dims])
entropy_float = normal_float.entropy()
entropy_float_np_broadcast = normal_float_np_broadcast.entropy()
entropy_np = normal_np.entropy()
entropy_variable = normal_variable.entropy()
lp_float_np_broadcast = normal_float_np_broadcast.log_prob(values)
lp_np = normal_np.log_prob(values)
lp_variable = normal_variable.log_prob(values)
kl_float = normal_float.kl_divergence(other_normal_float)
kl_float_np_broadcast = normal_float_np_broadcast.kl_divergence(
other_normal_float_np_broadcast
)
kl_np = normal_np.kl_divergence(other_normal_np)
kl_variable = normal_variable.kl_divergence(other_normal_variable)
fetch_list = [
sample_float,
sample_float_np_broadcast,
sample_np,
sample_variable,
entropy_float,
entropy_float_np_broadcast,
entropy_np,
entropy_variable,
lp_float_np_broadcast,
lp_np,
lp_variable,
kl_float,
kl_float_np_broadcast,
kl_np,
kl_variable,
]
feed_vars = {
'loc': loc_np,
'scale': scale_np,
'other_loc': other_loc_np,
'other_scale': other_scale_np,
'values': values_np,
}
return feed_vars, fetch_list
def get_normal_random_input(self, batch_size, dims):
loc_np = np.random.randn(batch_size, dims).astype('float32')
other_loc_np = np.random.randn(batch_size, dims).astype('float32')
loc_float = (np.random.ranf() - 0.5) * 4
scale_float = (np.random.ranf() - 0.5) * 4
while scale_float < 0:
scale_float = (np.random.ranf() - 0.5) * 4
other_loc_float = (np.random.ranf() - 0.5) * 4
other_scale_float = (np.random.ranf() - 0.5) * 4
while other_scale_float < 0:
other_scale_float = (np.random.ranf() - 0.5) * 4
scale_np = np.random.randn(batch_size, dims).astype('float32')
other_scale_np = np.random.randn(batch_size, dims).astype('float32')
values_np = np.random.randn(batch_size, dims).astype('float32')
while not np.all(scale_np > 0):
scale_np = np.random.randn(batch_size, dims).astype('float32')
while not np.all(other_scale_np > 0):
other_scale_np = np.random.randn(batch_size, dims).astype('float32')
return (
loc_np,
other_loc_np,
loc_float,
scale_float,
other_loc_float,
other_scale_float,
scale_np,
other_scale_np,
values_np,
)
def test_normal_distribution(self, batch_size=2, dims=3, tolerance=1e-6):
test_program = fluid.Program()
(
loc_np,
other_loc_np,
loc_float,
scale_float,
other_loc_float,
other_scale_float,
scale_np,
other_scale_np,
values_np,
) = self.get_normal_random_input(batch_size, dims)
feed_vars, fetch_list = self.build_normal_program(
test_program,
batch_size,
dims,
loc_float,
scale_float,
other_loc_float,
other_scale_float,
scale_np,
other_scale_np,
loc_np,
other_loc_np,
values_np,
)
self.executor.run(fluid.default_startup_program())
np_normal_float = NormalNumpy(loc_float, scale_float)
np_other_normal_float = NormalNumpy(other_loc_float, other_scale_float)
np_normal_float_np_broadcast = NormalNumpy(loc_float, scale_np)
np_other_normal_float_np_broadcast = NormalNumpy(
other_loc_float, other_scale_np
)
np_normal = NormalNumpy(loc_np, scale_np)
np_other_normal = NormalNumpy(other_loc_np, other_scale_np)
gt_sample_float = np_normal_float.sample([batch_size, dims])
gt_sample_float_np_broadcast = np_normal_float_np_broadcast.sample(
[batch_size, dims]
)
gt_sample_np = np_normal.sample([batch_size, dims])
gt_entropy_float = np_normal_float.entropy()
gt_entropy_float_np_broadcast = np_normal_float_np_broadcast.entropy()
gt_entropy = np_normal.entropy()
gt_lp_float_np_broadcast = np_normal_float_np_broadcast.log_prob(
values_np
)
gt_lp = np_normal.log_prob(values_np)
gt_kl_float = np_normal_float.kl_divergence(np_other_normal_float)
gt_kl_float_np_broadcast = np_normal_float_np_broadcast.kl_divergence(
np_other_normal_float_np_broadcast
)
gt_kl = np_normal.kl_divergence(np_other_normal)
[
output_sample_float,
output_sample_float_np_broadcast,
output_sample_np,
output_sample_variable,
output_entropy_float,
output_entropy_float_np_broadcast,
output_entropy_np,
output_entropy_variable,
output_lp_float_np_broadcast,
output_lp_np,
output_lp_variable,
output_kl_float,
output_kl_float_np_broadcast,
output_kl_np,
output_kl_variable,
] = self.executor.run(
program=test_program, feed=feed_vars, fetch_list=fetch_list
)
np.testing.assert_allclose(
output_sample_float.shape,
gt_sample_float.shape,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_sample_float_np_broadcast.shape,
gt_sample_float_np_broadcast.shape,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_sample_np.shape,
gt_sample_np.shape,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_sample_variable.shape,
gt_sample_np.shape,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_entropy_float,
gt_entropy_float,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_entropy_float_np_broadcast,
gt_entropy_float_np_broadcast,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_entropy_np, gt_entropy, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_entropy_variable, gt_entropy, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_lp_float_np_broadcast,
gt_lp_float_np_broadcast,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_lp_np, gt_lp, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_lp_variable, gt_lp, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_kl_float, gt_kl_float, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_kl_float_np_broadcast,
gt_kl_float_np_broadcast,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_kl_np, gt_kl, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_kl_variable, gt_kl, rtol=tolerance, atol=tolerance
)
def build_uniform_program(
self,
test_program,
batch_size,
dims,
low_float,
high_float,
high_np,
low_np,
values_np,
):
with fluid.program_guard(test_program):
low = layers.data(name='low', shape=[dims], dtype='float32')
high = layers.data(name='high', shape=[dims], dtype='float32')
values = layers.data(name='values', shape=[dims], dtype='float32')
uniform_float = Uniform(low_float, high_float)
uniform_float_np_broadcast = Uniform(low_float, high_np)
uniform_np = Uniform(low_np, high_np)
uniform_variable = Uniform(low, high)
sample_float = uniform_float.sample([batch_size, dims])
sample_float_np_broadcast = uniform_float_np_broadcast.sample(
[batch_size, dims]
)
sample_np = uniform_np.sample([batch_size, dims])
sample_variable = uniform_variable.sample([batch_size, dims])
entropy_float = uniform_float.entropy()
entropy_float_np_broadcast = uniform_float_np_broadcast.entropy()
entropy_np = uniform_np.entropy()
entropy_variable = uniform_variable.entropy()
lp_float_np_broadcast = uniform_float_np_broadcast.log_prob(values)
lp_np = uniform_np.log_prob(values)
lp_variable = uniform_variable.log_prob(values)
fetch_list = [
sample_float,
sample_float_np_broadcast,
sample_np,
sample_variable,
entropy_float,
entropy_float_np_broadcast,
entropy_np,
entropy_variable,
lp_float_np_broadcast,
lp_np,
lp_variable,
]
feed_vars = {'low': low_np, 'high': high_np, 'values': values_np}
return feed_vars, fetch_list
def test_uniform_distribution(self, batch_size=2, dims=3, tolerance=1e-6):
test_program = fluid.Program()
low_np = np.random.randn(batch_size, dims).astype('float32')
low_float = np.random.uniform(-2, 1)
high_float = np.random.uniform(1, 3)
high_np = np.random.uniform(-5.0, 5.0, (batch_size, dims)).astype(
'float32'
)
values_np = np.random.randn(batch_size, dims).astype('float32')
feed_vars, fetch_list = self.build_uniform_program(
test_program,
batch_size,
dims,
low_float,
high_float,
high_np,
low_np,
values_np,
)
self.executor.run(fluid.default_startup_program())
np_uniform_float = UniformNumpy(low_float, high_float)
np_uniform_float_np_broadcast = UniformNumpy(low_float, high_np)
np_uniform = UniformNumpy(low_np, high_np)
gt_sample_float = np_uniform_float.sample([batch_size, dims])
gt_sample_float_np_broadcast = np_uniform_float_np_broadcast.sample(
[batch_size, dims]
)
gt_sample_np = np_uniform.sample([batch_size, dims])
gt_entropy_float = np_uniform_float.entropy()
gt_entropy_float_np_broadcast = np_uniform_float_np_broadcast.entropy()
gt_entropy = np_uniform.entropy()
gt_lp_float_np_broadcast = np_uniform_float_np_broadcast.log_prob(
values_np
)
gt_lp = np_uniform.log_prob(values_np)
# result calculated by paddle
[
output_sample_float,
output_sample_float_np_broadcast,
output_sample_np,
output_sample_variable,
output_entropy_float,
output_entropy_float_np_broadcast,
output_entropy_np,
output_entropy_variable,
output_lp_float_np_broadcast,
output_lp_np,
output_lp_variable,
] = self.executor.run(
program=test_program, feed=feed_vars, fetch_list=fetch_list
)
np.testing.assert_allclose(
output_sample_float.shape,
gt_sample_float.shape,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_sample_float_np_broadcast.shape,
gt_sample_float_np_broadcast.shape,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_sample_np.shape,
gt_sample_np.shape,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_sample_variable.shape,
gt_sample_np.shape,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_entropy_float,
gt_entropy_float,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_entropy_float_np_broadcast,
gt_entropy_float_np_broadcast,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_entropy_np, gt_entropy, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_entropy_variable, gt_entropy, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_lp_float_np_broadcast,
gt_lp_float_np_broadcast,
rtol=tolerance,
atol=tolerance,
)
np.testing.assert_allclose(
output_lp_np, gt_lp, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_lp_variable, gt_lp, rtol=tolerance, atol=tolerance
)
def test_categorical_distribution(
self, batch_size=2, dims=3, tolerance=1e-6
):
test_program = fluid.Program()
logits_np = np.random.randn(batch_size, dims).astype('float32')
other_logits_np = np.random.randn(batch_size, dims).astype('float32')
with fluid.program_guard(test_program):
logits = layers.data(name='logits', shape=[dims], dtype='float32')
other_logits = layers.data(
name='other_logits', shape=[dims], dtype='float32'
)
categorical_np = Categorical(logits_np)
other_categorical_np = Categorical(other_logits_np)
entropy_np = categorical_np.entropy()
kl_np = categorical_np.kl_divergence(other_categorical_np)
self.executor.run(fluid.default_main_program())
np_categorical = CategoricalNumpy(logits_np)
np_other_categorical = CategoricalNumpy(other_logits_np)
gt_entropy_np = np_categorical.entropy()
gt_kl_np = np_categorical.kl_divergence(np_other_categorical)
# result calculated by paddle
[output_entropy_np, output_kl_np] = self.executor.run(
program=test_program,
feed={'logits': logits_np},
fetch_list=[entropy_np, kl_np],
)
np.testing.assert_allclose(
output_entropy_np, gt_entropy_np, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_kl_np, gt_kl_np, rtol=tolerance, atol=tolerance
)
def test_multivariateNormalDiag_distribution(
self, batch_size=2, tolerance=1e-6
):
test_program = fluid.Program()
loc_np = np.random.random(
batch_size,
).astype('float32')
scale_np = np.diag(
np.random.random(
batch_size,
)
).astype('float32')
other_loc_np = np.random.random(
batch_size,
).astype('float32')
other_scale_np = np.diag(
np.random.random(
batch_size,
)
).astype('float32')
with fluid.program_guard(test_program):
loc = layers.data(
name='loc',
shape=[
batch_size,
],
dtype='float32',
append_batch_size=False,
)
scale = layers.data(
name='scale',
shape=[batch_size, batch_size],
dtype='float32',
append_batch_size=False,
)
other_loc = layers.data(
name='other_loc',
shape=[
batch_size,
],
dtype='float32',
append_batch_size=False,
)
other_scale = layers.data(
name='other_scale',
shape=[batch_size, batch_size],
dtype='float32',
append_batch_size=False,
)
multivariate_np = MultivariateNormalDiag(loc, scale)
other_multivariate_np = MultivariateNormalDiag(
other_loc, other_scale
)
entropy_np = multivariate_np.entropy()
other_entropy_np = other_multivariate_np.entropy()
kl_np = multivariate_np.kl_divergence(other_multivariate_np)
self.executor.run(fluid.default_main_program())
np_multivariate = MultivariateNormalDiagNumpy(loc_np, scale_np)
np_other_multivariate = MultivariateNormalDiagNumpy(
other_loc_np, other_scale_np
)
gt_entropy_np = np_multivariate.entropy()
gt_kl_np = np_multivariate.kl_divergence(np_other_multivariate)
# result calculated by paddle
[output_entropy_np, output_kl_np] = self.executor.run(
program=test_program,
feed={
'loc': loc_np,
'scale': scale_np,
'other_loc': other_loc_np,
'other_scale': other_scale_np,
},
fetch_list=[entropy_np, kl_np],
)
np.testing.assert_allclose(
output_entropy_np, gt_entropy_np, rtol=tolerance, atol=tolerance
)
np.testing.assert_allclose(
output_kl_np, gt_kl_np, rtol=tolerance, atol=tolerance
)
class DistributionTestError(unittest.TestCase):
def test_normal_error(self):
loc = int(1)
scale = int(1)
# type of loc and scale must be float, list, numpy.ndarray, Variable
self.assertRaises(TypeError, Normal, loc, 1.0)
self.assertRaises(TypeError, Normal, 1.0, scale)
normal = Normal(0.0, 1.0)
value = [1.0, 2.0]
# type of value must be variable
self.assertRaises(TypeError, normal.log_prob, value)
shape = 1.0
# type of shape must be list
self.assertRaises(TypeError, normal.sample, shape)
seed = 1.0
# type of seed must be int
self.assertRaises(TypeError, normal.sample, [2, 3], seed)
normal_other = Uniform(1.0, 2.0)
# type of other must be an instance of Normal
self.assertRaises(TypeError, normal.kl_divergence, normal_other)
def test_uniform_error(self):
low = int(1)
high = int(1)
# type of loc and scale must be float, list, numpy.ndarray, Variable
self.assertRaises(TypeError, Uniform, low, 1.0)
self.assertRaises(TypeError, Uniform, 1.0, high)
uniform = Uniform(0.0, 1.0)
value = [1.0, 2.0]
# type of value must be variable
self.assertRaises(TypeError, uniform.log_prob, value)
shape = 1.0
# type of shape must be list
self.assertRaises(TypeError, uniform.sample, shape)
seed = 1.0
# type of seed must be int
self.assertRaises(TypeError, uniform.sample, [2, 3], seed)
def test_categorical_error(self):
logit = 1.0
# type of loc and scale must be list, numpy.ndarray, Variable
self.assertRaises(TypeError, Categorical, logit)
categorical = Categorical([-0.602, -0.602])
categorical_other = Normal(1.0, 2.0)
# type of other must be an instance of Normal
self.assertRaises(
TypeError, categorical.kl_divergence, categorical_other
)
def test_multivariate_normal_diag_error(self):
loc = 1.0
scale = 1.0
# type of loc and scale must be list, numpy.ndarray, Variable
self.assertRaises(TypeError, MultivariateNormalDiag, loc, [1.0])
self.assertRaises(TypeError, MultivariateNormalDiag, [1.0], scale)
mnd = MultivariateNormalDiag([0.3, 0.5], [[0.4, 0], [0, 0.5]])
categorical_other = Normal(1.0, 2.0)
# type of other must be an instance of Normal
self.assertRaises(TypeError, mnd.kl_divergence, categorical_other)
if __name__ == '__main__':
unittest.main()
# 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): ...@@ -174,8 +174,8 @@ class TestSaveInferenceModel(unittest.TestCase):
x = layers.data(name='x', shape=[2], dtype='float32') x = layers.data(name='x', shape=[2], dtype='float32')
y = layers.data(name='y', shape=[1], dtype='int32') y = layers.data(name='y', shape=[1], dtype='int32')
predict = fluid.layers.fc(input=x, size=2, act='softmax') predict = fluid.layers.fc(input=x, size=2, act='softmax')
acc = fluid.layers.accuracy(input=predict, label=y) acc = paddle.static.accuracy(input=predict, label=y)
auc_var, batch_auc_var, auc_states = fluid.layers.auc( auc_var, batch_auc_var, auc_states = paddle.static.auc(
input=predict, label=y input=predict, label=y
) )
cost = fluid.layers.cross_entropy(input=predict, label=y) cost = fluid.layers.cross_entropy(input=predict, label=y)
......
...@@ -34,7 +34,6 @@ from paddle.fluid.framework import ( ...@@ -34,7 +34,6 @@ from paddle.fluid.framework import (
program_guard, program_guard,
) )
from paddle.fluid.initializer import Constant from paddle.fluid.initializer import Constant
from paddle.fluid.layers.device import get_places
from paddle.fluid.param_attr import ParamAttr from paddle.fluid.param_attr import ParamAttr
from paddle.tensor import random from paddle.tensor import random
...@@ -2895,7 +2894,7 @@ class TestLayer(LayerTest): ...@@ -2895,7 +2894,7 @@ class TestLayer(LayerTest):
label = fluid.data(name="label", shape=[-1, 1], dtype="int") label = fluid.data(name="label", shape=[-1, 1], dtype="int")
fc_out = fluid.layers.fc(input=data, size=10) fc_out = fluid.layers.fc(input=data, size=10)
predict = fluid.layers.softmax(input=fc_out) 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() place = fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -2911,7 +2910,9 @@ class TestLayer(LayerTest): ...@@ -2911,7 +2910,9 @@ class TestLayer(LayerTest):
label = base.to_variable(y) label = base.to_variable(y)
fc_out = fluid.layers.fc(data, size=10) fc_out = fluid.layers.fc(data, size=10)
predict = fluid.layers.softmax(fc_out) 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()) np.testing.assert_array_equal(static_out[0], dynamic_out.numpy())
...@@ -2954,7 +2955,6 @@ class TestBook(LayerTest): ...@@ -2954,7 +2955,6 @@ class TestBook(LayerTest):
) )
else: else:
assert method.__name__ in ('make_get_places')
continue continue
if method.__name__ in self.only_static_set: if method.__name__ in self.only_static_set:
continue continue
...@@ -3201,12 +3201,6 @@ class TestBook(LayerTest): ...@@ -3201,12 +3201,6 @@ class TestBook(LayerTest):
hid = layers.fc(input=data, size=20) hid = layers.fc(input=data, size=20)
return layers.softmax(hid, axis=1) 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() @prog_scope()
def make_nce(self): def make_nce(self):
window_size = 5 window_size = 5
......
...@@ -60,7 +60,7 @@ class TestProfiler(unittest.TestCase): ...@@ -60,7 +60,7 @@ class TestProfiler(unittest.TestCase):
cost = fluid.layers.cross_entropy(input=predict, label=label) cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = paddle.mean(cost) avg_cost = paddle.mean(cost)
batch_size = fluid.layers.create_tensor(dtype='int64') 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 input=predict, label=label, total=batch_size
) )
......
...@@ -76,7 +76,7 @@ def simple_fc_net_with_accuracy(use_feed): ...@@ -76,7 +76,7 @@ def simple_fc_net_with_accuracy(use_feed):
prediction = fluid.layers.fc(hidden, size=10, act='softmax') prediction = fluid.layers.fc(hidden, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label) loss = fluid.layers.cross_entropy(input=prediction, label=label)
loss = paddle.mean(loss) 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 return loss
......
...@@ -68,11 +68,12 @@ from ..fluid.io import batch # noqa: F401 ...@@ -68,11 +68,12 @@ from ..fluid.io import batch # noqa: F401
from ..fluid.layers import create_parameter # noqa: F401 from ..fluid.layers import create_parameter # noqa: F401
from ..fluid.layers import create_global_var # 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.contrib.layers import ctr_metric_bundle # noqa: F401
from ..fluid.layers import exponential_decay # 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 __all__ = [ # noqa
'append_backward', 'append_backward',
'gradients', 'gradients',
......
...@@ -15,22 +15,16 @@ ...@@ -15,22 +15,16 @@
All layers just related to metric. All layers just related to metric.
""" """
import warnings from paddle.fluid.layer_helper import LayerHelper
from ..layer_helper import LayerHelper from paddle.fluid.initializer import Constant
from ..initializer import Normal, Constant from paddle.fluid.framework import (
from ..framework import (
Variable, Variable,
_non_static_mode, _non_static_mode,
_varbase_creator, _varbase_creator,
_in_legacy_dygraph,
in_dygraph_mode,
) )
from .. import core from paddle.fluid.layers import tensor
from ..param_attr import ParamAttr from paddle.fluid.data_feeder import check_variable_and_dtype
from . import nn from paddle import _legacy_C_ops
from . import tensor
from ..data_feeder import check_variable_and_dtype
from paddle import _C_ops, _legacy_C_ops
__all__ = ['accuracy', 'auc'] __all__ = ['accuracy', 'auc']
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册