Exemple #1
0
def logical_and(inputs, **kwargs):
    r"""Compute the element-wise AND logical operation.

    .. math:: \text{out} = \text{input1} \mathbin{\&} \text{input2}

    Examples:

    ```python
    a = dragon.constant([False, True, False, True])
    b = dragon.constant([False, True, True, False])
    c = dragon.constant([0, 1, 0, 2])
    d = dragon.constant([0, 3, 4, 0])
    print(dragon.math.logical_and([a, b]))  # [False, True, False, False]
    print(dragon.math.logical_and([c, d]))  # [False, True, False, False]
    ```

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The input1 and input2 tensor.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute('And', inputs)
    return OpLib.add('And', inputs, **kwargs)
Exemple #2
0
def add(inputs, **kwargs):
    r"""Compute the element-wise addition.

    .. math:: \text{out} = \text{input1} + \text{input2}

    Examples:

    ```python
    a = dragon.constant(1)
    b = dragon.constant(2)
    print(dragon.math.add([a, b]))
    print(a + b)  # Equivalent operation
    ```

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The input1 and input2 tensor.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute('Add', inputs)
    return OpLib.add('Add', inputs, **kwargs)
Exemple #3
0
def less_equal(inputs, **kwargs):
    r"""Compute the element-wise less-equal comparison.

    .. math:: \text{out} = (\text{input1} <= \text{input2})

    Examples:

    ```python
    a = dragon.constant(2)
    b = dragon.constant(3)
    c = dragon.constant(3)
    print(dragon.math.less_equal([a, b]))
    print(dragon.math.less_equal([b, c]))
    ```

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The input1 and input2 tensor.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute('LessEqual', inputs)
    return OpLib.add('LessEqual', inputs, **kwargs)
Exemple #4
0
def atan2(inputs, **kwargs):
    r"""Compute the element-wise arc-tangent of two arguments.

    .. math:: \text{out} = \text{arctan}(\frac{\text{input1}}{\text{input2}})

    Examples:

    ```python
    y = dragon.constant(1)
    x = dragon.constant(2)
    print(dragon.math.atan2([y, x]))  # 0.46364761
    ```

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The input1 and input2 tensor.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute('Atan2', inputs)
    return OpLib.add('Atan2', inputs, **kwargs)
Exemple #5
0
def bitwise_xor(inputs, **kwargs):
    r"""Compute the element-wise XOR bitwise operation.

    .. math:: \text{out} = \text{input1} \oplus \text{input2}

    Examples:

    ```python
    a = dragon.constant([0, -1, 2, -3, 4])
    b = dragon.constant([-4, 3, -2, 1, 0])
    print(dragon.bitwise.bitwise_xor([a, b]))  # [-4, -4, -4, -4, 4]
    ```

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The input1 and input2 tensor.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute('BitwiseXor', inputs)
    return OpLib.add('BitwiseXor', inputs, **kwargs)
Exemple #6
0
def pow(inputs, **kwargs):
    r"""Compute the power of input.

    .. math:: \text{out} = \text{input}^{\text{exponent}}

    The two inputs should be broadcast to each other:

    ```python
    x = dragon.fill(shape=(1, 2), value=2)
    print(dragon.math.pow([x, x]))  # [[4, 4]]
    print(dragon.math.pow([x, 3]))  # [[8, 8]]
    print(dragon.math.pow([3, x]))  # [[9, 9]]
    ```

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The input and exponent tensor.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute('Pow', inputs)
    return OpLib.add('Pow', inputs, **kwargs)
Exemple #7
0
def assign(inputs, starts=None, sizes=None, copy=False, **kwargs):
    r"""Assign the value to input.

    .. math:: \text{input}[\text{start}:\text{start} + \text{size}, ...] = \text{value}

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The input and value tensor.
    starts : Union[Sequence[int], dragon.Tensor]], optional
        The start location for each dimension.
    sizes : Union[Sequence[int], dragon.Tensor]], optional
        The number of elements from start.
    copy : bool, optional, default=False
        Return a new tensor or call in-place.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    args = OpSchema.parse_args(locals())
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        starts = args['starts'] if starts is not None else [0]
        sizes = args['sizes'] if sizes is not None else [-1]
        return OpLib.execute(
            'Assign', inputs, outputs=[None if copy else inputs[0]],
            ndim=len(starts), starts=starts, sizes=sizes)
    return OpLib.add('Assign', **args)
Exemple #8
0
def matmul(inputs, **kwargs):
    r"""Compute the matrix multiplication.

    .. math:: \text{out} = \text{input1} \times \text{input2}

    The behavior depends on the shape of input tensors:

    * If both tensors are 1d, computes the vector product.
    * If tensors are 1d and >=2d, computes the vector-matrix multiplication.
    * If tensors are >=2d and 1d, computes the matrix-vector multiplication.
    * If both tensors are >= 2d, computes the matrix-matrix multiplication.
    * If one tensor is >= 3d, applies batching and broadcasting to the computation.

    Examples:

    ```python
    # Vector x Vector
    a = dragon.ones((2,), 'float32')
    b = dragon.ones((2,), 'float32')
    print(dragon.math.matmul([a, b]))
    # Vector x Matrix
    a = dragon.ones((2,), 'float32')
    b = dragon.ones((2, 3), 'float32')
    print(dragon.math.matmul([a, b]))
    # Matrix x Vector
    a = dragon.ones((3, 2), 'float32')
    b = dragon.ones((2,), 'float32')
    print(dragon.math.matmul([a, b]))
    # Matrix x Matrix
    a = dragon.ones((2, 3), 'float32')
    b = dragon.ones((3, 2), 'float32')
    print(dragon.math.matmul([a, b]))
    ```

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The input tensors.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute('MatMul', inputs)
    return OpLib.add('MatMul', inputs, **kwargs)
Exemple #9
0
def minimum(inputs, **kwargs):
    r"""Compute the minimum value of given two inputs.

    .. math:: \text{out} = \min(\text{input1}, \text{input2})

    Parameters
    ----------
    inputs : Sequence[Union[dragon.Tensor, number]]
        The input1 and input2 tensor.

    Returns
    -------
    dragon.Tensor
        The output tensor.

    """
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute('Minimum', inputs)
    return OpLib.add('Minimum', inputs, **kwargs)
Exemple #10
0
def _apply_binary_op(inputs, op_type, outputs=(None,)):
    """Apply the binary operator."""
    inputs = constant_ops.remove_scalars(inputs)
    if context.executing_eagerly():
        return OpLib.execute(op_type, inputs, outputs=outputs)
    return OpLib.add(op_type, inputs)