Layers#

ivy.adaptive_avg_pool1d(input, output_size)[source]#

Apply a 1D adaptive average pooling over an input signal composed of several input planes.

Parameters:
  • input (Union[Array, NativeArray]) – Input array. Must have shape (N, C, L_in) or (C, L_in) where N is the batch dimension, C is the feature dimension, and L_in is the spatial dimension.

  • output_size (int) – Spatial output size.

Return type:

Array

Returns:

The result of the pooling operation. Will have shape (N, C, L_out) or (C, L_out), where L_out = output_size

ivy.adaptive_avg_pool2d(input, output_size, /, *, data_format='NHWC')[source]#

Apply a 2D adaptive average pooling over an input signal composed of several input planes.

Parameters:
  • input (Union[Array, NativeArray]) – A 3D or 4D input array. Should have a floating-point data type.

  • output_size (Union[Sequence[int], int]) – Spatial output size.

  • data_format (str, default: 'NHWC') – “NHWC” or “NCHW”. Defaults to “NHWC”.

Return type:

Array

Returns:

The result of the pooling operation. Will have shape (N, C, S_0, S_1) or (C, S_0, S_1), where S = output_size

ivy.adaptive_max_pool2d(input, output_size)[source]#

Apply a 2D adaptive maximum pooling over an input signal composed of several input planes.

Parameters:
  • input (Union[Array, NativeArray]) – Input array. Must have shape (N, C, H_in, W_in) or (C, H_in, W_in) where N is the batch dimension, C is the feature dimension, and H_in and W_in are the 2 spatial dimensions.

  • output_size (Union[Sequence[int], int]) – Spatial output size.

Returns:

The result of the pooling operation. Will have shape (N, C, S_0, S_1) or (C, S_0, S_1), where S = output_size

ivy.adaptive_max_pool3d(input, output_size)[source]#

Apply a 3D adaptive maximum pooling over an input signal composed of several input planes.

Parameters:
  • input (Union[Array, NativeArray]) – Input array. Must have shape (N, C, D_in, H_in, W_in) or (C, D_in, H_in, W_in) where N is the batch dimension, C is the feature dimension, and D_in, H_in, and W_in are the 3 spatial dimensions.

  • output_size (Union[Sequence[int], int]) – Spatial output size.

Returns:

The result of the pooling operation. Will have shape (N, C, D_out, H_out, W_out) or (C, D_out, H_out, W_out), where D_out, H_out, W_out = output_size

ivy.area_interpolate(x, dims, size, scale)[source]#
ivy.avg_pool1d(x, kernel, strides, padding, /, *, data_format='NWC', count_include_pad=False, ceil_mode=False, divisor_override=None, out=None)[source]#

Compute a 1-D avg pool given 3-D input x.

Parameters:
  • x (Union[Array, NativeArray]) – Input image [batch_size, w, d_in].

  • kernel (Union[int, Tuple[int]]) – Size of the kernel i.e., the sliding window for each dimension of input. [w].

  • strides (Union[int, Tuple[int]]) – The stride of the sliding window for each dimension of input.

  • padding (Union[str, int, List[Tuple[int, int]]]) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (str, default: 'NWC') – NWC” or “NCW”. Defaults to “NWC”.

  • count_include_pad (bool, default: False) – Whether to include padding in the averaging calculation.

  • ceil_mode (bool, default: False) – Whether to use ceil or floor for creating the output shape.

  • divisor_override (Optional[int], default: None) – If specified, it will be used as the divisor, otherwise kernel_size will be used.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.avg_pool1d(x, 2, 2, 'SAME'))
ivy.array([[[ 2.,  3.,  4.,  5.],
        [ 8.,  9., 10., 11.]],
[[14., 15., 16., 17.],

[20., 21., 22., 23.]]])

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.avg_pool1d(x, 2, 2, 'VALID'))
ivy.array([[[ 2.,  3.,  4.,  5.]],

[[14., 15., 16., 17.]]])

ivy.avg_pool2d(x, kernel, strides, padding, /, *, data_format='NHWC', count_include_pad=False, ceil_mode=False, divisor_override=None, out=None)[source]#

Compute a 2-D average pool given 4-D input x.

Parameters:
  • x (Union[Array, NativeArray]) – Input image [batch_size,h,w,d_in].

  • kernel (Union[int, Tuple[int], Tuple[int, int]]) – Size of the kernel i.e., the sliding window for each dimension of input. [h,w].

  • strides (Union[int, Tuple[int], Tuple[int, int]]) – The stride of the sliding window for each dimension of input.

  • padding (Union[str, int, List[Tuple[int, int]]]) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (str, default: 'NHWC') – NHWC” or “NCHW”. Defaults to “NHWC”.

  • count_include_pad (bool, default: False) – Whether to include padding in the averaging calculation.

  • ceil_mode (bool, default: False) – Whether to use ceil or floor for creating the output shape.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(12.).reshape((2, 1, 3, 2))
>>> print(ivy.avg_pool2d(x, (2, 2), (1, 1), 'SAME'))
ivy.array([[[[ 1.,  2.],
         [ 3.,  4.],
         [ 4.,  5.]]],
[[[ 7., 8.],

[ 9., 10.], [10., 11.]]]])

>>> x = ivy.arange(48.).reshape((2, 4, 3, 2))
>>> print(ivy.avg_pool2d(x, 3, 1, 'VALID'))
ivy.array([[[[ 8.,  9.]],

[[14., 15.]]],

[[[32., 33.]],

[[38., 39.]]]])

ivy.avg_pool3d(x, kernel, strides, padding, /, *, data_format='NDHWC', count_include_pad=False, ceil_mode=False, divisor_override=None, out=None)[source]#

Compute a 3-D avg pool given 5-D input x.

Parameters:
  • x (Union[Array, NativeArray]) – Input volume [batch_size,d,h,w,d_in].

  • kernel (Union[int, Tuple[int], Tuple[int, int, int]]) – Convolution filters [d,h,w].

  • strides (Union[int, Tuple[int], Tuple[int, int, int]]) – The stride of the sliding window for each dimension of input.

  • padding (Union[str, int, List[Tuple[int, int]]]) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (str, default: 'NDHWC') – NDHWC” or “NCDHW”. Defaults to “NDHWC”.

  • count_include_pad (bool, default: False) – Whether to include padding in the averaging calculation.

  • ceil_mode (bool, default: False) – Whether to use ceil or floor for creating the output shape.

  • divisor_override (Optional[int], default: None) – If specified, it will be used as divisor, otherwise kernel_size will be used.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(48.).reshape((2, 3, 2, 2, 2))
