未验证 提交 d3c9db75 编写于 作者: Z zhongpu 提交者: GitHub

copy api from paddle to paddle.fluid (#24164)

* copy api from paddle to paddle.fluid, test=develop

* fix optest, test=develop
上级 a4519a5d
此差异已折叠。
此差异已折叠。
...@@ -18,8 +18,8 @@ from six.moves import reduce ...@@ -18,8 +18,8 @@ from six.moves import reduce
from ..layer_helper import LayerHelper from ..layer_helper import LayerHelper
from ..param_attr import ParamAttr from ..param_attr import ParamAttr
from ..initializer import Initializer from ..initializer import Initializer
from ..framework import convert_np_dtype_to_dtype_, in_dygraph_mode, _varbase_creator from ..framework import convert_np_dtype_to_dtype_, in_dygraph_mode, _varbase_creator, device_guard, OpProtoHolder
from ..framework import Variable from ..framework import Variable, in_dygraph_mode
from ..initializer import Constant from ..initializer import Constant
from ..core import VarDesc from ..core import VarDesc
from .. import core from .. import core
...@@ -30,32 +30,12 @@ import numpy ...@@ -30,32 +30,12 @@ import numpy
import warnings import warnings
__all__ = [ __all__ = [
'create_tensor', 'create_tensor', 'create_parameter', 'create_global_var', 'cast',
'create_parameter', 'tensor_array_to_tensor', 'concat', 'sums', 'assign',
'create_global_var', 'fill_constant_batch_size_like', 'fill_constant', 'argmin', 'argmax',
'cast', 'argsort', 'ones', 'zeros', 'reverse', 'has_inf', 'has_nan', 'isfinite',
'tensor_array_to_tensor', 'range', 'linspace', 'zeros_like', 'ones_like', 'diag', 'eye', 'kron',
'concat', 'full_like', 'arange', 'full', 'tril', 'triu'
'sums',
'assign',
'fill_constant_batch_size_like',
'fill_constant',
'argmin',
'argmax',
'argsort',
'ones',
'zeros',
'reverse',
'has_inf',
'has_nan',
'isfinite',
'range',
'linspace',
'zeros_like',
'ones_like',
'diag',
'eye',
'kron',
] ]
...@@ -1587,6 +1567,412 @@ def ones_like(x, out=None): ...@@ -1587,6 +1567,412 @@ def ones_like(x, out=None):
return out return out
def full_like(input,
fill_value,
out=None,
dtype=None,
device=None,
stop_gradient=True,
name=None):
"""
**full_like**
This function creates a tensor filled with `fill_value` which has identical shape and dtype
with `input`.
Args:
input(Variable): The input tensor which specifies shape and dtype.
fill_value: The value to fill the tensor with. Data type can be bool, float32, float64, int32, int64. Default value is 0.
out(Variable): The output tensor.
Returns:
out(Variable): The tensor variable storing the output.
Examples:
.. code-block:: python
import paddle.fluid as fluid
import numpy as np
input = fluid.data(name='input', dtype='float32', shape=[2, 3])
output = fluid.layers.full_like(input, 2.0)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
img=np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
res = exe.run(fluid.default_main_program(), feed={'input':img}, fetch_list=[output])
print(res) # [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)]
"""
helper = LayerHelper("full_like", **locals())
if dtype is None:
dtype = 'float32'
check_dtype(dtype, 'dtype',
['bool', 'float16', 'float32', 'int32', 'int64'], 'full_like')
if out is None:
out = helper.create_variable_for_type_inference(dtype=dtype)
helper.append_op(
type='fill_any_like',
inputs={'X': [input]},
attrs={'value': fill_value},
outputs={'Out': [out]})
out.stop_gradient = stop_gradient
return out
def arange(start, end, step=1, dtype=None, name=None):
"""
Return evenly spaced values within a given interval.
Values are generated within the half-open interval [start, stop) (in other words,
the interval including start but excluding stop).
Parameters:
start(float32 | float64 | int32 | int64 | Variable): Start of interval. The interval includes this value.
when start is Variable, it is a 1-D Tensor with shape [1].
end(float32 | float64 | int32 | int64 | Variable): End of interval. The interval does not include this
value, except in some cases where step is not an integer
and floating point round-off affects the length of out. When end is Variable,
it is a 1-D Tensor with shape [1].
step(float32 | float64 | int32 | int64 | Variable): Spacing between values. For any output out, this is the
distance between two adjacent values, out[i+1] - out[i].
dtype(str|core.VarDesc.VarType): the data type of the output tensor, can be float32, float64, int32, int64.
Returns: a 1-D Tensor which is evenly spaced values within a given interval. Its data type is set by dtype.
Return type: Variable
examples:
.. code-block:: python
import paddle.fluid as fluid
# expected out put: [0, 2, 4, 6, 8]
data = fluid.layers.arange(0, 10, 2, 'int32')
#dygraph mode
import paddle.fluid as fluid
with fluid.dygraph.guard():
x = fluid.layers.arange(0, 6, 2)
# x: [0, 2, 4]
# x dtype: float32
"""
helper = LayerHelper("range", **locals())
if dtype is None:
dtype = 'float32'
check_dtype(dtype, 'create data type',
['float32', 'float64', 'int32', 'int64'], 'range')
dtype = convert_dtype(dtype)
if not isinstance(start, Variable):
start = fill_constant([1], dtype, start)
if not isinstance(end, Variable):
end = fill_constant([1], dtype, end)
if not isinstance(step, Variable):
step = fill_constant([1], dtype, step)
out = helper.create_variable_for_type_inference(dtype=start.dtype)
helper.append_op(
type='range',
inputs={'Start': start,
'End': end,
'Step': step},
outputs={'Out': [out]})
out.stop_gradient = True
return out
def full(shape,
fill_value,
out=None,
dtype=None,
device=None,
stop_gradient=True,
name=None):
"""
This Op return a Tensor with the `fill_value` which size is same as `shape`
Args:
shape(list|tuple|Variable): Shape of the Tensor to be created.
The data type is ``int32`` or ``int64`` . If ``shape`` is a list or tuple,
the elements of it should be integers or Tensors with shape [1].
If ``shape`` is an Variable, it should be an 1-D Tensor .
fill_value(bool|float16|float32|float64|int32|int64|Variable): The constant value
used to initialize the Tensor to be created. If fill_value is an Variable, it must be an 1-D Tensor.
out(Variable, optional): Optional output which can be any created
Variable that meets the requirements to store the result of operation.
if out is None, a new Varibale will be create to store the result.
dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of the output tensor
which can be float16, float32, float64, int32, int64, if dytpe is `None`, the data
type of created tensor is `float32`
device(str, optional): On which device to run this Op. The :attr:`device` must be
None, 'cpu' or 'gpu'. If :attr:`device` is None, the device that the user set in
the paddle program will be chosen. Default value is None.
stop_gradient(bool, optional): Indicating if we stop gradient from current(out) Variable,
default value is True.
name(str, optional): The default value is None. Normally there is no need for user to set this
property. For more information, please refer to :ref:`api_guide_Name`.
Returns:
Variable: Tensor which is created according to shape and dtype.
Raises:
TypeError: The `dtype` must be one of None, bool, float16, float32, float64, int32 and int64.
TypeError: The `out` must be a Variable.
TypeError: The `shape` must be one of Variable, list tuple.
Examples:
.. code-block:: python
import paddle.fluid as fluid
data1 = fluid.layers.full(shape=[2,1], fill_value=0, dtype='int64') # data1=[[0],[0]]
data2 = fluid.layers.full(shape=[2,1], fill_value=5, dtype='int64', device='gpu') # data2=[[5],[5]]
# attr shape is a list which contains Variable Tensor.
positive_2 = fluid.layers.fill_constant([1], "int32", 2)
data3 = fluid.layers.full(shape=[1, positive_2], dtype='float32', fill_value=1.5) # data3=[1.5, 1.5]
# attr shape is an Variable Tensor.
shape = fluid.layers.fill_constant([1,2], "int32", 2) # shape=[2,2]
data4 = fluid.layers.full(shape=shape, dtype='bool', fill_value=True) # data4=[[True,True],[True,True]]
# attr value is an Variable Tensor.
val = fluid.layers.fill_constant([1], "float32", 2.0) # val=[2.0]
data5 = fluid.layers.full(shape=[2,1], fill_value=val, dtype='float32') #data5=[[2.0],[2.0]]
"""
helper = LayerHelper("full", **locals())
if dtype is None:
dtype = 'float32'
check_dtype(dtype, 'create data type',
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
'full')
check_type(shape, 'shape', (Variable, list, tuple), 'full')
if out is not None:
check_type(shape, 'out', (Variable), 'full')
if out is None:
out = helper.create_variable_for_type_inference(dtype=dtype)
out.stop_gradient = stop_gradient
with device_guard(device):
out = fill_constant(shape=shape, dtype=dtype, value=fill_value, out=out)
return out
def _tril_triu_op(helper):
"""Base op of tril_op and triu_op
"""
op_type = helper.layer_type
x = helper.kwargs.get('input', None)
assert x is not None, 'x cannot be None in {}'.format(op_type)
check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'],
op_type)
if len(x.shape) < 2:
raise ValueError("input shape in {} must be at least 2-D".format(
op_type))
diagonal = helper.kwargs.get('diagonal', 0)
if not isinstance(diagonal, (int, )):
raise TypeError("diagonal in {} must be a python Int".format(op_type))
name = helper.kwargs.get('name', None)
if name is None:
out = helper.create_variable_for_type_inference(dtype=x.dtype)
else:
out = helper.create_variable(
name=name, dtype=x.dtype, persistable=False)
helper.append_op(
type="tril_triu",
inputs={"X": x},
attrs={
"diagonal": diagonal,
"lower": True if op_type == 'tril' else False,
},
outputs={"Out": out}, )
return out
def tril(input, diagonal=0, name=None):
"""
This op returns the lower triangular part of a matrix (2-D tensor) or batch
of matrices :attr:`input`, the other elements of the result tensor are set
to 0. The lower triangular part of the matrix is defined as the elements
on and below the diagonal.
Args:
input (Variable): The input variable which is a Tensor.
Support data types: ``float64``, ``float32``, ``int32``, ``int64``.
diagonal (int, optional): The diagonal to consider, default value is 0.
If :attr:`diagonal` = 0, all elements on and below the main diagonal are
retained. A positive value includes just as many diagonals above the main
diagonal, and similarly a negative value excludes just as many diagonals below
the main diagonal. The main diagonal are the set of indices
:math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
:math:`d_{1}, d_{2}` are the dimensions of the matrix.
name (str, optional): The default value is None. Normally there is no need for
user to set this property. For more information, please refer to :ref:`api_guide_Name`.
Returns:
Variable: Tensor, results of lower triangular operation by the specified diagonal of input tensor,
it's data type is the same as input's Tensor.
Raises:
TypeError: diagonal is not a int type.
ValueError: dimension of :attr:`input` is less than 2.
Examples:
.. code-block:: python
import numpy as np
import paddle.fluid as fluid
data = np.arange(1, 13, dtype="int64").reshape(3,-1)
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
x = fluid.data(shape=(-1, 4), dtype='int64', name='x')
exe = fluid.Executor(fluid.CPUPlace())
# example 1, default diagonal
tril = fluid.layers.tril(x)
tril_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[tril], return_numpy=True)
# array([[ 1, 0, 0, 0],
# [ 5, 6, 0, 0],
# [ 9, 10, 11, 0]])
.. code-block:: python
# example 2, positive diagonal value
import paddle.fluid as fluid
import numpy as np
data = np.arange(1, 13, dtype="int64").reshape(3,-1)
x = fluid.data(shape=(-1, 4), dtype='int64', name='x')
exe = fluid.Executor(fluid.CPUPlace())
tril = fluid.layers.tril(x, diagonal=2)
tril_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[tril], return_numpy=True)
# array([[ 1, 2, 3, 0],
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
.. code-block:: python
# example 3, negative diagonal value
import paddle.fluid as fluid
import numpy as np
data = np.arange(1, 13, dtype="int64").reshape(3,-1)
x = fluid.data(shape=(-1, 4), dtype='int64', name='x')
exe = fluid.Executor(fluid.CPUPlace())
tril = fluid.layers.tril(x, diagonal=-1)
tril_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[tril], return_numpy=True)
# array([[ 0, 0, 0, 0],
# [ 5, 0, 0, 0],
# [ 9, 10, 0, 0]])
"""
return _tril_triu_op(LayerHelper('tril', **locals()))
def triu(input, diagonal=0, name=None):
"""
This op returns the upper triangular part of a matrix (2-D tensor) or batch of matrices
:attr:`input`, the other elements of the result tensor are set to 0.
The upper triangular part of the matrix is defined as the elements on and
above the diagonal.
Args:
input (Variable): The input variable which is a Tensor.
Support data types: ``float64``, ``float32``, ``int32``, ``int64``.
diagonal (int, optional): The diagonal to consider, default value is 0.
If :attr:`diagonal` = 0, all elements on and above the main diagonal are
retained. A positive value excludes just as many diagonals above the main
diagonal, and similarly a negative value includes just as many diagonals below
the main diagonal. The main diagonal are the set of indices
:math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
:math:`d_{1}, d_{2}` are the dimensions of the matrix.
name (str, optional): The default value is None. Normally there is no need for
user to set this property. For more information, please refer to :ref:`api_guide_Name`.
Returns:
Variable: Tensor, results of upper triangular operation by the specified diagonal of input tensor,
it's data type is the same as input's Tensor.
Raises:
TypeError: diagonal is not a int type.
ValueError: dimension of :attr:`input` is less than 2.
Examples:
.. code-block:: python
import numpy as np
import paddle.fluid as fluid
data = np.arange(1, 13, dtype="int64").reshape(3,-1)
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
x = fluid.data(shape=(-1, 4), dtype='int64', name='x')
exe = fluid.Executor(fluid.CPUPlace())
# example 1, default diagonal
import paddle.fluid as fluid
triu = fluid.layers.triu(x)
triu_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[triu], return_numpy=True)
# array([[ 1, 2, 3, 4],
# [ 0, 6, 7, 8],
# [ 0, 0, 11, 12]])
.. code-block:: python
# example 2, positive diagonal value
import paddle.fluid as fluid
import numpy as np
data = np.arange(1, 13, dtype="int64").reshape(3,-1)
x = fluid.data(shape=(-1, 4), dtype='int64', name='x')
exe = fluid.Executor(fluid.CPUPlace())
triu = fluid.layers.triu(x, diagonal=2)
triu_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[triu], return_numpy=True)
# array([[0, 0, 3, 4],
# [0, 0, 0, 8],
# [0, 0, 0, 0]])
.. code-block:: python
# example 3, negative diagonal value
import paddle.fluid as fluid
import numpy as np
data = np.arange(1, 13, dtype="int64").reshape(3,-1)
x = fluid.data(shape=(-1, 4), dtype='int64', name='x')
exe = fluid.Executor(fluid.CPUPlace())
triu = fluid.layers.triu(x, diagonal=-1)
triu_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[triu], return_numpy=True)
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 0, 10, 11, 12]])
"""
return _tril_triu_op(LayerHelper('triu', **locals()))
@templatedoc(op_type="kron") @templatedoc(op_type="kron")
def kron(x, y, out=None, name=None): def kron(x, y, out=None, name=None):
"""${comment} """${comment}
......
...@@ -23,9 +23,9 @@ class TestAllcloseLayer(unittest.TestCase): ...@@ -23,9 +23,9 @@ class TestAllcloseLayer(unittest.TestCase):
a = fluid.data(name="a", shape=[2], dtype='float32') a = fluid.data(name="a", shape=[2], dtype='float32')
b = fluid.data(name="b", shape=[2], dtype='float32') b = fluid.data(name="b", shape=[2], dtype='float32')
result = paddle.allclose( result = fluid.layers.allclose(
a, b, rtol=1e-05, atol=1e-08, equal_nan=False, name="ignore_nan") a, b, rtol=1e-05, atol=1e-08, equal_nan=False, name="ignore_nan")
result_nan = paddle.allclose( result_nan = fluid.layers.allclose(
a, b, rtol=1e-05, atol=1e-08, equal_nan=True, name="equal_nan") a, b, rtol=1e-05, atol=1e-08, equal_nan=True, name="equal_nan")
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
...@@ -82,7 +82,7 @@ class TestAllcloseLayer(unittest.TestCase): ...@@ -82,7 +82,7 @@ class TestAllcloseLayer(unittest.TestCase):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x_v_1 = fluid.dygraph.to_variable(x_1) x_v_1 = fluid.dygraph.to_variable(x_1)
y_v_1 = fluid.dygraph.to_variable(y_1) y_v_1 = fluid.dygraph.to_variable(y_1)
ret_1 = paddle.allclose( ret_1 = fluid.layers.allclose(
x_v_1, x_v_1,
y_v_1, y_v_1,
rtol=1e-05, rtol=1e-05,
...@@ -90,7 +90,7 @@ class TestAllcloseLayer(unittest.TestCase): ...@@ -90,7 +90,7 @@ class TestAllcloseLayer(unittest.TestCase):
equal_nan=False, equal_nan=False,
name='test_1') name='test_1')
self.assertEqual(ret_1.numpy()[0], False) self.assertEqual(ret_1.numpy()[0], False)
ret_1 = paddle.allclose( ret_1 = fluid.layers.allclose(
x_v_1, x_v_1,
y_v_1, y_v_1,
rtol=1e-05, rtol=1e-05,
...@@ -100,7 +100,7 @@ class TestAllcloseLayer(unittest.TestCase): ...@@ -100,7 +100,7 @@ class TestAllcloseLayer(unittest.TestCase):
self.assertEqual(ret_1.numpy()[0], False) self.assertEqual(ret_1.numpy()[0], False)
x_v_2 = fluid.dygraph.to_variable(x_2) x_v_2 = fluid.dygraph.to_variable(x_2)
y_v_2 = fluid.dygraph.to_variable(y_2) y_v_2 = fluid.dygraph.to_variable(y_2)
ret_2 = paddle.allclose( ret_2 = fluid.layers.allclose(
x_v_2, x_v_2,
y_v_2, y_v_2,
rtol=1e-05, rtol=1e-05,
...@@ -108,7 +108,7 @@ class TestAllcloseLayer(unittest.TestCase): ...@@ -108,7 +108,7 @@ class TestAllcloseLayer(unittest.TestCase):
equal_nan=False, equal_nan=False,
name='test_3') name='test_3')
self.assertEqual(ret_2.numpy()[0], True) self.assertEqual(ret_2.numpy()[0], True)
ret_2 = paddle.allclose( ret_2 = fluid.layers.allclose(
x_v_2, x_v_2,
y_v_2, y_v_2,
rtol=1e-05, rtol=1e-05,
...@@ -118,7 +118,7 @@ class TestAllcloseLayer(unittest.TestCase): ...@@ -118,7 +118,7 @@ class TestAllcloseLayer(unittest.TestCase):
self.assertEqual(ret_2.numpy()[0], True) self.assertEqual(ret_2.numpy()[0], True)
x_v_3 = fluid.dygraph.to_variable(x_3) x_v_3 = fluid.dygraph.to_variable(x_3)
y_v_3 = fluid.dygraph.to_variable(y_3) y_v_3 = fluid.dygraph.to_variable(y_3)
ret_3 = paddle.allclose( ret_3 = fluid.layers.allclose(
x_v_3, x_v_3,
y_v_3, y_v_3,
rtol=1e-05, rtol=1e-05,
...@@ -126,7 +126,7 @@ class TestAllcloseLayer(unittest.TestCase): ...@@ -126,7 +126,7 @@ class TestAllcloseLayer(unittest.TestCase):
equal_nan=False, equal_nan=False,
name='test_5') name='test_5')
self.assertEqual(ret_3.numpy()[0], False) self.assertEqual(ret_3.numpy()[0], False)
ret_3 = paddle.allclose( ret_3 = fluid.layers.allclose(
x_v_3, x_v_3,
y_v_3, y_v_3,
rtol=1e-05, rtol=1e-05,
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
from __future__ import print_function from __future__ import print_function
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import unittest import unittest
import numpy as np import numpy as np
...@@ -71,7 +70,7 @@ class TestInt32ArangeOpCase2(TestArangeOp): ...@@ -71,7 +70,7 @@ class TestInt32ArangeOpCase2(TestArangeOp):
class TestArangeAPI(unittest.TestCase): class TestArangeAPI(unittest.TestCase):
def test_out(self): def test_out(self):
with fluid.program_guard(fluid.Program()): with fluid.program_guard(fluid.Program()):
data = paddle.arange(0, 5, 1) data = fluid.layers.arange(0, 5, 1)
place = fluid.CPUPlace() place = fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
result, = exe.run(fetch_list=[data]) result, = exe.run(fetch_list=[data])
...@@ -79,7 +78,7 @@ class TestArangeAPI(unittest.TestCase): ...@@ -79,7 +78,7 @@ class TestArangeAPI(unittest.TestCase):
self.assertEqual((result == expected_data).all(), True) self.assertEqual((result == expected_data).all(), True)
with fluid.program_guard(fluid.Program()): with fluid.program_guard(fluid.Program()):
data = paddle.arange(0.0, 5.0, 1.0, 'int32') data = fluid.layers.arange(0.0, 5.0, 1.0, 'int32')
place = fluid.CPUPlace() place = fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
result, = exe.run(fetch_list=[data]) result, = exe.run(fetch_list=[data])
......
...@@ -36,7 +36,7 @@ class TestBCELoss(unittest.TestCase): ...@@ -36,7 +36,7 @@ class TestBCELoss(unittest.TestCase):
name='input', shape=[None, 30], dtype='float64') name='input', shape=[None, 30], dtype='float64')
label = fluid.data( label = fluid.data(
name='label', shape=[None, 30], dtype='float64') name='label', shape=[None, 30], dtype='float64')
bce_loss = paddle.nn.loss.BCELoss(reduction=red) bce_loss = fluid.dygraph.BCELoss(reduction=red)
res = bce_loss(input, label) res = bce_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -47,7 +47,7 @@ class TestBCELoss(unittest.TestCase): ...@@ -47,7 +47,7 @@ class TestBCELoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
bce_loss = paddle.nn.loss.BCELoss(reduction=red) bce_loss = fluid.dygraph.BCELoss(reduction=red)
dy_res = bce_loss( dy_res = bce_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -80,7 +80,7 @@ class TestBCELoss(unittest.TestCase): ...@@ -80,7 +80,7 @@ class TestBCELoss(unittest.TestCase):
name='label', shape=[None, 3, 4, 10], dtype='float64') name='label', shape=[None, 3, 4, 10], dtype='float64')
weight = fluid.data( weight = fluid.data(
name='weight', shape=[3, 4, 10], dtype='float64') name='weight', shape=[3, 4, 10], dtype='float64')
bce_loss = paddle.nn.loss.BCELoss(weight=weight) bce_loss = fluid.dygraph.BCELoss(weight=weight)
res = bce_loss(input, label) res = bce_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -93,7 +93,7 @@ class TestBCELoss(unittest.TestCase): ...@@ -93,7 +93,7 @@ class TestBCELoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
bce_loss = paddle.nn.loss.BCELoss( bce_loss = fluid.dygraph.BCELoss(
weight=fluid.dygraph.to_variable(weight_np)) weight=fluid.dygraph.to_variable(weight_np))
dy_res = bce_loss( dy_res = bce_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
......
...@@ -82,7 +82,7 @@ class API_TestElementwise_Equal(unittest.TestCase): ...@@ -82,7 +82,7 @@ class API_TestElementwise_Equal(unittest.TestCase):
with fluid.program_guard(fluid.Program(), fluid.Program()): with fluid.program_guard(fluid.Program(), fluid.Program()):
label = fluid.layers.assign(np.array([3, 3], dtype="int32")) label = fluid.layers.assign(np.array([3, 3], dtype="int32"))
limit = fluid.layers.assign(np.array([3, 2], dtype="int32")) limit = fluid.layers.assign(np.array([3, 2], dtype="int32"))
out = paddle.elementwise_equal(x=label, y=limit) out = fluid.layers.elementwise_equal(x=label, y=limit)
place = fluid.CPUPlace() place = fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
res, = exe.run(fetch_list=[out]) res, = exe.run(fetch_list=[out])
...@@ -91,7 +91,7 @@ class API_TestElementwise_Equal(unittest.TestCase): ...@@ -91,7 +91,7 @@ class API_TestElementwise_Equal(unittest.TestCase):
with fluid.program_guard(fluid.Program(), fluid.Program()): with fluid.program_guard(fluid.Program(), fluid.Program()):
label = fluid.layers.assign(np.array([3, 3], dtype="int32")) label = fluid.layers.assign(np.array([3, 3], dtype="int32"))
limit = fluid.layers.assign(np.array([3, 3], dtype="int32")) limit = fluid.layers.assign(np.array([3, 3], dtype="int32"))
out = paddle.elementwise_equal(x=label, y=limit) out = fluid.layers.elementwise_equal(x=label, y=limit)
place = fluid.CPUPlace() place = fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
res, = exe.run(fetch_list=[out]) res, = exe.run(fetch_list=[out])
......
...@@ -35,7 +35,7 @@ class CrossEntropyLoss(unittest.TestCase): ...@@ -35,7 +35,7 @@ class CrossEntropyLoss(unittest.TestCase):
label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64') label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64')
weight = fluid.layers.data( weight = fluid.layers.data(
name='weight', shape=[100], dtype='float32') name='weight', shape=[100], dtype='float32')
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(weight=weight) cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(weight=weight)
ret = cross_entropy_loss(input, label) ret = cross_entropy_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -48,7 +48,7 @@ class CrossEntropyLoss(unittest.TestCase): ...@@ -48,7 +48,7 @@ class CrossEntropyLoss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
self.assertIsNotNone(static_ret) self.assertIsNotNone(static_ret)
with fluid.dygraph.guard(): with fluid.dygraph.guard():
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
weight=fluid.dygraph.to_variable(weight_np)) weight=fluid.dygraph.to_variable(weight_np))
dy_ret = cross_entropy_loss( dy_ret = cross_entropy_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -71,7 +71,7 @@ class CrossEntropyLoss(unittest.TestCase): ...@@ -71,7 +71,7 @@ class CrossEntropyLoss(unittest.TestCase):
label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64') label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64')
weight = fluid.layers.data( weight = fluid.layers.data(
name='weight', shape=[100], dtype='float32') name='weight', shape=[100], dtype='float32')
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
weight=weight, reduction='sum') weight=weight, reduction='sum')
ret = cross_entropy_loss(input, label) ret = cross_entropy_loss(input, label)
...@@ -85,7 +85,7 @@ class CrossEntropyLoss(unittest.TestCase): ...@@ -85,7 +85,7 @@ class CrossEntropyLoss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
self.assertIsNotNone(static_ret) self.assertIsNotNone(static_ret)
with fluid.dygraph.guard(): with fluid.dygraph.guard():
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
weight=fluid.dygraph.to_variable(weight_np), reduction='sum') weight=fluid.dygraph.to_variable(weight_np), reduction='sum')
dy_ret = cross_entropy_loss( dy_ret = cross_entropy_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -108,7 +108,7 @@ class CrossEntropyLoss(unittest.TestCase): ...@@ -108,7 +108,7 @@ class CrossEntropyLoss(unittest.TestCase):
label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64') label = fluid.layers.data(name='label', shape=[5, 1], dtype='int64')
weight = fluid.layers.data( weight = fluid.layers.data(
name='weight', shape=[100], dtype='float32') name='weight', shape=[100], dtype='float32')
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
weight=weight, reduction='none') weight=weight, reduction='none')
ret = cross_entropy_loss(input, label) ret = cross_entropy_loss(input, label)
...@@ -122,7 +122,7 @@ class CrossEntropyLoss(unittest.TestCase): ...@@ -122,7 +122,7 @@ class CrossEntropyLoss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
self.assertIsNotNone(static_ret) self.assertIsNotNone(static_ret)
with fluid.dygraph.guard(): with fluid.dygraph.guard():
cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( cross_entropy_loss = fluid.dygraph.CrossEntropyLoss(
weight=fluid.dygraph.to_variable(weight_np), reduction='none') weight=fluid.dygraph.to_variable(weight_np), reduction='none')
dy_ret = cross_entropy_loss( dy_ret = cross_entropy_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
......
...@@ -106,7 +106,7 @@ class TestFillAnyLikeOp_attr_out(unittest.TestCase): ...@@ -106,7 +106,7 @@ class TestFillAnyLikeOp_attr_out(unittest.TestCase):
with fluid.program_guard(train_program, startup_program): with fluid.program_guard(train_program, startup_program):
fill_value = 2.0 fill_value = 2.0
input = fluid.data(name='input', dtype='float32', shape=[2, 3]) input = fluid.data(name='input', dtype='float32', shape=[2, 3])
output = paddle.full_like(input, fill_value) output = fluid.layers.full_like(input, fill_value)
place = fluid.CPUPlace() place = fluid.CPUPlace()
if fluid.core.is_compiled_with_cuda(): if fluid.core.is_compiled_with_cuda():
...@@ -132,20 +132,20 @@ class TestFillAnyLikeOpError(unittest.TestCase): ...@@ -132,20 +132,20 @@ class TestFillAnyLikeOpError(unittest.TestCase):
#for ci coverage #for ci coverage
input_data = fluid.data(name='input', dtype='float32', shape=[2, 3]) input_data = fluid.data(name='input', dtype='float32', shape=[2, 3])
output = paddle.full_like(input_data, 2.0) output = fluid.layers.full_like(input_data, 2.0)
def test_input_dtype(): def test_input_dtype():
paddle.full_like fluid.layers.full_like
self.assertRaises( self.assertRaises(
ValueError, ValueError,
paddle.full_like, fluid.layers.full_like,
input=input_data, input=input_data,
fill_value=2, fill_value=2,
dtype='uint4') dtype='uint4')
self.assertRaises( self.assertRaises(
TypeError, TypeError,
paddle.full_like, fluid.layers.full_like,
input=input_data, input=input_data,
fill_value=2, fill_value=2,
dtype='int16') dtype='int16')
......
...@@ -16,7 +16,6 @@ from __future__ import print_function ...@@ -16,7 +16,6 @@ from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid import Program, program_guard from paddle.fluid import Program, program_guard
...@@ -32,7 +31,7 @@ class TestFlipOp_API(unittest.TestCase): ...@@ -32,7 +31,7 @@ class TestFlipOp_API(unittest.TestCase):
with fluid.program_guard(train_program, startup_program): with fluid.program_guard(train_program, startup_program):
dims = [0] dims = [0]
input = fluid.data(name='input', dtype='float32', shape=[2, 3]) input = fluid.data(name='input', dtype='float32', shape=[2, 3])
output = paddle.flip(input, dims) output = fluid.layers.flip(input, dims)
place = fluid.CPUPlace() place = fluid.CPUPlace()
if fluid.core.is_compiled_with_cuda(): if fluid.core.is_compiled_with_cuda():
place = fluid.CUDAPlace(0) place = fluid.CUDAPlace(0)
...@@ -52,7 +51,7 @@ class TestFlipOp_API(unittest.TestCase): ...@@ -52,7 +51,7 @@ class TestFlipOp_API(unittest.TestCase):
img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
with fluid.dygraph.guard(): with fluid.dygraph.guard():
inputs = fluid.dygraph.to_variable(img) inputs = fluid.dygraph.to_variable(img)
ret = paddle.flip(inputs, [0]) ret = fluid.layers.flip(inputs, [0])
out_ref = np.array([[4, 5, 6], [1, 2, 3]]).astype(np.float32) out_ref = np.array([[4, 5, 6], [1, 2, 3]]).astype(np.float32)
self.assertTrue( self.assertTrue(
(ret.numpy() == out_ref).all(), (ret.numpy() == out_ref).all(),
......
...@@ -21,7 +21,6 @@ from op_test import OpTest ...@@ -21,7 +21,6 @@ from op_test import OpTest
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid.op import Operator from paddle.fluid.op import Operator
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle
from paddle.fluid import compiler, Program, program_guard from paddle.fluid import compiler, Program, program_guard
...@@ -37,39 +36,39 @@ class TestFullAPI(unittest.TestCase): ...@@ -37,39 +36,39 @@ class TestFullAPI(unittest.TestCase):
shape_tensor_int64 = fluid.data( shape_tensor_int64 = fluid.data(
name="shape_tensor_int64", shape=[2], dtype="int64") name="shape_tensor_int64", shape=[2], dtype="int64")
out_1 = paddle.full( out_1 = fluid.layers.full(
shape=[1, 2], dtype="float32", fill_value=1.1, device='gpu') shape=[1, 2], dtype="float32", fill_value=1.1, device='gpu')
out_2 = paddle.full( out_2 = fluid.layers.full(
shape=[1, positive_2_int32], shape=[1, positive_2_int32],
dtype="float32", dtype="float32",
fill_value=1.1, fill_value=1.1,
device='cpu') device='cpu')
out_3 = paddle.full( out_3 = fluid.layers.full(
shape=[1, positive_2_int64], shape=[1, positive_2_int64],
dtype="float32", dtype="float32",
fill_value=1.1, fill_value=1.1,
device='gpu') device='gpu')
out_4 = paddle.full( out_4 = fluid.layers.full(
shape=shape_tensor_int32, shape=shape_tensor_int32,
dtype="float32", dtype="float32",
fill_value=1.2, fill_value=1.2,
out=out_3) out=out_3)
out_5 = paddle.full( out_5 = fluid.layers.full(
shape=shape_tensor_int64, shape=shape_tensor_int64,
dtype="float32", dtype="float32",
fill_value=1.1, fill_value=1.1,
device='gpu', device='gpu',
stop_gradient=False) stop_gradient=False)
out_6 = paddle.full( out_6 = fluid.layers.full(
shape=shape_tensor_int64, dtype=np.float32, fill_value=1.1) shape=shape_tensor_int64, dtype=np.float32, fill_value=1.1)
val = fluid.layers.fill_constant(shape=[1], dtype=np.float32, value=1.1) val = fluid.layers.fill_constant(shape=[1], dtype=np.float32, value=1.1)
out_7 = paddle.full( out_7 = fluid.layers.full(
shape=shape_tensor_int64, dtype=np.float32, fill_value=val) shape=shape_tensor_int64, dtype=np.float32, fill_value=val)
exe = fluid.Executor(place=fluid.CPUPlace()) exe = fluid.Executor(place=fluid.CPUPlace())
...@@ -97,17 +96,21 @@ class TestFullOpError(unittest.TestCase): ...@@ -97,17 +96,21 @@ class TestFullOpError(unittest.TestCase):
x1 = fluid.layers.data(name='x1', shape=[1], dtype="int16") x1 = fluid.layers.data(name='x1', shape=[1], dtype="int16")
x2 = np.random.randn(1, 2).astype('int32') x2 = np.random.randn(1, 2).astype('int32')
self.assertRaises( self.assertRaises(
ValueError, paddle.full, shape=[1], fill_value=5, dtype='uint4') ValueError,
fluid.layers.full,
shape=[1],
fill_value=5,
dtype='uint4')
self.assertRaises( self.assertRaises(
TypeError, TypeError,
paddle.full, fluid.layers.full,
shape=[1], shape=[1],
fill_value=5, fill_value=5,
dtype='int32', dtype='int32',
out=x2) out=x2)
self.assertRaises( self.assertRaises(
TypeError, TypeError,
paddle.full, fluid.layers.full,
shape=[1], shape=[1],
fill_value=5, fill_value=5,
dtype='int16', dtype='int16',
...@@ -118,17 +121,21 @@ class TestFullOpError(unittest.TestCase): ...@@ -118,17 +121,21 @@ class TestFullOpError(unittest.TestCase):
x2 = fluid.layers.data(name='x2', shape=[1], dtype="int32") x2 = fluid.layers.data(name='x2', shape=[1], dtype="int32")
self.assertRaises( self.assertRaises(
TypeError, paddle.full, shape=[1], fill_value=5, dtype='uint8') TypeError,
fluid.layers.full,
shape=[1],
fill_value=5,
dtype='uint8')
# The argument shape's type of full_op must be list, tuple or Variable. # The argument shape's type of full_op must be list, tuple or Variable.
def test_shape_type(): def test_shape_type():
paddle.full(shape=1, dtype="float32", fill_value=1) fluid.layers.full(shape=1, dtype="float32", fill_value=1)
self.assertRaises(TypeError, test_shape_type) self.assertRaises(TypeError, test_shape_type)
# The argument shape's size of full_op must not be 0. # The argument shape's size of full_op must not be 0.
def test_shape_size(): def test_shape_size():
paddle.full(shape=[], dtype="float32", fill_value=1) fluid.layers.full(shape=[], dtype="float32", fill_value=1)
self.assertRaises(AssertionError, test_shape_size) self.assertRaises(AssertionError, test_shape_size)
...@@ -136,14 +143,15 @@ class TestFullOpError(unittest.TestCase): ...@@ -136,14 +143,15 @@ class TestFullOpError(unittest.TestCase):
def test_shape_tensor_dtype(): def test_shape_tensor_dtype():
shape = fluid.data( shape = fluid.data(
name="shape_tensor", shape=[2], dtype="float32") name="shape_tensor", shape=[2], dtype="float32")
paddle.full(shape=shape, dtype="float32", fill_value=1) fluid.layers.full(shape=shape, dtype="float32", fill_value=1)
self.assertRaises(TypeError, test_shape_tensor_dtype) self.assertRaises(TypeError, test_shape_tensor_dtype)
def test_shape_tensor_list_dtype(): def test_shape_tensor_list_dtype():
shape = fluid.data( shape = fluid.data(
name="shape_tensor_list", shape=[1], dtype="bool") name="shape_tensor_list", shape=[1], dtype="bool")
paddle.full(shape=[shape, 2], dtype="float32", fill_value=1) fluid.layers.full(
shape=[shape, 2], dtype="float32", fill_value=1)
self.assertRaises(TypeError, test_shape_tensor_list_dtype) self.assertRaises(TypeError, test_shape_tensor_list_dtype)
......
...@@ -33,7 +33,7 @@ class TestL1Loss(unittest.TestCase): ...@@ -33,7 +33,7 @@ class TestL1Loss(unittest.TestCase):
name='input', shape=[10, 1], dtype='float32') name='input', shape=[10, 1], dtype='float32')
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=[10, 1], dtype='float32') name='label', shape=[10, 1], dtype='float32')
l1_loss = paddle.nn.loss.L1Loss() l1_loss = fluid.dygraph.L1Loss()
ret = l1_loss(input, label) ret = l1_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -44,7 +44,7 @@ class TestL1Loss(unittest.TestCase): ...@@ -44,7 +44,7 @@ class TestL1Loss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
l1_loss = paddle.nn.loss.L1Loss() l1_loss = fluid.dygraph.L1Loss()
dy_ret = l1_loss( dy_ret = l1_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -68,7 +68,7 @@ class TestL1Loss(unittest.TestCase): ...@@ -68,7 +68,7 @@ class TestL1Loss(unittest.TestCase):
name='input', shape=[10, 10, 5], dtype='float32') name='input', shape=[10, 10, 5], dtype='float32')
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=[10, 10, 5], dtype='float32') name='label', shape=[10, 10, 5], dtype='float32')
l1_loss = paddle.nn.loss.L1Loss(reduction='sum') l1_loss = fluid.dygraph.L1Loss(reduction='sum')
ret = l1_loss(input, label) ret = l1_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -79,7 +79,7 @@ class TestL1Loss(unittest.TestCase): ...@@ -79,7 +79,7 @@ class TestL1Loss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
l1_loss = paddle.nn.loss.L1Loss(reduction='sum') l1_loss = fluid.dygraph.L1Loss(reduction='sum')
dy_ret = l1_loss( dy_ret = l1_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -103,7 +103,7 @@ class TestL1Loss(unittest.TestCase): ...@@ -103,7 +103,7 @@ class TestL1Loss(unittest.TestCase):
name='input', shape=[10, 5], dtype='float32') name='input', shape=[10, 5], dtype='float32')
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=[10, 5], dtype='float32') name='label', shape=[10, 5], dtype='float32')
l1_loss = paddle.nn.loss.L1Loss(reduction='none') l1_loss = fluid.dygraph.L1Loss(reduction='none')
ret = l1_loss(input, label) ret = l1_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -114,7 +114,7 @@ class TestL1Loss(unittest.TestCase): ...@@ -114,7 +114,7 @@ class TestL1Loss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
l1_loss = paddle.nn.loss.L1Loss(reduction='none') l1_loss = fluid.dygraph.L1Loss(reduction='none')
dy_ret = l1_loss( dy_ret = l1_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
......
...@@ -17,7 +17,6 @@ import numpy as np ...@@ -17,7 +17,6 @@ import numpy as np
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.nn as nn import paddle.nn as nn
import paddle.nn.functional as functional
def stable_softmax(x): def stable_softmax(x):
...@@ -84,14 +83,14 @@ class TestNNFunctionalLogSoftmaxAPI(unittest.TestCase): ...@@ -84,14 +83,14 @@ class TestNNFunctionalLogSoftmaxAPI(unittest.TestCase):
mylogsoftmax = nn.LogSoftmax(axis) mylogsoftmax = nn.LogSoftmax(axis)
with fluid.program_guard(main_program): with fluid.program_guard(main_program):
x = fluid.data(name='x', shape=self.x_shape) x = fluid.data(name='x', shape=self.x_shape)
y = functional.log_softmax(x, axis, dtype) y = fluid.layers.log_softmax(x, axis, dtype)
exe = fluid.Executor(place) exe = fluid.Executor(place)
out = exe.run(main_program, feed={'x': self.x}, fetch_list=[y]) out = exe.run(main_program, feed={'x': self.x}, fetch_list=[y])
self.assertTrue(np.allclose(out[0], ref_out)) self.assertTrue(np.allclose(out[0], ref_out))
with fluid.dygraph.guard(place): with fluid.dygraph.guard(place):
x = fluid.dygraph.to_variable(self.x) x = fluid.dygraph.to_variable(self.x)
y = functional.log_softmax(x, axis, dtype) y = fluid.layers.log_softmax(x, axis, dtype)
self.assertTrue(np.allclose(y.numpy(), ref_out)) self.assertTrue(np.allclose(y.numpy(), ref_out))
def test_check_api(self): def test_check_api(self):
......
...@@ -18,8 +18,8 @@ import unittest ...@@ -18,8 +18,8 @@ import unittest
import numpy as np import numpy as np
from op_test import OpTest, skip_check_grad_ci from op_test import OpTest, skip_check_grad_ci
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle
from paddle.fluid import compiler, Program, program_guard, core from paddle.fluid import compiler, Program, program_guard, core
import paddle
class TestMeshgridOp(OpTest): class TestMeshgridOp(OpTest):
...@@ -79,7 +79,7 @@ class TestMeshgridOp3(unittest.TestCase): ...@@ -79,7 +79,7 @@ class TestMeshgridOp3(unittest.TestCase):
out_2 = np.broadcast_to(out_2, [100, 200]) out_2 = np.broadcast_to(out_2, [100, 200])
exe = fluid.Executor(place=fluid.CPUPlace()) exe = fluid.Executor(place=fluid.CPUPlace())
grid_x, grid_y = paddle.tensor.meshgrid([x, y]) grid_x, grid_y = paddle.meshgrid([x, y])
res_1, res_2 = exe.run(fluid.default_main_program(), res_1, res_2 = exe.run(fluid.default_main_program(),
feed={'x': input_1, feed={'x': input_1,
'y': input_2}, 'y': input_2},
...@@ -95,7 +95,7 @@ class TestMeshgridOp4(unittest.TestCase): ...@@ -95,7 +95,7 @@ class TestMeshgridOp4(unittest.TestCase):
def test_input_type(): def test_input_type():
x = fluid.data(shape=[200], dtype='float32', name='x2') x = fluid.data(shape=[200], dtype='float32', name='x2')
paddle.tensor.meshgrid(x) paddle.meshgrid(x)
self.assertRaises(TypeError, test_input_type) self.assertRaises(TypeError, test_input_type)
...@@ -108,7 +108,7 @@ class TestMeshgridOp5(unittest.TestCase): ...@@ -108,7 +108,7 @@ class TestMeshgridOp5(unittest.TestCase):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
tensor_3 = fluid.dygraph.to_variable(input_3) tensor_3 = fluid.dygraph.to_variable(input_3)
tensor_4 = fluid.dygraph.to_variable(input_4) tensor_4 = fluid.dygraph.to_variable(input_4)
res_3, res_4 = paddle.tensor.meshgrid([tensor_3, tensor_4]) res_3, res_4 = paddle.meshgrid([tensor_3, tensor_4])
assert np.array_equal(res_3.shape, [100, 200]) assert np.array_equal(res_3.shape, [100, 200])
assert np.array_equal(res_4.shape, [100, 200]) assert np.array_equal(res_4.shape, [100, 200])
......
...@@ -78,7 +78,7 @@ class TestNNMseLoss(unittest.TestCase): ...@@ -78,7 +78,7 @@ class TestNNMseLoss(unittest.TestCase):
name='input', shape=dim, dtype='float32') name='input', shape=dim, dtype='float32')
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=dim, dtype='float32') name='label', shape=dim, dtype='float32')
mse_loss = paddle.nn.loss.MSELoss() mse_loss = fluid.dygraph.MSELoss()
ret = mse_loss(input, label) ret = mse_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -89,7 +89,7 @@ class TestNNMseLoss(unittest.TestCase): ...@@ -89,7 +89,7 @@ class TestNNMseLoss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
mse_loss = paddle.nn.loss.MSELoss() mse_loss = fluid.dygraph.MSELoss()
dy_ret = mse_loss( dy_ret = mse_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -115,7 +115,7 @@ class TestNNMseLoss(unittest.TestCase): ...@@ -115,7 +115,7 @@ class TestNNMseLoss(unittest.TestCase):
name='input', shape=dim, dtype='float32') name='input', shape=dim, dtype='float32')
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=dim, dtype='float32') name='label', shape=dim, dtype='float32')
mse_loss = paddle.nn.loss.MSELoss(reduction='sum') mse_loss = fluid.dygraph.MSELoss(reduction='sum')
ret = mse_loss(input, label) ret = mse_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -126,7 +126,7 @@ class TestNNMseLoss(unittest.TestCase): ...@@ -126,7 +126,7 @@ class TestNNMseLoss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
mse_loss = paddle.nn.loss.MSELoss(reduction='sum') mse_loss = fluid.dygraph.MSELoss(reduction='sum')
dy_ret = mse_loss( dy_ret = mse_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -152,7 +152,7 @@ class TestNNMseLoss(unittest.TestCase): ...@@ -152,7 +152,7 @@ class TestNNMseLoss(unittest.TestCase):
name='input', shape=dim, dtype='float32') name='input', shape=dim, dtype='float32')
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=dim, dtype='float32') name='label', shape=dim, dtype='float32')
mse_loss = paddle.nn.loss.MSELoss(reduction='none') mse_loss = fluid.dygraph.MSELoss(reduction='none')
ret = mse_loss(input, label) ret = mse_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -163,7 +163,7 @@ class TestNNMseLoss(unittest.TestCase): ...@@ -163,7 +163,7 @@ class TestNNMseLoss(unittest.TestCase):
fetch_list=[ret]) fetch_list=[ret])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
mse_loss = paddle.nn.loss.MSELoss(reduction='none') mse_loss = fluid.dygraph.MSELoss(reduction='none')
dy_ret = mse_loss( dy_ret = mse_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
......
...@@ -82,7 +82,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -82,7 +82,7 @@ class TestNLLLoss(unittest.TestCase):
with fluid.program_guard(prog, startup_prog): with fluid.program_guard(prog, startup_prog):
input = fluid.data(name='input', shape=[10, 10], dtype='float64') input = fluid.data(name='input', shape=[10, 10], dtype='float64')
label = fluid.data(name='label', shape=[10], dtype='int64') label = fluid.data(name='label', shape=[10], dtype='int64')
nll_loss = paddle.nn.loss.NLLLoss() nll_loss = fluid.dygraph.NLLLoss()
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -93,7 +93,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -93,7 +93,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss() nll_loss = fluid.dygraph.NLLLoss()
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -115,7 +115,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -115,7 +115,7 @@ class TestNLLLoss(unittest.TestCase):
with fluid.program_guard(prog, startup_prog): with fluid.program_guard(prog, startup_prog):
input = fluid.data(name='input', shape=[10, 10], dtype='float64') input = fluid.data(name='input', shape=[10, 10], dtype='float64')
label = fluid.data(name='label', shape=[10], dtype='int64') label = fluid.data(name='label', shape=[10], dtype='int64')
nll_loss = paddle.nn.loss.NLLLoss(reduction='sum') nll_loss = fluid.dygraph.NLLLoss(reduction='sum')
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -126,7 +126,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -126,7 +126,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss(reduction='sum') nll_loss = fluid.dygraph.NLLLoss(reduction='sum')
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -150,7 +150,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -150,7 +150,7 @@ class TestNLLLoss(unittest.TestCase):
input = fluid.data(name='input', shape=[10, 10], dtype='float64') input = fluid.data(name='input', shape=[10, 10], dtype='float64')
label = fluid.data(name='label', shape=[10], dtype='int64') label = fluid.data(name='label', shape=[10], dtype='int64')
weight = fluid.data(name='weight', shape=[10], dtype='float64') weight = fluid.data(name='weight', shape=[10], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight) nll_loss = fluid.dygraph.NLLLoss(weight=weight)
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -163,7 +163,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -163,7 +163,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np)) weight=fluid.dygraph.to_variable(weight_np))
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -188,7 +188,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -188,7 +188,7 @@ class TestNLLLoss(unittest.TestCase):
input = fluid.data(name='input', shape=[10, 10], dtype='float64') input = fluid.data(name='input', shape=[10, 10], dtype='float64')
label = fluid.data(name='label', shape=[10], dtype='int64') label = fluid.data(name='label', shape=[10], dtype='int64')
weight = fluid.data(name='weight', shape=[10], dtype='float64') weight = fluid.data(name='weight', shape=[10], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='sum') nll_loss = fluid.dygraph.NLLLoss(weight=weight, reduction='sum')
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -201,7 +201,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -201,7 +201,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np), reduction='sum') weight=fluid.dygraph.to_variable(weight_np), reduction='sum')
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -225,7 +225,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -225,7 +225,7 @@ class TestNLLLoss(unittest.TestCase):
input = fluid.data(name='input', shape=[10, 10], dtype='float64') input = fluid.data(name='input', shape=[10, 10], dtype='float64')
label = fluid.data(name='label', shape=[10], dtype='int64') label = fluid.data(name='label', shape=[10], dtype='int64')
weight = fluid.data(name='weight', shape=[10], dtype='float64') weight = fluid.data(name='weight', shape=[10], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight) nll_loss = fluid.dygraph.NLLLoss(weight=weight)
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -238,7 +238,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -238,7 +238,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np)) weight=fluid.dygraph.to_variable(weight_np))
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -261,7 +261,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -261,7 +261,7 @@ class TestNLLLoss(unittest.TestCase):
input = fluid.data(name='input', shape=[10, 10], dtype='float64') input = fluid.data(name='input', shape=[10, 10], dtype='float64')
label = fluid.data(name='label', shape=[10], dtype='int64') label = fluid.data(name='label', shape=[10], dtype='int64')
weight = fluid.data(name='weight', shape=[10], dtype='float64') weight = fluid.data(name='weight', shape=[10], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='none') nll_loss = fluid.dygraph.NLLLoss(weight=weight, reduction='none')
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -274,7 +274,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -274,7 +274,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np), reduction='none') weight=fluid.dygraph.to_variable(weight_np), reduction='none')
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -299,7 +299,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -299,7 +299,7 @@ class TestNLLLoss(unittest.TestCase):
input = fluid.data( input = fluid.data(
name='input', shape=[5, 3, 5, 5], dtype='float64') name='input', shape=[5, 3, 5, 5], dtype='float64')
label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64')
nll_loss = paddle.nn.loss.NLLLoss() nll_loss = fluid.dygraph.NLLLoss()
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -310,7 +310,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -310,7 +310,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss() nll_loss = fluid.dygraph.NLLLoss()
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -334,7 +334,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -334,7 +334,7 @@ class TestNLLLoss(unittest.TestCase):
input = fluid.data( input = fluid.data(
name='input', shape=[5, 3, 5, 5], dtype='float64') name='input', shape=[5, 3, 5, 5], dtype='float64')
label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64')
nll_loss = paddle.nn.loss.NLLLoss(reduction='sum') nll_loss = fluid.dygraph.NLLLoss(reduction='sum')
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -345,7 +345,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -345,7 +345,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss(reduction='sum') nll_loss = fluid.dygraph.NLLLoss(reduction='sum')
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -372,7 +372,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -372,7 +372,7 @@ class TestNLLLoss(unittest.TestCase):
label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64')
weight = fluid.data(name='weight', shape=[3], dtype='float64') weight = fluid.data(name='weight', shape=[3], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight) nll_loss = fluid.dygraph.NLLLoss(weight=weight)
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -385,7 +385,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -385,7 +385,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np)) weight=fluid.dygraph.to_variable(weight_np))
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -411,7 +411,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -411,7 +411,7 @@ class TestNLLLoss(unittest.TestCase):
label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64')
weight = fluid.data(name='weight', shape=[3], dtype='float64') weight = fluid.data(name='weight', shape=[3], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight) nll_loss = fluid.dygraph.NLLLoss(weight=weight)
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -424,7 +424,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -424,7 +424,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np)) weight=fluid.dygraph.to_variable(weight_np))
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -452,7 +452,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -452,7 +452,7 @@ class TestNLLLoss(unittest.TestCase):
label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5], dtype='int64')
weight = fluid.data(name='weight', shape=[3], dtype='float64') weight = fluid.data(name='weight', shape=[3], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='sum') nll_loss = fluid.dygraph.NLLLoss(weight=weight, reduction='sum')
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -465,7 +465,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -465,7 +465,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np), reduction='sum') weight=fluid.dygraph.to_variable(weight_np), reduction='sum')
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -491,7 +491,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -491,7 +491,7 @@ class TestNLLLoss(unittest.TestCase):
input = fluid.data( input = fluid.data(
name='input', shape=[5, 3, 5, 5, 5], dtype='float64') name='input', shape=[5, 3, 5, 5, 5], dtype='float64')
label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64')
nll_loss = paddle.nn.loss.NLLLoss() nll_loss = fluid.dygraph.NLLLoss()
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -502,7 +502,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -502,7 +502,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss() nll_loss = fluid.dygraph.NLLLoss()
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
fluid.dygraph.to_variable(label_np)) fluid.dygraph.to_variable(label_np))
...@@ -533,7 +533,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -533,7 +533,7 @@ class TestNLLLoss(unittest.TestCase):
name='input', shape=[5, 3, 5, 5, 5], dtype='float64') name='input', shape=[5, 3, 5, 5, 5], dtype='float64')
label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64')
weight = fluid.data(name='weight', shape=[3], dtype='float64') weight = fluid.data(name='weight', shape=[3], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight) nll_loss = fluid.dygraph.NLLLoss(weight=weight)
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -546,7 +546,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -546,7 +546,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np)) weight=fluid.dygraph.to_variable(weight_np))
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -579,7 +579,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -579,7 +579,7 @@ class TestNLLLoss(unittest.TestCase):
name='input', shape=[5, 3, 5, 5, 5], dtype='float64') name='input', shape=[5, 3, 5, 5, 5], dtype='float64')
label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64')
weight = fluid.data(name='weight', shape=[3], dtype='float64') weight = fluid.data(name='weight', shape=[3], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='sum') nll_loss = fluid.dygraph.NLLLoss(weight=weight, reduction='sum')
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -592,7 +592,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -592,7 +592,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np), reduction='sum') weight=fluid.dygraph.to_variable(weight_np), reduction='sum')
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -628,7 +628,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -628,7 +628,7 @@ class TestNLLLoss(unittest.TestCase):
name='input', shape=[5, 3, 5, 5, 5], dtype='float64') name='input', shape=[5, 3, 5, 5, 5], dtype='float64')
label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64')
weight = fluid.data(name='weight', shape=[3], dtype='float64') weight = fluid.data(name='weight', shape=[3], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='none') nll_loss = fluid.dygraph.NLLLoss(weight=weight, reduction='none')
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -641,7 +641,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -641,7 +641,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np), reduction='none') weight=fluid.dygraph.to_variable(weight_np), reduction='none')
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
...@@ -676,7 +676,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -676,7 +676,7 @@ class TestNLLLoss(unittest.TestCase):
name='input', shape=[5, 3, 5, 5, 5], dtype='float64') name='input', shape=[5, 3, 5, 5, 5], dtype='float64')
label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64') label = fluid.data(name='label', shape=[5, 5, 5, 5], dtype='int64')
weight = fluid.data(name='weight', shape=[3], dtype='float64') weight = fluid.data(name='weight', shape=[3], dtype='float64')
nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='none') nll_loss = fluid.dygraph.NLLLoss(weight=weight, reduction='none')
res = nll_loss(input, label) res = nll_loss(input, label)
exe = fluid.Executor(place) exe = fluid.Executor(place)
...@@ -689,7 +689,7 @@ class TestNLLLoss(unittest.TestCase): ...@@ -689,7 +689,7 @@ class TestNLLLoss(unittest.TestCase):
fetch_list=[res]) fetch_list=[res])
with fluid.dygraph.guard(): with fluid.dygraph.guard():
nll_loss = paddle.nn.loss.NLLLoss( nll_loss = fluid.dygraph.NLLLoss(
weight=fluid.dygraph.to_variable(weight_np), reduction='none') weight=fluid.dygraph.to_variable(weight_np), reduction='none')
dy_res = nll_loss( dy_res = nll_loss(
fluid.dygraph.to_variable(input_np), fluid.dygraph.to_variable(input_np),
......
...@@ -22,7 +22,6 @@ import paddle.fluid.core as core ...@@ -22,7 +22,6 @@ import paddle.fluid.core as core
from paddle.fluid.op import Operator from paddle.fluid.op import Operator
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid import Program, program_guard from paddle.fluid import Program, program_guard
import paddle
def output_hist(out): def output_hist(out):
...@@ -62,17 +61,18 @@ class TestRandintOpError(unittest.TestCase): ...@@ -62,17 +61,18 @@ class TestRandintOpError(unittest.TestCase):
def test_shape(): def test_shape():
shape = np.array([2, 3]) shape = np.array([2, 3])
paddle.randint(5, shape=shape, dtype='int32') fluid.layers.randint(5, shape=shape, dtype='int32')
self.assertRaises(TypeError, test_shape) self.assertRaises(TypeError, test_shape)
def test_dtype(): def test_dtype():
paddle.randint(5, shape=[32, 32], dtype='float32') fluid.layers.randint(5, shape=[32, 32], dtype='float32')
self.assertRaises(TypeError, test_dtype) self.assertRaises(TypeError, test_dtype)
def test_low_high(): def test_low_high():
paddle.randint(low=5, high=5, shape=[32, 32], dtype='int32') fluid.layers.randint(
low=5, high=5, shape=[32, 32], dtype='int32')
self.assertRaises(ValueError, test_low_high) self.assertRaises(ValueError, test_low_high)
...@@ -131,21 +131,21 @@ class TestRandintAPI(unittest.TestCase): ...@@ -131,21 +131,21 @@ class TestRandintAPI(unittest.TestCase):
train_program = fluid.Program() train_program = fluid.Program()
with fluid.program_guard(train_program, startup_program): with fluid.program_guard(train_program, startup_program):
# results are from [0, 5). # results are from [0, 5).
output1 = paddle.randint(5) output1 = fluid.layers.randint(5)
# shape is a list and dtype is 'int32' # shape is a list and dtype is 'int32'
output2 = paddle.randint( output2 = fluid.layers.randint(
low=-100, high=100, shape=[64, 64], dtype='int32') low=-100, high=100, shape=[64, 64], dtype='int32')
# shape is a tuple and dtype is 'int64' # shape is a tuple and dtype is 'int64'
output3 = paddle.randint( output3 = fluid.layers.randint(
low=-100, high=100, shape=(32, 32, 3), dtype='int64') low=-100, high=100, shape=(32, 32, 3), dtype='int64')
# shape is a tensorlist and dtype is 'float32' # shape is a tensorlist and dtype is 'float32'
dim_1 = fluid.layers.fill_constant([1], "int64", 32) dim_1 = fluid.layers.fill_constant([1], "int64", 32)
dim_2 = fluid.layers.fill_constant([1], "int32", 50) dim_2 = fluid.layers.fill_constant([1], "int32", 50)
output4 = paddle.randint( output4 = fluid.layers.randint(
low=-100, high=100, shape=[dim_1, 5], dtype='int32') low=-100, high=100, shape=[dim_1, 5], dtype='int32')
# shape is a tensor and dtype is 'float64' # shape is a tensor and dtype is 'float64'
var_shape = fluid.data(name='var_shape', shape=[2], dtype="int64") var_shape = fluid.data(name='var_shape', shape=[2], dtype="int64")
output5 = paddle.randint( output5 = fluid.layers.randint(
low=1, high=1000, shape=var_shape, dtype='int64') low=1, high=1000, shape=var_shape, dtype='int64')
place = fluid.CPUPlace() place = fluid.CPUPlace()
...@@ -163,7 +163,7 @@ class TestRandintAPI(unittest.TestCase): ...@@ -163,7 +163,7 @@ class TestRandintAPI(unittest.TestCase):
class TestRandintDygraphMode(unittest.TestCase): class TestRandintDygraphMode(unittest.TestCase):
def test_check_output(self): def test_check_output(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = paddle.randint(10, shape=[10], dtype="int32") x = fluid.layers.randint(10, shape=[10], dtype="int32")
x_np = x.numpy() x_np = x.numpy()
for i in range(10): for i in range(10):
self.assertTrue((x_np[i] >= 0 and x_np[i] < 10)) self.assertTrue((x_np[i] >= 0 and x_np[i] < 10))
......
...@@ -16,7 +16,6 @@ from __future__ import print_function ...@@ -16,7 +16,6 @@ from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid import Program, program_guard from paddle.fluid import Program, program_guard
...@@ -24,14 +23,16 @@ from paddle.fluid import Program, program_guard ...@@ -24,14 +23,16 @@ from paddle.fluid import Program, program_guard
class TestRandnOp(unittest.TestCase): class TestRandnOp(unittest.TestCase):
def test_api(self): def test_api(self):
x1 = paddle.randn(shape=[1000, 784], dtype='float32') x1 = fluid.layers.randn(shape=[1000, 784], dtype='float32')
x2 = paddle.randn(shape=[1000, 784], dtype='float64') x2 = fluid.layers.randn(shape=[1000, 784], dtype='float64')
x3 = fluid.layers.fill_constant( x3 = fluid.layers.fill_constant(
shape=[1000, 784], dtype='float32', value=0) shape=[1000, 784], dtype='float32', value=0)
paddle.randn(shape=[1000, 784], out=x3, dtype='float32') fluid.layers.randn(shape=[1000, 784], out=x3, dtype='float32')
x4 = paddle.randn(shape=[1000, 784], dtype='float32', device='cpu') x4 = fluid.layers.randn(
x5 = paddle.randn(shape=[1000, 784], dtype='float32', device='gpu') shape=[1000, 784], dtype='float32', device='cpu')
x6 = paddle.randn( x5 = fluid.layers.randn(
shape=[1000, 784], dtype='float32', device='gpu')
x6 = fluid.layers.randn(
shape=[1000, 784], shape=[1000, 784],
dtype='float32', dtype='float32',
device='gpu', device='gpu',
...@@ -64,43 +65,43 @@ class TestRandnOpError(unittest.TestCase): ...@@ -64,43 +65,43 @@ class TestRandnOpError(unittest.TestCase):
# The argument shape's size of randn_op should not be 0. # The argument shape's size of randn_op should not be 0.
def test_shape_size(): def test_shape_size():
out = paddle.randn(shape=[]) out = fluid.layers.randn(shape=[])
self.assertRaises(AssertionError, test_shape_size) self.assertRaises(AssertionError, test_shape_size)
# The argument shape's type of randn_op should be list or tuple. # The argument shape's type of randn_op should be list or tuple.
def test_shape_type(): def test_shape_type():
out = paddle.randn(shape=1) out = fluid.layers.randn(shape=1)
self.assertRaises(TypeError, test_shape_type) self.assertRaises(TypeError, test_shape_type)
# The argument dtype of randn_op should be float32 or float64. # The argument dtype of randn_op should be float32 or float64.
def test_dtype_float16(): def test_dtype_float16():
out = paddle.randn(shape=[1, 2], dtype='float16') out = fluid.layers.randn(shape=[1, 2], dtype='float16')
self.assertRaises(TypeError, test_dtype_float16) self.assertRaises(TypeError, test_dtype_float16)
# The argument dtype of randn_op should be float32 or float64. # The argument dtype of randn_op should be float32 or float64.
def test_dtype_int32(): def test_dtype_int32():
out = paddle.randn(shape=[1, 2], dtype='int32') out = fluid.layers.randn(shape=[1, 2], dtype='int32')
self.assertRaises(TypeError, test_dtype_int32) self.assertRaises(TypeError, test_dtype_int32)
# The argument dtype of randn_op should be float32 or float64. # The argument dtype of randn_op should be float32 or float64.
def test_dtype_int64(): def test_dtype_int64():
out = paddle.randn(shape=[1, 2], dtype='int64') out = fluid.layers.randn(shape=[1, 2], dtype='int64')
self.assertRaises(TypeError, test_dtype_int64) self.assertRaises(TypeError, test_dtype_int64)
# The argument dtype of randn_op should be float32 or float64. # The argument dtype of randn_op should be float32 or float64.
def test_dtype_uint8(): def test_dtype_uint8():
out = paddle.randn(shape=[1, 2], dtype='uint8') out = fluid.layers.randn(shape=[1, 2], dtype='uint8')
self.assertRaises(TypeError, test_dtype_uint8) self.assertRaises(TypeError, test_dtype_uint8)
# The argument dtype of randn_op should be float32 or float64. # The argument dtype of randn_op should be float32 or float64.
def test_dtype_bool(): def test_dtype_bool():
out = paddle.randn(shape=[1, 2], dtype='bool') out = fluid.layers.randn(shape=[1, 2], dtype='bool')
self.assertRaises(TypeError, test_dtype_bool) self.assertRaises(TypeError, test_dtype_bool)
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
import unittest import unittest
import numpy as np import numpy as np
from op_test import OpTest from op_test import OpTest
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid.op import Operator from paddle.fluid.op import Operator
...@@ -120,12 +119,12 @@ class TestRandpermOpError(unittest.TestCase): ...@@ -120,12 +119,12 @@ class TestRandpermOpError(unittest.TestCase):
def test_Variable(): def test_Variable():
out = np.arange(10) out = np.arange(10)
paddle.randperm(n=10, out=out) fluid.layers.randperm(n=10, out=out)
self.assertRaises(TypeError, test_Variable) self.assertRaises(TypeError, test_Variable)
def test_value(): def test_value():
paddle.randperm(n=-3) fluid.layers.randperm(n=-3)
self.assertRaises(ValueError, test_value) self.assertRaises(ValueError, test_value)
...@@ -139,9 +138,9 @@ class TestRandpermOp_attr_out(unittest.TestCase): ...@@ -139,9 +138,9 @@ class TestRandpermOp_attr_out(unittest.TestCase):
with fluid.program_guard(train_program, startup_program): with fluid.program_guard(train_program, startup_program):
n = 10 n = 10
data_1 = fluid.layers.fill_constant([n], "int64", 3) data_1 = fluid.layers.fill_constant([n], "int64", 3)
paddle.randperm(n=n, out=data_1) fluid.layers.randperm(n=n, out=data_1)
data_2 = paddle.randperm(n=n, dtype="int32", device="cpu") data_2 = fluid.layers.randperm(n=n, dtype="int32", device="cpu")
place = fluid.CPUPlace() place = fluid.CPUPlace()
if fluid.core.is_compiled_with_cuda(): if fluid.core.is_compiled_with_cuda():
...@@ -160,12 +159,12 @@ class TestRandpermDygraphMode(unittest.TestCase): ...@@ -160,12 +159,12 @@ class TestRandpermDygraphMode(unittest.TestCase):
def test_check_output(self): def test_check_output(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
n = 10 n = 10
data_1 = paddle.randperm(n, dtype="int64") data_1 = fluid.layers.randperm(n, dtype="int64")
data_1_np = data_1.numpy() data_1_np = data_1.numpy()
self.assertTrue( self.assertTrue(
check_randperm_out(n, data_1_np), msg=error_msg(data_1_np)) check_randperm_out(n, data_1_np), msg=error_msg(data_1_np))
data_2 = paddle.randperm(n, dtype="int32", device="cpu") data_2 = fluid.layers.randperm(n, dtype="int32", device="cpu")
data_2_np = data_2.numpy() data_2_np = data_2.numpy()
self.assertTrue( self.assertTrue(
check_randperm_out(n, data_2_np), msg=error_msg(data_2_np)) check_randperm_out(n, data_2_np), msg=error_msg(data_2_np))
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import paddle
import numpy as np import numpy as np
import paddle.fluid.core as core import paddle.fluid.core as core
from op_test import OpTest from op_test import OpTest
...@@ -66,7 +65,7 @@ class TestRollAPI(unittest.TestCase): ...@@ -66,7 +65,7 @@ class TestRollAPI(unittest.TestCase):
# case 1: # case 1:
with program_guard(Program(), Program()): with program_guard(Program(), Program()):
x = fluid.layers.data(name='x', shape=[-1, 3]) x = fluid.layers.data(name='x', shape=[-1, 3])
z = paddle.roll(x, shifts=1) z = fluid.layers.roll(x, shifts=1)
exe = fluid.Executor(fluid.CPUPlace()) exe = fluid.Executor(fluid.CPUPlace())
res, = exe.run(feed={'x': self.data_x}, res, = exe.run(feed={'x': self.data_x},
fetch_list=[z.name], fetch_list=[z.name],
...@@ -78,7 +77,7 @@ class TestRollAPI(unittest.TestCase): ...@@ -78,7 +77,7 @@ class TestRollAPI(unittest.TestCase):
# case 2: # case 2:
with program_guard(Program(), Program()): with program_guard(Program(), Program()):
x = fluid.layers.data(name='x', shape=[-1, 3]) x = fluid.layers.data(name='x', shape=[-1, 3])
z = paddle.roll(x, shifts=1, dims=0) z = fluid.layers.roll(x, shifts=1, dims=0)
exe = fluid.Executor(fluid.CPUPlace()) exe = fluid.Executor(fluid.CPUPlace())
res, = exe.run(feed={'x': self.data_x}, res, = exe.run(feed={'x': self.data_x},
fetch_list=[z.name], fetch_list=[z.name],
...@@ -92,7 +91,7 @@ class TestRollAPI(unittest.TestCase): ...@@ -92,7 +91,7 @@ class TestRollAPI(unittest.TestCase):
# case 1: # case 1:
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(self.data_x) x = fluid.dygraph.to_variable(self.data_x)
z = paddle.roll(x, shifts=1) z = fluid.layers.roll(x, shifts=1)
np_z = z.numpy() np_z = z.numpy()
expect_out = np.array([[9.0, 1.0, 2.0], [3.0, 4.0, 5.0], expect_out = np.array([[9.0, 1.0, 2.0], [3.0, 4.0, 5.0],
[6.0, 7.0, 8.0]]) [6.0, 7.0, 8.0]])
...@@ -101,7 +100,7 @@ class TestRollAPI(unittest.TestCase): ...@@ -101,7 +100,7 @@ class TestRollAPI(unittest.TestCase):
# case 2: # case 2:
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(self.data_x) x = fluid.dygraph.to_variable(self.data_x)
z = paddle.roll(x, shifts=1, dims=0) z = fluid.layers.roll(x, shifts=1, dims=0)
np_z = z.numpy() np_z = z.numpy()
expect_out = np.array([[7.0, 8.0, 9.0], [1.0, 2.0, 3.0], expect_out = np.array([[7.0, 8.0, 9.0], [1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]]) [4.0, 5.0, 6.0]])
......
...@@ -17,7 +17,6 @@ import unittest ...@@ -17,7 +17,6 @@ import unittest
import numpy as np import numpy as np
from op_test import OpTest from op_test import OpTest
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.tensor as tensor
class TrilTriuOpDefaultTest(OpTest): class TrilTriuOpDefaultTest(OpTest):
...@@ -71,7 +70,7 @@ def case_generator(op_type, Xshape, diagonal, expected): ...@@ -71,7 +70,7 @@ def case_generator(op_type, Xshape, diagonal, expected):
data = fluid.data(shape=Xshape, dtype='float64', name=cls_name) data = fluid.data(shape=Xshape, dtype='float64', name=cls_name)
with self.assertRaisesRegexp( with self.assertRaisesRegexp(
eval(expected.split(':')[-1]), errmsg[expected]): eval(expected.split(':')[-1]), errmsg[expected]):
getattr(tensor, op_type)(input=data, diagonal=diagonal) getattr(fluid.layers, op_type)(input=data, diagonal=diagonal)
class SuccessCase(TrilTriuOpDefaultTest): class SuccessCase(TrilTriuOpDefaultTest):
def initTestCase(self): def initTestCase(self):
...@@ -122,7 +121,7 @@ class TestTrilTriuOpAPI(unittest.TestCase): ...@@ -122,7 +121,7 @@ class TestTrilTriuOpAPI(unittest.TestCase):
def test_api(self): def test_api(self):
data = np.random.random([1, 9, 9, 4]).astype('float32') data = np.random.random([1, 9, 9, 4]).astype('float32')
x = fluid.data(shape=[1, 9, -1, 4], dtype='float32', name='x') x = fluid.data(shape=[1, 9, -1, 4], dtype='float32', name='x')
tril_out, triu_out = tensor.tril(x), tensor.triu(x) tril_out, triu_out = fluid.layers.tril(x), fluid.layers.triu(x)
place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda( place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
) else fluid.CPUPlace() ) else fluid.CPUPlace()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册