Ejemplo n.º 1
0
def identity(inputs, **kwargs):
    """Return a tensor copied from the input.

    Examples:

    ```python
    # Copy ``x`` to ``y``
    x = dragon.zeros(shape=(2, 3))
    y = dragon.identity(x)

    # ``x`` != ``y``
    x += 1
    print(x)
    print(y)
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Identity', inputs)
    return OpLib.add('Identity', inputs, **kwargs)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def exp(inputs, **kwargs):
    r"""Compute the exponential of input.

    .. math:: \text{out} = \exp(\text{input})

    Examples:

    ```python
    x = dragon.constant([1., 2., 3.])
    print(dragon.math.exp(x))  # [2.71828183, 7.3890561, 20.08553692]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Exp', inputs)
    return OpLib.add('Exp', inputs, **kwargs)
Ejemplo n.º 5
0
def invert(inputs, **kwargs):
    r"""Invert each bit of input.

    .. math:: \text{out} = \,\,\sim \text{input}

    Examples:

    ```python
    # Typically, ``x`` is a bool tensor
    print(dragon.bitwise.invert(dragon.constant([0, 1], 'bool')))  # [True, False]

    # Otherwise, integral types are required (unsigned or signed)
    # 00001101 (13) -> 11110010 (?)
    print(dragon.bitwise.invert(dragon.constant(13, 'uint8')))  # 242
    print(dragon.bitwise.invert(dragon.constant(13, 'int8')))   # -14
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('BitwiseNot', inputs)
    return OpLib.add('BitwiseNot', inputs, **kwargs)
Ejemplo n.º 6
0
def ceil(inputs, **kwargs):
    r"""Compute the smallest integer not less than input.

    .. math:: \text{out} = \lceil \text{input} \rceil

    Examples:

    ```python
    x = dragon.constant([1.4, 1.7, 2.0])
    print(dragon.math.ceil(x))  # [2., 2., 2.]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Ceil', inputs)
    return OpLib.add('Ceil', inputs, **kwargs)
Ejemplo n.º 7
0
def cos(inputs, **kwargs):
    r"""Compute the cos of input.

    .. math:: \text{out} = \cos(\text{input})

    Examples:

    ```python
    x = dragon.constant([0., math.pi])
    print(dragon.math.cos(x))  # [1., -1.]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Cos', inputs)
    return OpLib.add('Cos', inputs, **kwargs)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
def reciprocal(inputs, **kwargs):
    r"""Compute the reciprocal of input.

    .. math:: \text{out} = \frac{1}{\text{input}}

    Examples:

    ```python
    x = dragon.constant([0., 1., 2.])
    print(dragon.math.reciprocal(x))  # [inf, 1., 0.5]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Reciprocal', inputs)
    return OpLib.add('Reciprocal', inputs, **kwargs)
Ejemplo n.º 10
0
def accuracy(inputs, axis=-1, top_k=1, ignore_index=None, **kwargs):
    """Compute the top-k accuracy.

    Parameters
    ----------
    inputs : Sequence[dragon.Tensor]
        The tensor ``logit`` and ``label``.
    axis : int, optional, default=-1
        The axis to reduce, can be negative.
    top_k : int, optional, default=1
        The top-k accuracy to compute.
    ignore_index : int, optional
        The ignored value of target.

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

    """
    if context.executing_eagerly():
        return OpLib.execute(
            'Accuracy', inputs, axis=axis, top_k=top_k,
            ignore_index=ignore_index)
    return OpLib.add('Accuracy', inputs, axis=axis, top_k=top_k,
                     ignore_index=ignore_index, **kwargs)
Ejemplo n.º 11
0
def argmin(inputs, axis=0, keepdims=False, **kwargs):
    """Compute the index of minimum elements along the given axis.

    :attr:`axis` could be negative:

    ```python
    # A negative axis is the last-k axis
    x = dragon.constant([[1, 2, 3], [4, 5, 6]])
    print(dragon.math.argmin(x, axis=1))
    print(dragon.math.argmin(x, axis=-1))  # Equivalent
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.
    axis : int, optional, default=0
        The axis to reduce.
    keepdims : bool, optional, default=False
        Keep the reduced dimension or not.

    Returns
    -------
    dragon.Tensor
        The index of minimum elements.

    """
    if context.executing_eagerly():
        return OpLib.execute('ArgMin', inputs, axis=axis, keepdims=keepdims)
    return OpLib.add('ArgMin', inputs, axis=axis, keepdims=keepdims)