>>> print(ivy.avg_pool3d(x,2,2,'VALID'))
ivy.array([[[[[ 7.,  8.]]]],

[[[[31., 32.]]]]])

>>> print(ivy.avg_pool3d(x,2,2,'SAME'))
ivy.array([[[[[ 7.,  8.]]],

[[[19., 20.]]]],

[[[[31., 32.]]],

[[[43., 44.]]]]])

ivy.dct(x, /, *, type=2, n=None, axis=-1, norm=None, out=None)[source]#

Compute the 1D Discrete Cosine Transformation of a given signal.

Parameters:
  • x (Union[Array, NativeArray]) – The input signal.

  • type (Literal[1, 2, 3, 4], default: 2) – The type of the dct. Must be 1, 2, 3 or 4.

  • n (Optional[int], default: None) – The length of the transform. If n is less than the input signal length, then x is truncated, if n is larger then x is zero-padded.

  • axis (int, default: -1) – The axis to compute the DCT along.

  • norm (Optional[Literal['ortho']], default: None) – The type of normalization to be applied. Must be either None or “ortho”.

  • out (Optional[Union[Array, NativeArray]], default: None) – optional output array, for writing the result to.

Return type:

Union[Array, NativeArray]

Returns:

  • ret – Array containing the transformed input.

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([8, 16, 24, 32, 40, 48, 56, 64])
>>> y = ivy.dct(x, type=2, n=None, norm='ortho')
>>> print(y)
ivy.array([ 1.01823380e+02, -5.15385818e+01,  1.36371466e-06, -5.38763905e+00,
        0.00000000e+00, -1.60722279e+00, -8.80319249e-08, -4.05617893e-01])
>>> x = ivy.array([[[8, 16, 24, 32], [40, 48, 56, 64]],
...                [[1,  2,  3,  4], [ 5,  6,  7,  8]]])
>>> y = ivy.dct(x, type=1, n=None, axis=0, norm=None)
>>> print(y)
ivy.array([[[ 9., 18., 27., 36.],
        [45., 54., 63., 72.]],
[[ 7., 14., 21., 28.],

[35., 42., 49., 56.]]])

>>> x = ivy.array([[ 8.1, 16.2, 24.3, 32.4],
...                [40.5, 48.6, 56.7, 64.8]])
>>> y = ivy.zeros((2, 4), dtype=ivy.float32)
>>> ivy.dct(x, type=1, n=None, norm=None, out=y)
>>> print(y)
ivy.array([[ 1.21500000e+02, -3.24000015e+01,  1.90734863e-06,
        -8.10000420e+00],
       [ 3.15899994e+02, -3.24000053e+01,  3.81469727e-06,
        -8.09999847e+00]])
>>> x = ivy.array([8., 16., 24., 32., 40., 48., 56., 64.])
>>> ivy.dct(x, type=4, n=None, norm=None, out=x)
>>> print(x)
ivy.array([ 279.4135742 , -279.6779785 ,  128.3770599 , -114.8719864 ,
         83.72109985,  -79.52869415,   69.79182434,  -68.72489166])

With one ivy.Container input:

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> y = ivy.dct(x, type=3, n=None, norm='ortho')
>>> print(y)
{
    a: ivy.array([79.49862671, -70.37691498, 30.00390816, -23.58938599,
                  13.92713165, -10.078475, 5.19664812, -1.95411837]),
    b: ivy.array([9.93732834, -8.79711437, 3.75048852, -2.94867325, 1.74089146,
                  -1.25980937, 0.64958102, -0.2442648])
}

With multiple ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> container_n = ivy.Container(a=9, b=4)
>>> container_type = ivy.Container(a=2, b=1)
>>> container_norm = ivy.Container(a="ortho", b=None)
>>> y = ivy.dct(x, type=container_type, n=container_n, norm=container_norm)
>>> print(y)
{
    a: ivy.array([96., -28.1580677, -31.89422607, 22.86190414,
                  -26.00041008, 19.75149155, -16.97056389, 10.87819386,
                  -5.89381361]),
    b: ivy.array([1.50000000e+01, -4.00000000e+00, -2.22044605e-16,
                  -1.00000000e+00])
}
ivy.dft(x, /, *, axis=1, inverse=False, onesided=False, dft_length=None, norm='backward', out=None)[source]#

Compute the discrete Fourier transform of input.

Parameters:
  • x (Union[Array, NativeArray]) – Input volume […,d_in,…], where d_in indicates the dimension that needs FFT.

  • axis (int, default: 1) – The axis on which to perform the DFT. By default this value is set to 1, which corresponds to the first dimension after the batch index.

  • inverse (bool, default: False) – Whether to perform the inverse discrete fourier transform. By default this value is set to False.

  • onesided (bool, default: False) – If onesided is True, only values for w in [0, 1, 2, …, floor(n_fft/2) + 1] are returned because the real-to-complex Fourier transform satisfies the conjugate symmetry, i.e., X[m, w] = X[m,w]=X[m,n_fft-w]*. Note if the input or window tensors are complex, then onesided output is not possible. Enabling onesided with real inputs performs a Real-valued fast Fourier transform (RFFT). When invoked with real or complex valued input, the default value is False. Values can be True or False.

  • dft_length (Optional[Union[int, Tuple[int]]], default: None) – The length of the signal.If greater than the axis dimension, the signal will be zero-padded up to dft_length. If less than the axis dimension, only the first dft_length values will be used as the signal. It’s an optional value.

  • norm (str, default: 'backward') – Optional argument, “backward”, “ortho” or “forward”. Defaults to be “backward”. “backward” indicates no normalization. “ortho” indicates normalization by 1/sqrt(n). “forward” indicates normalization by 1/n.

  • out (Optional[Array], default: None) – Optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – The Fourier Transform of the input vector.If onesided is False, the following shape is expected: [batch_idx][signal_dim1][signal_dim2] …[signal_dimN][2]. If axis=0 and onesided is True, the following shape is expected: [batch_idx][floor(signal_dim1/2)+1][signal_dim2]…[signal_dimN][2]. If axis=1 and onesided is True, the following shape is expected: [batch_idx][signal_dim1][floor(signal_dim2/2)+1]…[signal_dimN][2]. If axis=N-1 and onesided is True, the following shape is expected: [batch_idx][signal_dim1][signal_dim2]…[floor(signal_dimN/2)+1][2]. The signal_dim at the specified axis is equal to the dft_length.

ivy.dropout1d(x, prob, /, *, training=True, data_format='NWC', out=None)[source]#

