Skip to content

Commit

Permalink
Added Bernoulli, DFT, Expand in the Experimental API (#9849)
Browse files Browse the repository at this point in the history
* removed .idea files

* added DFT to experimental API

* resolve test_dft

* added Expand to experimental API(all tests passing)

* added PRELU to experimental
  • Loading branch information
Aarsh2001 authored Jan 27, 2023
1 parent 0f0f5a4 commit cd1a4ea
Show file tree
Hide file tree
Showing 17 changed files with 900 additions and 0 deletions.
73 changes: 73 additions & 0 deletions ivy/array/experimental/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,76 @@ def ifft(

def embedding(self, indices, /, *, max_norm=None, out=None):
return ivy.embedding(self._data, indices, max_norm=max_norm, out=out)

def dft(
self,
/,
*,
axis: int = 1,
inverse: bool = False,
onesided: bool = False,
dft_length: Optional[Union[int, Tuple[int]]] = None,
norm: Optional[str] = "backward",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""
Computes the discrete Fourier transform of input.
Parameters
----------
self
Input volume *[...,d_in,...]*,
where d_in indicates the dimension that needs FFT.
axis
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
Whether to perform the inverse discrete fourier transform.
By default this value is set to False.
onesided
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
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
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 output array, for writing the result to. It must
have a shape that the inputs broadcast to.
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.
"""
return ivy.dft(
self._data,
axis=axis,
inverse=inverse,
onesided=onesided,
dft_length=dft_length,
norm=norm,
out=out,
)
22 changes: 22 additions & 0 deletions ivy/array/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,25 @@ def hsplit(
[14., 15.]]))
"""
return ivy.hsplit(self._data, indices_or_sections, out=out)

def expand(
self: ivy.Array,
shape: Union[ivy.Shape, ivy.NativeShape],
/,
*,
device: Optional[Union[ivy.Device, ivy.NativeDevice]] = None,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""
Parameters
----------
shape
device
out
Returns
-------
"""
return ivy.expand(self._data, shape, device=device, out=out)
55 changes: 55 additions & 0 deletions ivy/array/experimental/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,58 @@ def poisson(
seed=seed,
out=out,
)

def bernoulli(
self: ivy.Array,
*,
logits: Optional[Union[float, ivy.Array, ivy.NativeArray]] = None,
shape: Optional[Union[ivy.Shape, ivy.NativeShape]] = None,
device: Optional[Union[ivy.Device, ivy.NativeDevice]] = None,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype]] = None,
seed: Optional[int] = None,
out: Optional[ivy.Array] = None,
):
"""
Parameters
----------
self
An N-D Array representing the probability of a 1 event.
Each entry in the Array parameterizes an independent Bernoulli
distribution. Only one of logits or probs should be passed in
logits
An N-D Array representing the log-odds of a 1 event.
Each entry in the Array parameterizes an independent Bernoulli
distribution where the probability of an event is sigmoid
(logits). Only one of logits or probs should be passed in.
shape
If the given shape is, e.g '(m, n, k)', then 'm * n * k' samples are drawn.
(Default value = 'None', where 'ivy.shape(logits)' samples are drawn)
device
device on which to create the array 'cuda:0', 'cuda:1', 'cpu' etc.
(Default value = None).
dtype
output array data type. If ``dtype`` is ``None``, the output array data
type will be the default floating-point data type. Default ``None``
seed
A python integer. Used to create a random seed distribution
out
optional output array, for writing the result to. It must
have a shape that the inputs broadcast to.
Returns
-------
ret
Drawn samples from the Bernoulli distribution
"""
return ivy.bernoulli(
self,
logits=logits,
shape=shape,
device=device,
dtype=dtype,
seed=seed,
out=out,
)
100 changes: 100 additions & 0 deletions ivy/container/experimental/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,3 +1258,103 @@ def embedding(
map_sequences=map_sequences,
out=out,
)

@staticmethod
def static_dft(
x: Union[ivy.Array, ivy.NativeArray, ivy.Container],
/,
*,
axis: int = 1,
inverse: bool = False,
onesided: bool = False,
dft_length: Optional[Union[int, Tuple[int]]] = None,
norm: Optional[str] = "backward",
key_chains: Optional[Union[List[str], Dict[str, str]]] = None,
to_apply: bool = True,
prune_unapplied: bool = False,
map_sequences: bool = False,
out: Optional[ivy.Array] = None,
) -> ivy.Container:
"""
Parameters
----------
x
axis
inverse
onesided
dft_length
norm
key_chains
to_apply
prune_unapplied
map_sequences
out
Returns
-------
"""

return ContainerBase.cont_multi_map_in_function(
"dft",
x,
axis=axis,
inverse=inverse,
onesided=onesided,
dft_length=dft_length,
norm=norm,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
)

def dft(
self: Union[ivy.Array, ivy.NativeArray, ivy.Container],
/,
*,
axis: int = 1,
inverse: bool = False,
onesided: bool = False,
dft_length: Optional[Union[int, Tuple[int]]] = None,
norm: Optional[str] = "backward",
key_chains: Optional[Union[List[str], Dict[str, str]]] = None,
to_apply: bool = True,
prune_unapplied: bool = False,
map_sequences: bool = False,
out: Optional[ivy.Array] = None,
) -> ivy.Container:
"""
Parameters
----------
axis
inverse
onesided
dft_length
norm
key_chains
to_apply
prune_unapplied
map_sequences
out
Returns
-------
"""
return self.static_dft(
self,
axis=axis,
inverse=inverse,
onesided=onesided,
dft_length=dft_length,
norm=norm,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
)
64 changes: 64 additions & 0 deletions ivy/container/experimental/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2289,3 +2289,67 @@ def broadcast_shapes(
"""
return self.static_broadcast_shapes(self, out=out)

@staticmethod
def static_expand(
x: Union[ivy.Array, ivy.NativeArray, ivy.Container],
shape: Union[ivy.Shape, ivy.NativeShape],
/,
*,
device: Optional[Union[ivy.Device, ivy.NativeDevice]] = None,
key_chains: Optional[Union[List[str], Dict[str, str]]] = None,
to_apply: bool = True,
prune_unapplied: bool = False,
map_sequences: bool = False,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
Parameters
----------
x
shape
device
out
key_chains
to_apply
prune_unapplied
map_sequences
Returns
-------
"""
return ContainerBase.cont_multi_map_in_function(
"expand",
x,
shape,
device=device,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
)

def expand(
self: Union[ivy.Array, ivy.NativeArray, ivy.Container],
shape: Union[ivy.Shape, ivy.NativeShape],
/,
*,
device: Optional[Union[ivy.Device, ivy.NativeDevice]] = None,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
Parameters
----------
shape
device
out
Returns
-------
"""
return self.static_expand(self, shape, device=device, out=out)
Loading

0 comments on commit cd1a4ea

Please sign in to comment.