Ejemplo n.º 1
0
def sin_cos_positional_embedding(x,
                                 num_encoding_functions,
                                 include_input=True,
                                 log_sampling=True):
    """Given coordinate positions of sampling points as a (N,3) array, this functions returns embeds each point with the sine and cosine function

    Args:
        x (nn.Variable or nn.NdArray): Shape is (N, 3). 
        num_encoding_functions (int): number of frequencies to encode for each grid position
        include_input (bool, optional): Whether include the original grid position along with the encoding of the position. Defaults to True.
        log_sampling (bool, optional): Sample logarithmically and not linearly. Defaults to True.

    Returns:
        [nn.Variable or nn.NdArray]: (N, num_encoding_functions*3*2+3) if include_input is True else (N, num_encoding_functions*3*2)
    """

    encoding = [x] if include_input else []

    if log_sampling:
        frequency_increments = F.arange(0, num_encoding_functions)
        frequency_bands = F.pow2(
            F.constant(2, shape=frequency_increments.shape),
            frequency_increments)
    else:
        frequency_bands = F.arange(2**0,
                                   2**(num_encoding_functions - 1) + 1e-5,
                                   (2**(num_encoding_functions - 1) - 1) /
                                   (num_encoding_functions - 1.0))

    for freq in frequency_bands:
        for func in [F.sin, F.cos]:
            encoding.append(func(x * F.reshape(freq, (1, 1))))
    return F.concatenate(*encoding, axis=x.ndim - 1)
Ejemplo n.º 2
0
    def __rpow__(self, other):
        """
        Element-wise power function.
        Part of the implementation of the power operator expression.

        Args:
            other (float or ~nnabla.Variable): Internally calling
                :func:`~nnabla.functions.pow2` or
                :func:`~nnabla.functions.r_pow_scalar` according to the
                type.

        Returns: :class:`nnabla.Variable`

        """
        import nnabla.functions as F
        if isinstance(other, Variable):
            return F.pow2(other, self)
        return F.r_pow_scalar(self, other)
Ejemplo n.º 3
0
    def __pow__(self, other):
        """
        Element-wise power function.
        Implements the power operator expression ``A ** B``, together with :func:`~nnabla.variable.__rpow__` .
        When a scalar is specified for ``other``, this function performs an
        element-wise operation for all elements in ``self``.

        Args:
            other (float or ~nnabla.Variable): Internally calling
                :func:`~nnabla.functions.pow2` or
                :func:`~nnabla.functions.pow_scalar` according to the
                type.

        Returns: :class:`nnabla.Variable`

        """
        import nnabla.functions as F
        if isinstance(other, Variable):
            return F.pow2(self, other)
        return F.pow_scalar(self, other)