Randomly zero out entire channels with probability prob using samples from a Bernoulli distribution and the remaining channels are scaled by (1/1-prob). In this case, dropout1d performs a channel-wise dropout but assumes a channel is a 1D feature map.

Parameters:
  • x (Union[Array, NativeArray]) – a 2D or 3D input array. Should have a floating-point data type.

  • prob (float) – probability of a channel to be zero-ed.

  • training (bool, default: True) – controls whether dropout1d is performed during training or ignored during testing.

  • data_format (str, default: 'NWC') – “NWC” or “NCW”. Defaults to “NWC”.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret

    an array with some channels zero-ed and the rest of channels are

    scaled by (1/1-prob).

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([1, 1, 1]).reshape([1, 1, 3])
>>> y = ivy.dropout1d(x, 0.5)
>>> print(y)
ivy.array([[[2., 0, 2.]]])
>>> x = ivy.array([1, 1, 1]).reshape([1, 1, 3])
>>> y = ivy.dropout1d(x, 1, training=False, data_format="NCW")
>>> print(y)
ivy.array([[[1, 1, 1]]])

With one ivy.Container input: >>> x = ivy.Container(a=ivy.array([100, 200, 300]).reshape([1, 1, 3]), … b=ivy.array([400, 500, 600]).reshape([1, 1, 3])) >>> y = ivy.dropout1d(x, 0.5) >>> print(y) {

a: ivy.array([[[200., 400., 0.]]]), b: ivy.array([[[0., 0., 0.]]])

}

ivy.dropout2d(x, prob, /, *, training=True, data_format='NHWC', out=None)[source]#

Randomly zero out entire channels with probability prob using samples from a Bernoulli distribution and the remaining channels are scaled by (1/1-prob). In this case, dropout2d performs a channel-wise dropout but assumes a channel is a 2D feature map.

Parameters:
  • x (Union[Array, NativeArray]) – a 3D or 4D input array. Should have a floating-point data type.

  • prob (float) – probability of a channel to be zero-ed.

  • training (bool, default: True) – controls whether dropout2d is performed during training or ignored during testing.

  • data_format (str, default: 'NHWC') – “NHWC” or “NCHW”. Defaults to “NHWC”.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret

    an array with some channels zero-ed and the rest of channels are

    scaled by (1/1-prob).

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([[1, 1, 1]])
>>> y = ivy.dropout2d(x, 0.5)
>>> print(y)
ivy.array([[0., 2., 2.]])
>>> x = ivy.array([[1, 1, 1]])
>>> y = ivy.dropout2d(x, 1, training=False, data_format="NCW")
>>> print(y)
ivy.array([[1, 1, 1]])
ivy.dropout3d(x, prob, /, *, training=True, data_format='NDHWC', out=None)[source]#

Randomly zero out entire channels with probability prob using samples from a Bernoulli distribution and the remaining channels are scaled by (1/1-prob). In this case, dropout3d performs a channel-wise dropout but assumes a channel is a 1D feature map.

Parameters:
  • x (Union[Array, NativeArray]) – a 4D or 5D input array. Should have a floating-point data type.

  • prob (float) – probability of a channel to be zero-ed.

  • training (bool, default: True) – controls whether dropout3d is performed during training or ignored during testing.

  • data_format (str, default: 'NDHWC') – “NDHWC” or “NCDHW”. Defaults to “NDHWC”.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret

    an array with some channels zero-ed and the rest of channels are

    scaled by (1/1-prob).

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

ivy.embedding(weights, indices, /, *, max_norm=None, out=None)[source]#

Embeds a given tensor of indices using a given tensor of weights.

Parameters:
  • weights (Union[Array, NativeArray]) – The weights tensor.

  • indices (Union[Array, NativeArray]) – The indices tensor.

  • max_norm (Optional[int], default: None) – The maximum norm of the embeddings.

  • out (Optional[Array], default: None) – Optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – The result of the embedding operation.

Examples

>>> weights = ivy.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
>>> indices = ivy.array([0, 2])
>>> print(ivy.embedding(weights, indices, max_norm=5))
ivy.array([[1.        , 2.        , 3.        ],
       [2.51285338, 2.87183261, 3.2308116 ]])
ivy.fft(x, dim, /, *, norm='backward', n=None, out=None)[source]#

Compute the one dimensional discrete Fourier transform given input at least 1-D input x.

Parameters:
  • x (Union[Array, NativeArray]) – Input volume […,d_in,…], where d_in indicates the dimension that needs FFT.

  • dim (int) – The dimension along which to take the one dimensional FFT.

  • norm (str, default: 'backward') – Optional argument, “backward”, “ortho” or “forward”. Defaults to be “backward”. “backward” indicates no normalization. “ortho” indicates normalization by $frac{1}{sqrt{n}}$. “forward” indicates normalization by $frac{1}{n}$.

  • n (Optional[Union[int, Tuple[int]]], default: None) – Optional argument indicating the sequence length, if given, the input would be padded with zero or truncated to length n before performing FFT. Should be a integer greater than 1.

  • out (Optional[Array], default: None) – Optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – The result of the FFT operation.

Examples

>>> ivy.fft(np.exp(2j * np.pi * np.arange(8) / 8), 0)
ivy.array([-3.44509285e-16+1.14423775e-17j,  8.00000000e+00-8.11483250e-16j,
        2.33486982e-16+1.22464680e-16j,  0.00000000e+00+1.22464680e-16j,
        9.95799250e-17+2.33486982e-16j,  0.00000000e+00+7.66951701e-17j,
        1.14423775e-17+1.22464680e-16j,  0.00000000e+00+1.22464680e-16j])
>>> ivy.fft(np.exp(2j * np.pi * np.arange(8) / 8), 0, n=16)
ivy.array([-3.44509285e-16+1.14423775e-17j,  1.00000000e+00+5.02733949e+00j,
    8.00000000e+00-8.11483250e-16j,  1.00000000e+00-5.02733949e+00j,
    2.33486982e-16+1.22464680e-16j,  1.00000000e+00-1.49660576e+00j,
    0.00000000e+00+1.22464680e-16j,  1.00000000e+00-6.68178638e-01j,
    9.95799250e-17+2.33486982e-16j,  1.00000000e+00-1.98912367e-01j,
    0.00000000e+00+7.66951701e-17j,  1.00000000e+00+1.98912367e-01j,
    1.14423775e-17+1.22464680e-16j,  1.00000000e+00+6.68178638e-01j,
    0.00000000e+00+1.22464680e-16j,  1.00000000e+00+1.49660576e+00j])