Ejemplo n.º 12
0
def tile(inputs, repeats, **kwargs):
    """Repeat elements along each axis of input.

    Examples:

    ```python
    x = dragon.constant([[1, 2], [3, 4]])
    print(dragon.tile(x, repeats=(1, 2)))  # [[1, 2, 1, 2], [3, 4, 3, 4]]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.
    repeats : Union[Sequence[int], dragon.Tensor]]
        The repetition for each axis.

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

    """
    args = OpSchema.parse_args(locals())
    if context.executing_eagerly():
        return OpLib.execute(
            'Tile', inputs, ndim=len(args['repeats']), repeats=args['repeats'])
    return OpLib.add('Tile', **args)
Ejemplo n.º 13
0
def shape(inputs, **kwargs):
    """Return the shape of input.

    Examples:

    ```python
    x = dragon.ones((2, 3))
    print(x.shape)  # Return a sequence
    print(dragon.shape(x))  # Return a tensor
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

    Returns
    -------
    dragon.Tensor
        The tensor shape.

    """
    if context.executing_eagerly():
        return OpLib.execute('Shape', inputs)
    return OpLib.add('Shape', inputs, **kwargs)
Ejemplo n.º 14
0
def reverse(inputs, axis, **kwargs):
    """Reverse elements along the given axis.

    :attr:`axis` could be negative:

    ```python
    x = dragon.constant([[1, 2, 3], [4, 5, 6]])

    # A negative axis is the last-k axis
    print(dragon.reverse(x, axis=1))  # [[3, 2, 1], [6, 5, 4]]
    print(dragon.reverse(x, axis=-1))  # Equivalent

    # Also, axis could be a sequence of integers
    print(dragon.reverse(x, axis=(0, 1)))  # [[6, 5, 4], [3, 2, 1]]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.
    axis : Union[int, Sequence[int]]
        The axis to reverse.

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

    """
    axes = nest.flatten(axis) if axis is not None else axis
    if context.executing_eagerly():
        return OpLib.execute('Reverse', inputs, axes=axes)
    return OpLib.add('Reverse', inputs, axes=axes, **kwargs)
Ejemplo n.º 15
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)
Ejemplo n.º 16
0
def round(inputs, **kwargs):
    r"""Compute the nearest integer of input.

    .. math:: \text{out} = \lfloor \text{input} \rceil

    Examples:

    ```python
    x = dragon.constant([0.9, 1.4, 1.9])
    print(dragon.math.round(x))  # [1., 1., 2.]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Round', inputs)
    return OpLib.add('Round', inputs, **kwargs)
Ejemplo n.º 17
0
def cast(inputs, dtype, copy=True, **kwargs):
    """Convert the data type of input.

    Examples:

    ```python
    x = dragon.constant([1, 2, 3], dtype='int64')
    print(dragon.cast(x, dtype='float32'))
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.
    dtype : str
        The data type to convert to.
    copy : bool, optional, default=True
        Return a new tensor or call in-place.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Cast',
                             inputs,
                             outputs=[None] if copy else inputs,
                             dtype=dtype)
    return OpLib.add('Cast', inputs, dtype=dtype, **kwargs)
Ejemplo n.º 18
0
def rsqrt(inputs, **kwargs):
    r"""Compute the reciprocal square root of input.

    .. math:: \text{out} = \frac{1}{\sqrt{\text{input}}}

    Examples:

    ```python
    x = dragon.constant([0., 4., 16.])
    print(dragon.math.rsqrt(x))  # [inf, 0.5, 0.25]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Rsqrt', inputs)
    return OpLib.add('Rsqrt', inputs, **kwargs)
