manipulation.py 6.0 KB
Newer Older
1
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle.common_ops_import import *
from ..helper import is_complex, is_real, complex_variable_exists
17 18
from ....fluid.framework import ComplexVariable
from ....fluid import layers
19

20 21 22 23
__all__ = [
    'reshape',
    'transpose',
]
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76


def reshape(x, shape, inplace=False, name=None):
    """
    To change the shape of ``x`` without changing its data.

    There are some tricks when specifying the target shape.

    1. -1 means the value of this dimension is inferred from the total element
    number of x and remaining dimensions. Thus one and only one dimension can
    be set -1.

    2. 0 means the actual dimension value is going to be copied from the
    corresponding dimension of x. The index of 0s in shape can not exceed
    the dimension of x.

    Here are some examples to explain it.

    1. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape
    is [6, 8], the reshape operator will transform x into a 2-D tensor with
    shape [6, 8] and leaving x's data unchanged.

    2. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape
    specified is [2, 3, -1, 2], the reshape operator will transform x into a
    4-D tensor with shape [2, 3, 4, 2] and leaving x's data unchanged. In this
    case, one dimension of the target shape is set to -1, the value of this
    dimension is inferred from the total element number of x and remaining
    dimensions.

    3. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape
    is [-1, 0, 3, 2], the reshape operator will transform x into a 4-D tensor
    with shape [2, 4, 3, 2] and leaving x's data unchanged. In this case,
    besides -1, 0 means the actual dimension value is going to be copied from
    the corresponding dimension of x.

    Args:
        x(ComplexVariable): the input. A ``Tensor`` or ``LoDTensor`` , data 
            type: ``complex64`` or ``complex128``.
        shape(list|tuple|Variable): target shape. At most one dimension of 
            the target shape can be -1. If ``shape`` is a list or tuple, the 
            elements of it should be integers or Tensors with shape [1] and 
            data type ``int32``. If ``shape`` is an Variable, it should be 
            an 1-D Tensor of data type ``int32``.
        inplace(bool, optional): If ``inplace`` is True, the output of 
            ``reshape`` is the same ComplexVariable as the input. Otherwise, 
            the input and output of ``reshape`` are different 
            ComplexVariables. Defaults to False. Note that if ``x``is more 
            than one OPs' input, ``inplace`` must be False.
        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:
77
        ComplexVariable: A ``Tensor`` or ``LoDTensor``. The data type is same as ``x``. It is a new ComplexVariable if ``inplace`` is ``False``, otherwise it is ``x``.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        
    Raises:
        ValueError: If more than one elements of ``shape`` is -1.
        ValueError: If the element of ``shape`` is 0, the corresponding dimension should be less than or equal to the dimension of ``x``.
        ValueError: If the elements in ``shape`` is negative except -1.

    Examples:
        .. code-block:: python

            import paddle.fluid as fluid
            import paddle.complex as cpx
            import paddle.fluid.dygraph as dg
            import numpy as np
            
            x_np = np.random.randn(2, 3, 4) + 1j * np.random.randn(2, 3, 4)
            
            place = fluid.CPUPlace()
            with dg.guard(place):
                x_var = dg.to_variable(x_np)
                y_var = cpx.reshape(x_var, (2, -1))
                y_np = y_var.numpy()
                print(y_np.shape)
                # (2, 12)
101
    """
102 103 104 105 106 107 108 109
    complex_variable_exists([x], "reshape")
    if inplace:
        x.real = fluid.layers.reshape(x.real, shape, inplace=inplace, name=name)
        x.imag = fluid.layers.reshape(x.imag, shape, inplace=inplace, name=name)
        return x
    out_real = fluid.layers.reshape(x.real, shape, inplace=inplace, name=name)
    out_imag = fluid.layers.reshape(x.imag, shape, inplace=inplace, name=name)
    return ComplexVariable(out_real, out_imag)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145


def transpose(x, perm, name=None):
    """
    Permute the data dimensions for complex number :attr:`input` according to `perm`. 
    
    See :ref:`api_fluid_layers_transpose` for the real number API. 
    
    Args:
        x (ComplexVariable): The input n-D ComplexVariable with data type 
            complex64 or complex128.
        perm (list): Permute the input according to the value of perm.
        name (str): The name of this layer. It is optional.

    Returns:
        ComplexVariable: A transposed n-D ComplexVariable, with the same data type as :attr:`input`.

    Examples:
        .. code-block:: python
 
            import paddle
            import numpy as np
            import paddle.fluid.dygraph as dg
 
            with dg.guard():
                a = np.array([[1.0 + 1.0j, 2.0 + 1.0j], [3.0+1.0j, 4.0+1.0j]])
                x = dg.to_variable(a)
                y = paddle.complex.transpose(x, [1, 0])
                print(y.numpy())
                # [[1.+1.j 3.+1.j]
                #  [2.+1.j 4.+1.j]]
    """
    complex_variable_exists([x], "transpose")
    real = layers.transpose(x.real, perm, name)
    imag = layers.transpose(x.imag, perm, name)
    return ComplexVariable(real, imag)