>>> ivy.fft(np.exp(2j * np.pi * np.arange(8) / 8), 0, norm="ortho")
ivy.array([-1.21802426e-16+4.04549134e-18j,  2.82842712e+00-2.86902654e-16j,
    8.25501143e-17+4.32978028e-17j,  0.00000000e+00+4.32978028e-17j,
    3.52068201e-17+8.25501143e-17j,  0.00000000e+00+2.71158374e-17j,
    4.04549134e-18+4.32978028e-17j,  0.00000000e+00+4.32978028e-17j])
ivy.fft2(x, *, s=None, dim=(-2, -1), norm='backward', out=None)[source]#

Compute the 2-dimensional discrete Fourier Transform.

Parameters:
  • x (Union[Array, NativeArray]) – Input volume […,d_in,…], where d_in indicates the dimension that needs FFT2.

  • s (Optional[Sequence[int]], default: None) – sequence of ints, optional Shape (length of each transformed axis) of the output (s[0] refers to axis 0, s[1] to axis 1, etc.). This corresponds to n for fft(x, n). Along each axis, if the given shape is smaller than that of the input, the input is cropped. If it is larger, the input is padded with zeros. if s is not given, the shape of the input along the axes specified by axes is used.

  • dim (Sequence[int], default: (-2, -1)) – Axes over which to compute the FFT2. If not given, the last two axes are used. A repeated index in axes means the transform over that axis is performed multiple times. A one-element sequence means that a one-dimensional FFT is performed.

  • norm (str, default: 'backward') – Optional argument, “backward”, “ortho” or “forward”. Defaults to be “backward”. “backward” indicates no normalization. “ortho” indicates normalization by $frac{1}{sqrt{n}}$. “forward” indicates normalization by $frac{1}{n}$.

  • out (Optional[Array], default: None) – Optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – The result of the FFT2 operation.

Examples

>>> x = ivy.array([[0, 0, 0, 0, 0],
...                [1, 1, 1, 1, 1],
...                [2, 2, 2, 2, 2],
...                [3, 3, 3, 3, 3],
...                [4, 4, 4, 4, 4]])
>>> y = ivy.fft2(x)
>>> print(y)
ivy.array([[ 50.  +0.j        ,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ],
       [-12.5+17.20477401j,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ],
       [-12.5 +4.0614962j ,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ],
       [-12.5 -4.0614962j ,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ],
       [-12.5-17.20477401j,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ]])
ivy.generate_einsum_equation(dim)[source]#
ivy.get_interpolate_kernel(mode)[source]#
ivy.idct(x, /, *, type=2, n=None, axis=-1, norm=None, out=None)[source]#

Compute the 1D Inverse Discrete Cosine Transformation of a given signal.

Parameters:
  • x (Union[Array, NativeArray]) – The input signal.

  • type (Literal[1, 2, 3, 4], default: 2) – The type of the idct. Must be 1, 2, 3 or 4.

  • n (Optional[int], default: None) – The length of the transform. If n is less than the input signal length, then x is truncated, if n is larger then x is zero-padded.

  • axis (int, default: -1) – The axis to compute the IDCT along.

  • norm (Optional[Literal['ortho']], default: None) – The type of normalization to be applied. Must be either None or “ortho”.

  • out (Optional[Union[Array, NativeArray]], default: None) – optional output array, for writing the result to.

Return type:

Union[Array, NativeArray]

Returns:

  • ret – Array containing the transformed input.

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([8, 16, 24, 32, 40, 48, 56, 64])
>>> y = ivy.idct(x, type=2, n=None, norm='ortho')
>>> print(y)
ivy.array([ 79.49862671, -70.37691498,  30.00390816, -23.58938599,
        13.92713165, -10.078475  ,   5.19664812,  -1.95411837])
>>> x = ivy.array([[[8, 16, 24, 32], [40, 48, 56, 64]],
...                [[1,  2,  3,  4], [ 5,  6,  7,  8]]])
>>> y = ivy.idct(x, type=1, n=None, axis=0, norm=None)
>>> print(y)
ivy.array([[[ 9., 18., 27., 36.],
        [45., 54., 63., 72.]],
[[ 7., 14., 21., 28.],

[35., 42., 49., 56.]]])

>>> x = ivy.array([[ 8.1, 16.2, 24.3, 32.4],
...                [40.5, 48.6, 56.7, 64.8]])
>>> y = ivy.zeros((2, 4), dtype=ivy.float32)
>>> ivy.idct(x, type=1, n=None, norm=None, out=y)
>>> print(y)
ivy.array([[ 1.21500000e+02, -3.24000015e+01,  1.90734863e-06,
        -8.10000420e+00],
       [ 3.15899994e+02, -3.24000053e+01,  3.81469727e-06,
        -8.09999847e+00]])
>>> x = ivy.array([8., 16., 24., 32., 40., 48., 56., 64.])
>>> ivy.idct(x, type=4, n=None, norm=None, out=x)
>>> print(x)
ivy.array([279.4135742, -279.6779785, 128.3770599, -114.8719864,
           83.72109985, -79.52869415, 69.79182434, -68.72489166])

With one ivy.Container input:

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> y = ivy.idct(x, type=3, n=None, norm='ortho')
>>> print(y)
{
    a: ivy.array([1.01823380e+02, -5.15385818e+01, 1.36371466e-06, -5.38763905e+00,
                  0.00000000e+00, -1.60722279e+00, -8.80319249e-08,
                  -4.05617893e-01]),
    b: ivy.array([1.27279224e+01, -6.44232273e+00, 1.70464332e-07, -6.73454881e-01,
                  0.00000000e+00, -2.00902849e-01, -1.10039906e-08,
                  -5.07022366e-02])
}

With multiple ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> container_n = ivy.Container(a=9, b=4)
>>> container_type = ivy.Container(a=2, b=1)
>>> container_norm = ivy.Container(a="ortho", b=None)
>>> y = ivy.idct(x, type=container_type, n=container_n, norm=container_norm)
>>> print(y)
{
    a: ivy.array([86.29723358, -66.69506073, 9.93914604, 2.88008881,
                  -16.18951607, 18.06697273, -17.57439613, 11.68861485,
                  -4.41308832]),
    b: ivy.array([1.50000000e+01, -4.00000000e+00, -2.22044605e-16,
                  -1.00000000e+00])
}
ivy.ifft(x, dim, *, norm='backward', n=None, out=None)[source]#

Compute the one dimensional discrete Fourier transform given input at least 1-D input x.