Ejemplo n.º 19
0
def clip(inputs, low=None, high=None, **kwargs):
    r"""Compute the clipped input according to the given bounds.

    .. math:: \text{out} = \min(\max(\text{input}, \text{low}), \text{high})

    Examples:

    ```python
    x = dragon.constant([0, 1, 2, 3])
    print(dragon.math.clip(x, low=1))  # [1, 1, 2, 3]
    print(dragon.math.clip(x, high=2))  # [0, 1, 2, 2]
    print(dragon.math.clip(x, low=1, high=2))  # [1, 1, 2, 2]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.
    low : number, optional
        The value to :math:`\text{low}`.
    high : number, optional
        The value to :math:`\text{high}`.

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

    """
    low = float(low) if low is not None else None
    high = float(high) if high is not None else None
    if context.executing_eagerly():
        return OpLib.execute('Clip', inputs, low=low, high=high)
    return OpLib.add('Clip', inputs, low=low, high=high, **kwargs)
Ejemplo n.º 20
0
def sign(inputs, **kwargs):
    r"""Compute the sign indication of input.

    .. math::
        \text{out}_[i] =
            \begin{cases}
                -1, & \text{ if } \text{input}_{i} < 0 \\
                 0, & \text{ if } \text{input}_{i} = 0 \\
                 1, & \text{ if } \text{input}_{i} > 0
            \end{cases}

    Examples:

    ```python
    x = dragon.constant([-2, 0, 2])
    print(dragon.math.sign(x))  # [-1, 0, 1]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Sign', inputs)
    return OpLib.add('Sign', inputs, **kwargs)
Ejemplo n.º 21
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)
Ejemplo n.º 22
0
def sqrt(inputs, **kwargs):
    r"""Compute the square root of input.

    .. math:: \text{out} = \sqrt{\text{input}}

    Examples:

    ```python
    x = dragon.constant([4., 9., 16.])
    print(dragon.math.sqrt(x))  # [2., 3., 4.]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Sqrt', inputs)
    return OpLib.add('Sqrt', inputs, **kwargs)
Ejemplo n.º 23
0
def floor(inputs, **kwargs):
    r"""Compute the largest integer not greater than input.

    .. math:: \text{out} = \lfloor \text{input} \rfloor

    Examples:

    ```python
    x = dragon.constant([0.9, 1.4, 1.9])
    print(dragon.math.floor(x))  # [0., 1., 1.]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Floor', inputs)
    return OpLib.add('Floor', inputs, **kwargs)
Ejemplo n.º 24
0
def square(inputs, **kwargs):
    r"""Compute the square of input.

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

    Examples:

    ```python
    x = dragon.constant([2, 3, 4])
    print(dragon.math.square(x))
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Square', inputs)
    return OpLib.add('Square', inputs, **kwargs)
Ejemplo n.º 25
0
def is_nan(inputs, **kwargs):
    r"""Check if the elements of input are NaN.

    .. math:: \text{out} = \text{isnan}(\text{input})

    Examples:

    ```python
    x = dragon.constant([0., 1., float('nan')])
    print(dragon.math.is_nan(x))  # [False, False, True]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('IsNaN', inputs)
    return OpLib.add('IsNaN', inputs, **kwargs)
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
def log(inputs, **kwargs):
    r"""Compute the logarithm of input.

    .. math:: \text{out} = \log(\text{input})

    Examples:

    ```python
    x = dragon.constant([1., 2., 3.])
    print(dragon.math.log(x))  # [0., 0.69314718, 1.09861229]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Log', inputs)
    return OpLib.add('Log', inputs, **kwargs)
Ejemplo n.º 28
0
def abs(inputs, **kwargs):
    r"""Compute the absolute value of input.

    .. math:: \text{out} = \left| \text{input} \right|

    Examples:

    ```python
    x = dragon.constant([-1, 0, 1])
    print(dragon.math.abs(x))  # [1, 0, 1]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Abs', inputs)
    return OpLib.add('Abs', inputs, **kwargs)
Ejemplo n.º 29
0
def logical_not(inputs, **kwargs):
    r"""Compute the element-wise NOT logical operation.

    .. math:: \text{out} = \,\,\sim \text{input}

    Examples:

    ```python
    a = dragon.constant([False, True, True])
    b = dragon.constant([0, 1, 2])
    print(dragon.math.logical_not(a))  # [True, False, False]
    print(dragon.math.logical_not(b))  # [True, False, False]
    ```

    Parameters
    ----------
    inputs : dragon.Tensor
        The input tensor.

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

    """
    if context.executing_eagerly():
        return OpLib.execute('Not', inputs)
    return OpLib.add('Not', inputs, **kwargs)
Ejemplo n.º 30
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)