diff --git a/paddle/fluid/operators/trace_op.cc b/paddle/fluid/operators/trace_op.cc index 6bb158c5816762a6d9c4660f49b3fb48168d57f6..66766b4e1cd830f8dda40befa228294b976a4ff7 100644 --- a/paddle/fluid/operators/trace_op.cc +++ b/paddle/fluid/operators/trace_op.cc @@ -30,8 +30,8 @@ class TraceOp : public framework::OperatorWithKernel { ctx->HasOutput("Out"), true, platform::errors::NotFound("Output of TraceOp is not found.")); - int dim1 = ctx->Attrs().Get("dim1"); - int dim2 = ctx->Attrs().Get("dim2"); + int dim1 = ctx->Attrs().Get("axis1"); + int dim2 = ctx->Attrs().Get("axis2"); auto x_dims = ctx->GetInputDim("Input"); @@ -84,15 +84,15 @@ class TraceOpMaker : public framework::OpProtoAndCheckerMaker { )DOC") .SetDefault(0); AddAttr( - "dim1", - R"DOC((int, default 0), the first dim of the 2-D planes from which the diagonals should be taken. - Can be both positive and negative. Default: 0. + "axis1", + R"DOC((int, default 0), the first axis of the 2-D planes from which the diagonals should be taken. + Can be either positive or negative. Default: 0. )DOC") .SetDefault(-2); AddAttr( - "dim2", - R"DOC((int, default 1), the second dim of the 2-D planes from which the diagonals should be taken. - Can be both positive and negative. Default: 1. + "axis2", + R"DOC((int, default 1), the second axis of the 2-D planes from which the diagonals should be taken. + Can be either positive or negative. Default: 1. )DOC") .SetDefault(-1); AddComment(R"DOC( diff --git a/paddle/fluid/operators/trace_op.cu b/paddle/fluid/operators/trace_op.cu index ffba298cc232e82bb7f133f181944f63df72da67..452f2dd9d62bedb449979a11698e4eb0bb116ce9 100644 --- a/paddle/fluid/operators/trace_op.cu +++ b/paddle/fluid/operators/trace_op.cu @@ -33,8 +33,8 @@ class TraceCUDAKernel : public framework::OpKernel { auto* out = context.Output("Out"); const int64_t offset = context.Attr("offset"); - const int64_t dim1 = context.Attr("dim1"); - const int64_t dim2 = context.Attr("dim2"); + const int64_t dim1 = context.Attr("axis1"); + const int64_t dim2 = context.Attr("axis2"); T* out_data = out->mutable_data(context.GetPlace()); const framework::Tensor diag = diff --git a/paddle/fluid/operators/trace_op.h b/paddle/fluid/operators/trace_op.h index 51d807bfb3dd02b2e15fe39ebb749f927667daec..54c4251a38cf10a8f489ca78346fae9471b464db 100644 --- a/paddle/fluid/operators/trace_op.h +++ b/paddle/fluid/operators/trace_op.h @@ -174,8 +174,8 @@ class TraceKernel : public framework::OpKernel { auto* out = context.Output("Out"); const int64_t offset = context.Attr("offset"); - const int64_t dim1 = context.Attr("dim1"); - const int64_t dim2 = context.Attr("dim2"); + const int64_t dim1 = context.Attr("axis1"); + const int64_t dim2 = context.Attr("axis2"); auto output_dims = out->dims(); @@ -205,8 +205,8 @@ class TraceGradKernel : public framework::OpKernel { context.Output(framework::GradVarName("Input")); int64_t offset = context.Attr("offset"); - int64_t dim1 = context.Attr("dim1"); - int64_t dim2 = context.Attr("dim2"); + int64_t dim1 = context.Attr("axis1"); + int64_t dim2 = context.Attr("axis2"); auto input_dims = d_x->dims(); auto input_stride = framework::stride(input_dims); diff --git a/python/paddle/fluid/tests/unittests/test_complex_trace_layer.py b/python/paddle/fluid/tests/unittests/test_complex_trace_layer.py index 5293d594be993a85a54508af99cf08b4a40a380b..acc1e41b246309b312426b0a5b0bb7670c2bdfb7 100644 --- a/python/paddle/fluid/tests/unittests/test_complex_trace_layer.py +++ b/python/paddle/fluid/tests/unittests/test_complex_trace_layer.py @@ -33,7 +33,7 @@ class TestComplexTraceLayer(unittest.TestCase): for place in self._places: with dg.guard(place): var_x = dg.to_variable(input) - result = cpx.trace(var_x, offset=1, dim1=0, dim2=2).numpy() + result = cpx.trace(var_x, offset=1, axis1=0, axis2=2).numpy() target = np.trace(input, offset=1, axis1=0, axis2=2) self.assertTrue(np.allclose(result, target)) diff --git a/python/paddle/fluid/tests/unittests/test_trace_op.py b/python/paddle/fluid/tests/unittests/test_trace_op.py index 5d96d149a08afb3109194303b8144b9487062eb8..7441ff24329fa5a5f57e253ba480d1c7d9ab647c 100644 --- a/python/paddle/fluid/tests/unittests/test_trace_op.py +++ b/python/paddle/fluid/tests/unittests/test_trace_op.py @@ -38,7 +38,7 @@ class TestTraceOp(OpTest): def init_config(self): self.case = np.random.randn(20, 6).astype('float64') self.inputs = {'Input': self.case} - self.attrs = {'offset': 0, 'dim1': 0, 'dim2': 1} + self.attrs = {'offset': 0, 'axis1': 0, 'axis2': 1} self.target = np.trace(self.inputs['Input']) @@ -46,24 +46,24 @@ class TestTraceOpCase1(TestTraceOp): def init_config(self): self.case = np.random.randn(2, 20, 2, 3).astype('float32') self.inputs = {'Input': self.case} - self.attrs = {'offset': 1, 'dim1': 0, 'dim2': 2} + self.attrs = {'offset': 1, 'axis1': 0, 'axis2': 2} self.target = np.trace( self.inputs['Input'], offset=self.attrs['offset'], - axis1=self.attrs['dim1'], - axis2=self.attrs['dim2']) + axis1=self.attrs['axis1'], + axis2=self.attrs['axis2']) class TestTraceOpCase2(TestTraceOp): def init_config(self): self.case = np.random.randn(2, 20, 2, 3).astype('float32') self.inputs = {'Input': self.case} - self.attrs = {'offset': -5, 'dim1': 1, 'dim2': -1} + self.attrs = {'offset': -5, 'axis1': 1, 'axis2': -1} self.target = np.trace( self.inputs['Input'], offset=self.attrs['offset'], - axis1=self.attrs['dim1'], - axis2=self.attrs['dim2']) + axis1=self.attrs['axis1'], + axis2=self.attrs['axis2']) class TestTraceAPICase(unittest.TestCase): @@ -71,7 +71,7 @@ class TestTraceAPICase(unittest.TestCase): case = np.random.randn(2, 20, 2, 3).astype('float32') data1 = fluid.data(name='data1', shape=[2, 20, 2, 3], dtype='float32') out1 = tensor.trace(data1) - out2 = tensor.trace(data1, offset=-5, dim1=1, dim2=-1) + out2 = tensor.trace(data1, offset=-5, axis1=1, axis2=-1) place = core.CPUPlace() exe = fluid.Executor(place) diff --git a/python/paddle/incubate/complex/tensor/math.py b/python/paddle/incubate/complex/tensor/math.py index 51477abd5dcb7ccb742291296db08d11f3614a32..5c26d6da8d9bb002a117ee40e0ce209c3fa0db9f 100644 --- a/python/paddle/incubate/complex/tensor/math.py +++ b/python/paddle/incubate/complex/tensor/math.py @@ -236,39 +236,38 @@ def elementwise_div(x, y, axis=-1, name=None): name=name) -def trace(input, offset=0, dim1=0, dim2=1, name=None): +def trace(x, offset=0, axis1=0, axis2=1, name=None): """ - The layer to compute the trace for a complex number tensor. input :attr:`input` must be a ComplexVariable. + The layer to compute the trace for a complex number tensor. x :attr:`x` must be a ComplexVariable. See the detailed description for the function and other arguments in :ref:`api_tensor_math_trace` . Args: - input(ComplexVariable): The input ComplexVariable. Must be at least 2-dimensional. + x(ComplexVariable): The input ComplexVariable x. Must be at least 2-dimensional. The supported data types include complex64 and complex128. - offset(int, optional): Which diagonals in input tensor will be taken. Default: 0 (main diagonals). - dim1(int, optional): The first dimension with respect to take diagonal. Default: 0. - dim2(int, optional): The second dimension with respect to take diagonal. Default: 1. + offset(int, optional): Which diagonals in input tensor x will be taken. Default: 0 (main diagonals). + axis1(int, optional): The first axis with respect to take diagonal. Default: 0. + axis2(int, optional): The second axis with respect to take diagonal. Default: 1. name (str, optional): Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Default: None. Returns: - ComplexVariable: The trace result of input tensor, it's data type is the same as input data type. + ComplexVariable: The trace result of input tensor x, it's data type is the same as input data type. Examples: .. code-block:: python import paddle - import paddle.fluid.dygraph as dg import numpy as np case1 = np.random.randn(3, 10, 10).astype('float64') + 1j * np.random.randn(3, 10, 10).astype('float64') - with dg.guard(): - case1 = dg.to_variable(case1) - data1 = paddle.complex.trace(case1, offset=1, dim1=1, dim2=2) # data1.shape = [3] + paddle.enable_imperative() + case1 = paddle.imperative.to_variable(case1) + data1 = paddle.complex.trace(case1, offset=1, axis1=1, axis2=2) # data1.shape = [3] """ - complex_variable_exists([input], "trace") - real = math.trace(input.real, offset, dim1, dim2, name) - imag = math.trace(input.imag, offset, dim1, dim2, name) + complex_variable_exists([x], "trace") + real = math.trace(x.real, offset, axis1, axis2, name) + imag = math.trace(x.imag, offset, axis1, axis2, name) return ComplexVariable(real, imag) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 7cc19186d00682d9b0bab473b013b8def3dd1786..500ab01f7c1fb49f37a6948829b9de26defffc58 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -191,8 +191,8 @@ Examples: @templatedoc() def pow(input, exponent, out=None, name=None): """ - :alias_main: paddle.pow - :alias: paddle.pow,paddle.tensor.pow,paddle.tensor.math.pow + :alias_main: paddle.pow + :alias: paddle.pow,paddle.tensor.pow,paddle.tensor.math.pow This is Pow Activation Operator. @@ -262,8 +262,8 @@ def pow(input, exponent, out=None, name=None): def mul(x, y, x_num_col_dims=1, y_num_col_dims=1, out=None, name=None): """ - :alias_main: paddle.mul - :alias: paddle.mul,paddle.tensor.mul,paddle.tensor.math.mul + :alias_main: paddle.mul + :alias: paddle.mul,paddle.tensor.mul,paddle.tensor.math.mul Mul Operator. This operator is used to perform matrix multiplication for input $x$ and $y$. @@ -411,8 +411,8 @@ def _elementwise_op(helper): def add(x, y, alpha=1, out=None, name=None): """ - :alias_main: paddle.add - :alias: paddle.add,paddle.tensor.add,paddle.tensor.math.add + :alias_main: paddle.add + :alias: paddle.add,paddle.tensor.add,paddle.tensor.math.add Examples: @@ -556,8 +556,8 @@ Examples: def div(x, y, out=None, name=None): """ - :alias_main: paddle.div - :alias: paddle.div,paddle.tensor.div,paddle.tensor.math.div + :alias_main: paddle.div + :alias: paddle.div,paddle.tensor.div,paddle.tensor.math.div Examples: @@ -709,8 +709,8 @@ for func in [ def sum(input, dim=None, dtype=None, keep_dim=False, name=None): """ - :alias_main: paddle.sum - :alias: paddle.sum,paddle.tensor.sum,paddle.tensor.math.sum + :alias_main: paddle.sum + :alias: paddle.sum,paddle.tensor.sum,paddle.tensor.math.sum Computes the sum of tensor elements over the given dimension. @@ -814,8 +814,8 @@ def sum(input, dim=None, dtype=None, keep_dim=False, name=None): @templatedoc(op_type="sum") def elementwise_sum(inputs, name=None): """ - :alias_main: paddle.elementwise_sum - :alias: paddle.elementwise_sum,paddle.tensor.elementwise_sum,paddle.tensor.math.elementwise_sum + :alias_main: paddle.elementwise_sum + :alias: paddle.elementwise_sum,paddle.tensor.elementwise_sum,paddle.tensor.math.elementwise_sum ${comment} @@ -912,8 +912,8 @@ def elementwise_sum(inputs, name=None): def mm(input, mat2, out=None, name=None): """ - :alias_main: paddle.mm - :alias: paddle.mm,paddle.tensor.mm,paddle.tensor.math.mm + :alias_main: paddle.mm + :alias: paddle.mm,paddle.tensor.mm,paddle.tensor.math.mm Applies matrix multiplication to two tensors. @@ -1017,8 +1017,8 @@ def mm(input, mat2, out=None, name=None): def addmm(input, x, y, alpha=1.0, beta=1.0, name=None): """ - :alias_main: paddle.addmm - :alias: paddle.addmm,paddle.tensor.addmm,paddle.tensor.math.addmm + :alias_main: paddle.addmm + :alias: paddle.addmm,paddle.tensor.addmm,paddle.tensor.math.addmm **addmm** @@ -1086,8 +1086,8 @@ def addmm(input, x, y, alpha=1.0, beta=1.0, name=None): def logsumexp(x, dim=None, keepdim=False, out=None, name=None): """ - :alias_main: paddle.logsumexp - :alias: paddle.logsumexp,paddle.tensor.logsumexp,paddle.tensor.math.logsumexp + :alias_main: paddle.logsumexp + :alias: paddle.logsumexp,paddle.tensor.logsumexp,paddle.tensor.math.logsumexp This operator calculates the log of the sum of exponentials of the input Tensor. @@ -1157,8 +1157,8 @@ def logsumexp(x, dim=None, keepdim=False, out=None, name=None): def inverse(input, out=None, name=None): """ - :alias_main: paddle.inverse - :alias: paddle.inverse,paddle.tensor.inverse,paddle.tensor.math.inverse + :alias_main: paddle.inverse + :alias: paddle.inverse,paddle.tensor.inverse,paddle.tensor.math.inverse Takes the inverse of the square matrix. A square matrix is a matrix with the same number of rows and columns. The input can be a square matrix @@ -1232,8 +1232,8 @@ def inverse(input, out=None, name=None): def max(input, dim=None, keep_dim=False, out=None, name=None): """ - :alias_main: paddle.max - :alias: paddle.max,paddle.tensor.max,paddle.tensor.math.max + :alias_main: paddle.max + :alias: paddle.max,paddle.tensor.max,paddle.tensor.math.max Computes the maximum of tensor elements over the given dimension. @@ -1312,8 +1312,8 @@ def max(input, dim=None, keep_dim=False, out=None, name=None): def min(input, dim=None, keep_dim=False, out=None, name=None): """ - :alias_main: paddle.min - :alias: paddle.min,paddle.tensor.min,paddle.tensor.math.min + :alias_main: paddle.min + :alias: paddle.min,paddle.tensor.min,paddle.tensor.math.min Computes the minimum of tensor elements over the given dimension. @@ -1391,8 +1391,8 @@ def min(input, dim=None, keep_dim=False, out=None, name=None): def log1p(x, out=None, name=None): """ - :alias_main: paddle.log1p - :alias: paddle.log1p,paddle.tensor.log1p,paddle.tensor.math.log1p + :alias_main: paddle.log1p + :alias: paddle.log1p,paddle.tensor.log1p,paddle.tensor.math.log1p Calculates the natural log of the given input tensor, element-wise. .. math:: @@ -1437,8 +1437,8 @@ def log1p(x, out=None, name=None): def addcmul(input, tensor1, tensor2, value=1.0, out=None, name=None): """ - :alias_main: paddle.addcmul - :alias: paddle.addcmul,paddle.tensor.addcmul,paddle.tensor.math.addcmul + :alias_main: paddle.addcmul + :alias: paddle.addcmul,paddle.tensor.addcmul,paddle.tensor.math.addcmul Calculate the element-wise multiplication of tensor1 and tensor2, then multiply the result by value, and add it to input. The shape of input, @@ -1486,8 +1486,8 @@ def addcmul(input, tensor1, tensor2, value=1.0, out=None, name=None): def clamp(input, min=None, max=None, output=None, name=None): """ - :alias_main: paddle.clamp - :alias: paddle.clamp,paddle.tensor.clamp,paddle.tensor.math.clamp + :alias_main: paddle.clamp + :alias: paddle.clamp,paddle.tensor.clamp,paddle.tensor.math.clamp **clampe layer** @@ -1572,30 +1572,30 @@ def clamp(input, min=None, max=None, output=None, name=None): return output -def trace(input, offset=0, dim1=0, dim2=1, out=None, name=None): +def trace(x, offset=0, axis1=0, axis2=1, name=None): """ - :alias_main: paddle.trace - :alias: paddle.trace,paddle.tensor.trace,paddle.tensor.math.trace + :alias_main: paddle.trace + :alias: paddle.trace,paddle.tensor.trace,paddle.tensor.math.trace - This OP computes the sum along diagonals of the input tensor. + This OP computes the sum along diagonals of the input tensor x. - If ``input`` is 2D, returns the sum of diagonal. + If ``x`` is 2D, returns the sum of diagonal. - If ``input`` has larger dimensions, then returns an tensor of diagonals sum, diagonals be taken from - the 2D planes specified by dim1 and dim2. By default, the 2D planes formed by the first and second dimensions - of the input tensor. + If ``x`` has larger dimensions, then returns an tensor of diagonals sum, diagonals be taken from + the 2D planes specified by axis1 and axis2. By default, the 2D planes formed by the first and second axes + of the input tensor x. - The argument ``offset`` determines where diagonals are taken from input tensor: + The argument ``offset`` determines where diagonals are taken from input tensor x: - If offset = 0, it is the main diagonal. - If offset > 0, it is above the main diagonal. - If offset < 0, it is below the main diagonal. Args: - input(Variable): The input tensor. Must be at least 2-dimensional. The input data type should be float32, float64, int32, int64. - offset(int, optional): Which diagonals in input tensor will be taken. Default: 0 (main diagonals). - dim1(int, optional): The first dimension with respect to take diagonal. Default: 0. - dim2(int, optional): The second dimension with respect to take diagonal. Default: 1. + x(Variable): The input tensor x. Must be at least 2-dimensional. The input data type should be float32, float64, int32, int64. + offset(int, optional): Which diagonals in input tensor x will be taken. Default: 0 (main diagonals). + axis1(int, optional): The first axis with respect to take diagonal. Default: 0. + axis2(int, optional): The second axis with respect to take diagonal. Default: 1. name (str, optional): Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Default: None. Returns: @@ -1605,74 +1605,71 @@ def trace(input, offset=0, dim1=0, dim2=1, out=None, name=None): .. code-block:: python import paddle - import paddle.fluid.dygraph as dg import numpy as np case1 = np.random.randn(2, 3).astype('float32') case2 = np.random.randn(3, 10, 10).astype('float32') case3 = np.random.randn(3, 10, 5, 10).astype('float32') - with dg.guard(): - case1 = dg.to_variable(case1) - case2 = dg.to_variable(case2) - case3 = dg.to_variable(case3) - data1 = paddle.trace(case1) # data1.shape = [1] - data2 = paddle.trace(case2, offset=1, dim1=1, dim2=2) # data2.shape = [3] - data3 = paddle.trace(case3, offset=-3, dim1=1, dim2=-1) # data2.shape = [3, 5] + paddle.enable_imperative() + + case1 = paddle.imperative.to_variable(case1) + case2 = paddle.imperative.to_variable(case2) + case3 = paddle.imperative.to_variable(case3) + data1 = paddle.trace(case1) # data1.shape = [1] + data2 = paddle.trace(case2, offset=1, axis1=1, axis2=2) # data2.shape = [3] + data3 = paddle.trace(case3, offset=-3, axis1=1, axis2=-1) # data2.shape = [3, 5] """ - inputs = {'Input': [input]} - attrs = {'offset': offset, 'dim1': dim1, 'dim2': dim2} + inputs = {'Input': [x]} + attrs = {'offset': offset, 'axis1': axis1, 'axis2': axis2} def __check_input(input, offset, dim1, dim2): - check_dtype(input.dtype, 'Input', + check_dtype(x.dtype, 'Input', ['int32', 'int64', 'float16', 'float32', 'float64'], 'trace') - input_shape = list(input.shape) + input_shape = list(x.shape) assert len(input_shape) >= 2, \ - "The input must be at least 2-dimensional, " \ - "But received Input's dimensional: %s.\n" % \ + "The x must be at least 2-dimensional, " \ + "But received Input x's dimensional: %s.\n" % \ len(input_shape) - dim1_ = dim1 if dim1 >= 0 else len(input_shape) + dim1 - dim2_ = dim2 if dim2 >= 0 else len(input_shape) + dim2 + axis1_ = axis1 if axis1 >= 0 else len(input_shape) + axis1 + axis2_ = axis2 if axis2 >= 0 else len(input_shape) + axis2 - assert dim1_ < len(input_shape), \ - "The argument dim1 is out of range (expected to be in range of [%d, %d], but got %d).\n" \ - % (-(len(input_shape)), len(input_shape) - 1, dim1) + assert axis1_ < len(input_shape), \ + "The argument axis1 is out of range (expected to be in range of [%d, %d], but got %d).\n" \ + % (-(len(input_shape)), len(input_shape) - 1, axis1) - assert dim2_ < len(input_shape), \ - "The argument dim2 is out of range (expected to be in range of [%d, %d], but got %d).\n" \ - % (-(len(input_shape)), len(input_shape) - 1, dim2) + assert axis2_ < len(input_shape), \ + "The argument axis2 is out of range (expected to be in range of [%d, %d], but got %d).\n" \ + % (-(len(input_shape)), len(input_shape) - 1, axis2) - assert dim1_ != dim2_, \ - "dim1 and dim2 cannot be the same dimension." \ - "But received dim1 = %d, dim2 = %d\n"%(dim1, dim2) + assert axis1_ != axis2_, \ + "axis1 and axis2 cannot be the same axis." \ + "But received axis1 = %d, axis2 = %d\n"%(axis1, axis2) if not in_dygraph_mode(): - __check_input(input, offset, dim1, dim2) + __check_input(input, offset, axis1, axis2) helper = LayerHelper('trace', **locals()) - if out is None: - out = helper.create_variable_for_type_inference(dtype=input.dtype) - else: - check_variable_and_dtype(out, 'out', ['float16', 'float32', 'float64', 'int32', 'int64'], 'trace') + out = helper.create_variable_for_type_inference(dtype=x.dtype) helper.append_op( type='trace', - inputs={'Input': [input]}, + inputs={'Input': [x]}, attrs={'offset': offset, - 'dim1': dim1, - 'dim2': dim2}, + 'axis1': axis1, + 'axis2': axis2}, outputs={'Out': [out]}) return out @templatedoc(op_type="kron") def kron(x, y, out=None, name=None): """ - :alias_main: paddle.kron - :alias: paddle.kron,paddle.tensor.kron,paddle.tensor.math.kron + :alias_main: paddle.kron + :alias: paddle.kron,paddle.tensor.kron,paddle.tensor.math.kron ${comment}