Parameters:
  • x (Union[Array, NativeArray]) – Input volume […,d_in,…], where d_in indicates the dimension that needs IFFT.

  • dim (int) – The dimension along which to take the one dimensional IFFT.

  • norm (str, default: 'backward') – Optional argument, “backward”, “ortho” or “forward”. Defaults to be “backward”. “backward” indicates no normalization. “ortho” indicates normalization by $frac{1}{sqrt{n}}$. “forward” indicates normalization by $frac{1}{n}$.

  • n (Optional[Union[int, Tuple[int]]], default: None) – Optional argument indicating the sequence length, if given, the input would be padded with zero or truncated to length n before performing IFFT. Should be a integer greater than 1.

  • out (Optional[Array], default: None) – Optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – The result of the IFFT operation.

Examples

>>> ivy.ifft(np.exp(2j * np.pi * np.arange(8) / 8), 0)
ivy.array([-4.30636606e-17+1.43029718e-18j,  0.00000000e+00+1.53080850e-17j,
            1.43029718e-18+1.53080850e-17j,  0.00000000e+00+9.58689626e-18j,
            1.24474906e-17+2.91858728e-17j,  0.00000000e+00+1.53080850e-17j,
            2.91858728e-17+1.53080850e-17j,  1.00000000e+00-1.01435406e-16j])
>>> ivy.ifft(np.exp(2j * np.pi * np.arange(8) / 8), 0, n=16)
ivy.array([-2.15318303e-17+7.15148591e-19j,  6.25000000e-02+9.35378602e-02j,
            0.00000000e+00+7.65404249e-18j,  6.25000000e-02+4.17611649e-02j,
            7.15148591e-19+7.65404249e-18j,  6.25000000e-02+1.24320230e-02j,
            0.00000000e+00+4.79344813e-18j,  6.25000000e-02-1.24320230e-02j,
            6.22374531e-18+1.45929364e-17j,  6.25000000e-02-4.17611649e-02j,
            0.00000000e+00+7.65404249e-18j,  6.25000000e-02-9.35378602e-02j,
            1.45929364e-17+7.65404249e-18j,  6.25000000e-02-3.14208718e-01j,
            5.00000000e-01-5.07177031e-17j,  6.25000000e-02+3.14208718e-01j])
>>> ivy.ifft(np.exp(2j * np.pi * np.arange(8) / 8), 0, norm="ortho")
ivy.array([-1.21802426e-16+4.04549134e-18j,  0.00000000e+00+4.32978028e-17j,
            4.04549134e-18+4.32978028e-17j,  0.00000000e+00+2.71158374e-17j,
            3.52068201e-17+8.25501143e-17j,  0.00000000e+00+4.32978028e-17j,
            8.25501143e-17+4.32978028e-17j,  2.82842712e+00-2.86902654e-16j])
ivy.ifftn(x, s=None, axes=None, *, norm='backward', out=None)[source]#

Compute the N-dimensional inverse discrete Fourier Transform.

Parameters:
  • x (Union[Array, NativeArray]) – Input array of complex numbers.

  • s (Optional[Union[int, Tuple[int, ...]]], default: None) – Shape (length of transformed axis) of the output (s[0] refers to axis 0, s[1] to axis 1, etc.). If given shape is smaller than that of the input, the input is cropped. If larger, input is padded with zeros. If s is not given, shape of input along axes specified by axes is used.

  • axes (Optional[Union[int, Tuple[int, ...]]], default: None) – Axes over which to compute the IFFT. If not given, last len(s) axes are used, or all axes if s is also not specified. Repeated indices in axes means inverse transform over that axis is performed multiple times.

  • norm (str, default: 'backward') – Indicates direction of the forward/backward pair of transforms is scaled and with what normalization factor. “backward” indicates no normalization. “ortho” indicates normalization by $frac{1}{sqrt{n}}$. “forward” indicates normalization by $frac{1}{n}$.

  • out (Optional[Array], default: None) – Optional output array for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

out – The truncated or zero-padded input, transformed along the axes indicated by axes, or by a combination of s or x, as explained in the parameters section above.

Raises:
  • ValueError – If s and axes have different length.

  • IndexError – If an element of axes is larger than the number of axes of x.

Examples

>>> x = ivy.array([[0.24730653+0.90832391j, 0.49495562+0.9039565j,
...                 0.98193269+0.49560517j],
...                 [0.93280757+0.48075343j, 0.28526384+0.3351205j,
...                 0.2343787 +0.83528011j],
...                 [0.18791352+0.30690572j, 0.82115787+0.96195183j,
...                 0.44719226+0.72654048j]])
>>> y = ivy.ifftn(x)
>>> print(y)
ivy.array([[ 0.51476765+0.66160417j, -0.04319742-0.05411636j,
        -0.015561  -0.04216015j],
       [ 0.06310689+0.05347854j, -0.13392983+0.16052352j,
        -0.08371392+0.17252843j],
       [-0.0031429 +0.05421245j, -0.10446617-0.17747098j,
         0.05344324+0.07972424j]])
>>> x = ivy.array([[0.24730653+0.90832391j, 0.49495562+0.9039565j,
...                 0.98193269+0.49560517j],
...                 [0.93280757+0.48075343j, 0.28526384+0.3351205j,
...                 0.2343787 +0.83528011j],
...                 [0.18791352+0.30690572j, 0.82115787+0.96195183j,
...                 0.44719226+0.72654048j]])
>>> b = ivy.ifftn(x, s=[2, 1], axes=[0, 1], norm='ortho')
>>> print(b)
ivy.array([[ 0.8344667 +0.98222595j],
       [-0.48472244+0.30233797j]])
ivy.interp(x, xp, fp, left=None, right=None, period=None)[source]#
ivy.interpolate(x, size, /, *, mode='linear', scale_factor=None, recompute_scale_factor=None, align_corners=False, antialias=False, out=None)[source]#

Down/up samples the input to the given size. The algorithm used for interpolation is determined by mode.

Parameters:
  • x (Union[Array, NativeArray]) – Input array, Must have the shape [batch x channels x [optional depth] x [optional height] x width].

  • size (Union[Sequence[int], int]) – Output size.

  • mode (Literal['linear', 'bilinear', 'trilinear', 'nd', 'nearest', 'area', 'nearest_exact', 'tf_area', 'tf_bicubic', 'bicubic', 'mitchellcubic', 'lanczos3', 'lanczos5', 'gaussian'], default: 'linear') – Interpolation mode. Can be one of the following: - linear - bilinear - trilinear - nd - nearest - nearest-exact - area - tf_area - bicubic - tf_bicubic - mitchellcubic - lanczos3 - lanczos5 - gaussian

  • scale_factor (Optional[Union[int, Sequence[int]]], default: None) – Multiplier for spatial size that defines the output size (overwriting size).

  • recompute_scale_factor (Optional[bool], default: None) – If True, then scale_factor must be provided and scale_factor is used to compute the output size. The computed output size will be used to infer new scales for the interpolation. If recompute_scale_factor is False, then size or scale_factor will be used directly for interpolation.

  • align_corners (bool, default: False) – If True, the corner pixels of the input and output tensors are aligned, and thus preserving the values at the corner pixels. If False, the corner pixels are not aligned, and the interpolation uses edge value padding for out-of-boundary values.

  • out (Optional[Array], default: None) – Optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

resized array

ivy.max_pool1d(x, kernel, strides, padding, /, *, data_format='NWC', dilation=1, ceil_mode=False, out=None)[source]#

Compute a 1-D max pool given 3-D input x.

Parameters:
  • x (Union[Array, NativeArray]) – Input image [batch_size, w, d_in] if data_format is “NWC”.

  • kernel (Union[int, Tuple[int, ...]]) – Size of the kernel i.e., the sliding window for each dimension of input. [w].

  • strides (Union[int, Tuple[int, ...]]) – The stride of the sliding window for each dimension of input.

  • padding (Union[str, int, Tuple[int], List[Tuple[int, int]]]) – “SAME” or “VALID” indicating the algorithm; int, or list of tuple indicating the per-dimension paddings. (e.g. 2, [(1, 0)])

  • data_format (str, default: 'NWC') – “NWC” or “NCW”. Defaults to “NWC”.

  • dilation (Union[int, Tuple[int]], default: 1) – The stride between elements within a sliding window, must be > 0.

  • ceil_mode (bool, default: False) – If True, ceil is used instead of floor to compute the output shape. This ensures that every element in ‘x’ is covered by a sliding window.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.max_pool1d(x, 2, 2, 'SAME'))
ivy.array([[[ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]],
[[16., 17., 18., 19.],

[20., 21., 22., 23.]]])

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.max_pool1d(x, 2, 2, 'VALID'))
ivy.array([[[ 4.,  5.,  6.,  7.]],

[[16., 17., 18., 19.]]])

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.max_pool1d(x, 2, 2, [(1,0)], data_format="NCW", dilation=1, ceil_mode=True))
ivy.array([[[ 0.,  2.,  3.],
    [ 4.,  6.,  7.],
    [ 8., 10., 11.]],
[[12., 14., 15.],

[16., 18., 19.], [20., 22., 23.]]])

ivy.max_pool2d(x, kernel, strides, padding, /, *, data_format='NHWC', dilation=1, ceil_mode=False, out=None)[source]#

Compute a 2-D max pool given 4-D input x.

Parameters:
  • x (Union[Array, NativeArray]) – Input image [batch_size,h,w,d_in].

  • kernel (Union[int, Tuple[int, ...]]) – Size of the kernel i.e., the sliding window for each dimension of input. [h,w].

  • strides (Union[int, Tuple[int, ...]]) – The stride of the sliding window for each dimension of input.

  • padding (Union[str, int, Tuple[int], List[Tuple[int, int]]]) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (str, default: 'NHWC') – NHWC” or “NCHW”. Defaults to “NHWC”.

  • dilation (Union[int, Tuple[int, ...]], default: 1) – The stride between elements within a sliding window, must be > 0.

  • ceil_mode (bool, default: False) – If True, ceil is used instead of floor to compute the output shape. This ensures that every element in ‘x’ is covered by a sliding window.

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(12.).reshape((2, 1, 3, 2))
>>> print(ivy.max_pool2d(x, (2, 2), (1, 1), 'SAME'))
ivy.array([[[[ 2.,  3.],
         [ 4.,  5.],
         [ 4.,  5.]]],
[[[ 8., 9.],

[10., 11.], [10., 11.]]]])

>>> x = ivy.arange(48.).reshape((2, 4, 3, 2))
>>> print(ivy.max_pool2d(x, 3, 1, 'VALID'))
ivy.array([[[[16., 17.]],

[[22., 23.]]],

[[[40., 41.]],

[[46., 47.]]]])

ivy.max_pool3d(x, kernel, strides, padding, /, *, data_format='NDHWC', dilation=1, ceil_mode=False, out=None)[source]#

Compute a 3-D max pool given 5-D input x.

Parameters:
  • x (Union[Array, NativeArray]) – Input tensor [batch_size,d,h,w,d_in] if data_format is “NDHWC”.

  • kernel (Union[int, Tuple[int, ...]]) – Convolution filters [d,h,w].

  • strides (Union[int, Tuple[int, ...]]) – The stride of the sliding window for each dimension of input.

  • padding (Union[str, int, Tuple[int], List[Tuple[int, int]]]) – “SAME” or “VALID” indicating the algorithm; int, or list of tuple indicating the per-dimension paddings. (e.g. 2, [(1, 0), (0, 1), (1, 1)])

  • data_format (str, default: 'NDHWC') – “NDHWC” or “NCDHW”. Defaults to “NDHWC”.

  • dilation (Union[int, Tuple[int, ...]], default: 1) – The stride between elements within a sliding window, must be > 0.

  • ceil_mode (bool, default: False) – If True, ceil is used instead of floor to compute the output shape. This ensures that every element in ‘x’ is covered by a sliding window.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(48.).reshape((2, 3, 2, 2, 2))
>>> print(ivy.max_pool3d(x, 2, 2, 'VALID'))
ivy.array([[[[[14., 15.]]]],

[[[[38., 39.]]]]])

>>> print(ivy.max_pool3d(x, 2, 2, 'SAME'))
ivy.array([[[[[14., 15.]]],

[[[22., 23.]]]],

[[[[38., 39.]]],

[[[46., 47.]]]]])

ivy.max_unpool1d(input, indices, kernel_size, /, *, strides=None, padding=0, data_format='NCW')[source]#

Compute a 1-D max unpooling given the 1-D pooled input x and its indices.

Parameters:
  • input (Array) – Pooled input image [batch_size, w, d_in].

  • indices (Array) – Indices obtained from the corresponding max pooling operation.

  • kernel_size (Union[Tuple[int], int]) – Size of the kernel i.e., the sliding window for each dimension of input. [w].

  • strides (Optional[Union[int, Tuple[int]]], default: None) – The stride of the sliding window for each dimension of input.

  • padding (Union[int, Tuple[int]], default: 0) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (Optional[str], default: 'NCW') – NWC” or “NCW”. Defaults to “NWC”.

Return type:

Array

Returns:

ret – The result of the unpooling operation.

ivy.nearest_interpolate(x, dims, size, scale, exact)[source]#
ivy.pool(x, window_shape, pool_type, /, *, strides=None, padding='VALID', data_format=None, dilations=None, ceil_mode=False, out=None)[source]#

Perform an N-D pooling operation.

Parameters:
  • x (Union[Array, NativeArray]) – Input array to pool over.

  • window_shape (Union[int, Tuple[int], Tuple[int, int]]) – Shape of the pooling window.

  • pool_type (str) – Type of pooling operation, either ‘MAX’ or ‘AVG’.

  • strides (Optional[Union[int, Tuple[int], Tuple[int, int]]], default: None) – Strides of the pooling operation.

  • padding (str, default: 'VALID') – Padding type, either ‘VALID’ or ‘SAME’.

  • data_format (Optional[str], default: None) – Data format of the input and output data, either ‘NCHW’ or ‘NHWC’.

  • dilations (Optional[Union[int, Tuple[int], Tuple[int, int]]], default: None) – Dilation rate of the pooling operation.

  • ceil_mode (bool, default: False) – Whether to use ceil or floor for creating the output shape.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – The result of the pooling operation.

Examples

>>> x = ivy.arange(12.).reshape((2, 1, 3, 2))
>>> print(ivy.pool(x, (2, 2), 'MAX', strides=(1, 1), padding='SAME'))
ivy.array([[[[ 1.,  2.],
            [ 3.,  4.],
            [ 4.,  5.]]],
        [[[ 7.,  8.],
            [ 9., 10.],
            [10., 11.]]]])
>>> x = ivy.arange(48.).reshape((2, 4, 3, 2))
>>> print(ivy.pool(x, 3, 'AVG', strides=1, padding='VALID'))
ivy.array([[[[ 8.,  9.]],
        [[14., 15.]]],
        [[[32., 33.]],
        [[38., 39.]]]])
ivy.reduce_window(operand, init_value, computation, window_dimensions, /, *, window_strides=1, padding='VALID', base_dilation=1, window_dilation=1)[source]#

Apply a reduction function to all elements in each window of an array.

Parameters:
  • operand (Union[Array, NativeArray]) – An array representing the base area on which the window is going to slide over.

  • init_value (Union[int, float]) – The starting value for the reduction.

  • computation (Callable) – The reduction function to apply to elements in each window.

  • window_dimensions (Union[int, Sequence[int]]) – A sequence containing the window dimensions.

  • window_strides (Union[int, Sequence[int]], default: 1) – A sequence containing the window strides.

  • padding (Union[str, int, Sequence[Tuple[int, int]]], default: 'VALID') – Either the string ‘SAME’ (padding with zeros evenly), the string ‘VALID’ (no padding), or a sequence of n (low, high) integer pairs that give the padding to apply before and after each spatial dimension.

  • base_dilation (Union[int, Sequence[int]], default: 1) – A sequence containing the base dilation values.

  • window_dilation (Union[int, Sequence[int]], default: 1) – A sequence containing the window dilation values.

Return type:

Array

Returns:

ret – The result of the pooling-like operation.

Examples

>>> x = ivy.array([[1, 2, 3, 4],
>>>                [5, 6, 7, 8],
>>>                [9, 10, 11, 12]])
>>> ivy.reduce_window(x, 0, ivy.add, (2, 2))
ivy.array([[14, 18, 22], [30, 34, 38]])
ivy.rfft(x, /, *, n=None, axis=-1, norm='backward', out=None)[source]#

Compute the one-dimensional discrete Fourier transform for real-valued input.

Note

Applying the one-dimensional inverse discrete Fourier transform for real-valued input to the output of this function must return the original (i.e., non-transformed) input array within numerical accuracy (i.e., irfft(rfft(x)) == x), provided that the transform and inverse transform are performed with the same arguments (axis and normalization mode) and consistent length.

Note

If the input a contains an imaginary part, it is silently discarded.

Parameters:
  • x (Union[Array, NativeArray]) – input array. Must have a real-valued floating-point data type.

  • n (Optional[int], default: None) – length of the transformed axis of the input. If - n is greater than the length of the input array, the input array is zero-padded to length n. - n is less than the length of the input array, the input array is trimmed to length n. - n is not provided, the length of the transformed axis of the output must equal the length of the input along the axis specified by axis. Default is None.

  • axis (int, default: -1) – axis (dimension) over which to compute the Fourier transform. If not set, the last axis (dimension) is used. Default is -1.

  • norm (Literal['backward', 'ortho', 'forward'], default: 'backward') – normalization mode. Should be one of the following modes: - ‘backward’: no normalization. - ‘ortho’: normalize by 1/sqrt(n) (i.e., make the FFT orthonormal). - ‘forward’: normalize by 1/n. Default is backward.

  • out (Optional[Array], default: None) – Optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret – an array transformed along the axis (dimension) indicated by axis. The returned array must have a complex-valued floating-point data type determined by Type Promotion Rules.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/)

  • API_specification/generated/array_api.max.html>`_

  • in the standard.

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([0,1,2])
>>> y = ivy.rfft(x)
>>> print(y)
ivy.array([ 3. +0.j       , -1.5+0.8660254j])
>>> x = ivy.array([2.3,3.14,7.2])
>>> y = ivy.zeros(2)
>>> ivy.rfft(x, out=y)
>>> print(x)
ivy.array([2.29999995, 3.1400001 , 7.19999981])
>>> x = ivy.array([-1.2, 3.4, -5.6])
>>> ivy.rfft(x, n=4, out=x)
>>> print(x)
ivy.array([ -3.3999999+0.j ,   4.3999996-3.4j, -10.2      +0.j ],
      dtype=complex64)

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0.,1.,2.]),
...                   b=ivy.array([3.,4.,5.]))
>>> y = ivy.rfft(x)
>>> print(y)
{
    a: ivy.array([3.+0.j, -1.5+0.8660254j]),
    b: ivy.array([12.+0.j, -1.5+0.8660254j])
}
ivy.rfftn(x, s=None, axes=None, *, norm=None, out=None)[source]#

Compute the N-dimensional discrete Fourier Transform for real input.

Parameters:
  • x (array_like) – Input array, taken to be real.

  • s (sequence of ints, optional) – Shape (length along each transformed axis) to use from the input. (s[0] refers to axis 0, s[1] to axis 1, etc.). The final element of s corresponds to n for rfft(x, n), while for the remaining axes, it corresponds to n for fft(x, n). Along any axis, if the given shape is smaller than that of the input, the input is cropped. If it is larger, the input is padded with zeros. If s is not given, the shape of the input along the axes specified by axes is used.

  • axes (sequence of ints, optional) – Axes over which to compute the FFT. If not given, the last len(s) axes are used, or all axes if s is also not specified.

  • norm ({"backward", "ortho", "forward"}, optional) – Normalization mode. Default is “backward”. Indicates which direction of the forward/backward pair of transforms is scaled and with what normalization factor.

  • out (array_like, optional) – Optional output array to store the result of the computation. The shape and dtype of this array must match the expected output.

Return type:

Array

Returns:

out (complex ndarray) – The truncated or zero-padded input, transformed along the axes indicated by axes or by a combination of s and a, as explained in the parameters section above. The length of the last axis transformed will be s[-1] // 2 + 1, while the remaining transformed axes will have lengths according to s, or unchanged from the input.

Raises:
  • ValueError – If s and axes have different lengths.

  • IndexError – If an element of axes is larger than the number of axes of a.

Examples

>>> x = ivy.array([1, 2, 3, 4], dtype=ivy.float32)
>>> result = ivy.rfftn(x, s=(4,), axes=(0,))
>>> print(result)
ivy.array([10.+0.j, -2.+2.j, -2.+0.j])
>>> x = ivy.array([[1, 2, 3], [4, 5, 6]], dtype=ivy.float32)
>>> result = ivy.rfftn(x, s=(3, 4), axes=(0, 1))
>>> print(result)
ivy.array([[21.         +0.j        , -4.         -7.j        ,
         7.         +0.j        ],
       [-1.5       -12.99038106j, -5.33012702 +2.23205081j,
        -0.5        -4.33012702j],
       [-1.5       +12.99038106j,  3.33012702 -1.23205081j,
        -0.5        +4.33012702j]])
ivy.rnn(step_function, inputs, initial_states, /, *, go_backwards=False, mask=None, constants=None, unroll=False, input_length=None, time_major=False, zero_output_for_mask=False, return_all_outputs=True)[source]#

Iterate over the time dimension of a tensor.

Parameters:
  • step_function (Callable) – RNN step function.

  • inputs (Array) – Array of temporal data of shape (samples, time, …).

  • initial_states (List[Array]) – Array with shape (samples, state_size).

  • go_backwards (bool, default: False) – If True, do the iteration over the time dimension in reverse order and return the reversed sequence.

  • mask (Optional[Array], default: None) – Binary array with shape (samples, time, 1), with a zero for every element that is masked.

  • constants (Optional[Array], default: None) – List of constant values passed at each step.

  • unroll (bool, default: False) – Whether to use a pythonic while loop or ivy.while_loop

  • input_length (Optional[int], default: None) – An integer or 1-D array, depending on whether the time dimension is fixed-length. In case of variable length input, it is used for masking in case there is no mask specified.

  • time_major (bool, default: False) – If True, the inputs and outputs will be in shape (timesteps, batch, …) whereas in the False case, it will be (batch, timesteps, …).

  • zero_output_for_mask (bool, default: False) – If True, the otput for masked timestep will be zeros, whereas in the False case, output from previous timestep is returned

  • return_all_outputs (bool, default: True) – If True, return the recurrent outputs for all timesteps in the sequence. If False, only return the output for the last timestep.

Returns:

  • ret – A tuple of - the latest output of the rnn of shape (samples, …) - the output of the rnn of shape (samples, time, …) if

    return_all_outputs=True else (samples, 1, …)

    • list of tensors, latest states returned by the step funciton, of shape (samples, …)

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

ivy.sliding_window(input, kernel_size, /, *, stride=1, dilation=1, padding='VALID')[source]#

Slide a window of specified dimension over all elements of an array.

Parameters:
  • input (Union[Array, NativeArray]) – An array representing the base area on which the window is going to slide over.

  • window_size – Size of the sliding window for each dimension of the input.

  • stride (Union[int, Tuple[int, int]], default: 1) – The stride of the sliding window for each dimension of input

  • padding (Union[str, int, Tuple[int, int]], default: 'VALID') – Either the string ‘SAME’ (padding with zeros evenly), the string ‘VALID’ (no padding), or a sequence of n (low, high) integer pairs that give the padding to apply before and after each spatial dimension.

  • dilation (Union[int, Tuple[int, int]], default: 1) – The stride between elements within a sliding window, must be > 0.

Return type:

Array

Returns:

ret – The result of the sliding window operation.

Examples

>>> x = ivy.array([[1, 2, 3, 4],
>>>                [5, 6, 7, 8],
>>>                [9, 10, 11, 12]])
>>> ivy.sliding_window(x, (2, 2))
ivy.array([[[ 1,  2,  5,  6],
            [ 2,  3,  6,  7],
            [ 3,  4,  7,  8]],

[[ 5, 6, 9, 10], [ 6, 7, 10, 11], [ 7, 8, 11, 12]]])

ivy.stft(signals, frame_length, frame_step, /, *, fft_length=None, window_fn=None, pad_end=False, name=None, out=None)[source]#

ivy.Container static method variant of ivy.stft.

This method simply wraps the function, and so the docstring for ivy.stft also applies to this method with minimal changes.

Parameters:
  • signals (Union[Array, NativeArray]) – Input Arrays.

  • frame_length (int) – An integer scalar Tensor. The window length in samples.

  • frame_step (int) – An integer scalar Tensor. The number of samples to step.

  • fft_length (Optional[int], default: None) – An integer scalar Tensor. The size of the FFT to apply. If not provided, uses the smallest power of 2 enclosing frame_length.

  • optional – An integer scalar Tensor. The size of the FFT to apply. If not provided, uses the smallest power of 2 enclosing frame_length.

  • window_fn (Optional[Callable], default: None) – A callable that takes a window length and a dtype keyword argument and returns a [window_length] Tensor of samples in the provided datatype. If set to None, no windowing is used.

  • optional – A callable that takes a window length and a dtype keyword argument and returns a [window_length] Tensor of samples in the provided datatype. If set to None, no windowing is used.

  • pad_end (bool, default: False) – Whether to pad the end of signals with zeros when the provided frame length and step produces a frame that lies partially past its end.

  • optional – Whether to pad the end of signals with zeros when the provided frame length and step produces a frame that lies partially past its end.

  • name (Optional[str], default: None) – An optional name for the operation.

  • optional – An optional name for the operation.

  • out (Optional[Array], default: None) – Optional output array for writing the result.

  • optional – Optional output array for writing the result.

Return type:

Array

Returns:

ret – A […, frames, fft_unique_bins] Tensor of complex64/complex128 STFT values where fft_unique_bins is fft_length // 2 + 1 (the unique components of the FFT).

This should have hopefully given you an overview of the layers submodule, if you have any questions, please feel free to reach out on our discord!