Skip to content

Commit

Permalink
Conv1d (#2103) (#2246)
Browse files Browse the repository at this point in the history
Co-authored-by: Rushali <[email protected]>
  • Loading branch information
YushaArif99 and rush2406 authored Jul 27, 2022
1 parent e13d89c commit 7ed9d72
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
54 changes: 54 additions & 0 deletions ivy/array/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,57 @@ def depthwise_conv2d(
return ivy.depthwise_conv2d(
self._data, filters, strides, padding, data_format, dilations, out=out
)

def conv1d(
self: ivy.Array,
filters: Union[ivy.Array, ivy.NativeArray],
strides: int,
padding: str,
data_format: str = "NWC",
dilations: int = 1,
*,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""
ivy.Array instance method variant of ivy.conv1d. This method simply
wraps the function, and so the docstring for ivy.conv1d also applies
to this method with minimal changes.
Parameters
----------
x
Input image *[batch_size,w,d_in]*.
filters
Convolution filters *[fw,d_in,d_out]*.
strides
The stride of the sliding window for each dimension of input.
padding
SAME" or "VALID" indicating the algorithm, or list indicating the
per-dimension paddings.
data_format
NWC" or "NCW". Defaults to "NWC".
dilations
The dilation factor for each dimension of input. (Default value = 1)
out
optional output array, for writing the result to. It must have a shape that
the inputs broadcast to.
Returns
-------
ret
The result of the convolution operation.
Examples
--------
>>> x = ivy.array([[[1., 2.], [3., 4.], [6., 7.], [9., 11.]]]) # NWC
>>> filters = ivy.array([[[0., 1.], [1., 1.]]]) # WIO (I == C)
>>> result = x.conv1d(filters, strides=(1,), padding='VALID')
>>> print(result)
ivy.array([[[ 2., 3.], \
[ 4., 7.], \
[ 7., 13.], \
[11., 20.]]])
"""
return ivy.conv1d(
self._data, filters, strides, padding, data_format, dilations, out=out
)
122 changes: 122 additions & 0 deletions ivy/container/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,125 @@ def depthwise_conv2d(
return self.static_depthwise_conv2d(
self, filters, strides, padding, data_format, dilations, out=out
)

@staticmethod
def static_conv1d(
x: ivy.Container,
filters: Union[ivy.Array, ivy.NativeArray],
strides: int,
padding: str,
data_format: str = "NWC",
dilations: int = 1,
*,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
ivy.Container static method variant of ivy.conv1d. This method simply
wraps the function, and so the docstring for ivy.conv1d also applies
to this method with minimal changes.
Parameters
----------
x
Input image *[batch_size,w, d_in]*.
filters
Convolution filters *[fw,d_in, d_out]*. (d_in must be the same as d from x)
strides
The stride of the sliding window for each dimension of input.
padding
"SAME" or "VALID" indicating the algorithm, or list indicating the
per-dimension paddings.
data_format
"NWC" or "NCW". Defaults to "NWC".
dilations
The dilation factor for each dimension of input. (Default value = 1)
out
optional output container, for writing the result to. It must have a shape
that the inputs broadcast to.
Returns
-------
ret
The result of the convolution operation.
Examples
--------
>>> x = ivy.Container(a=ivy.array([[[2., 3., 4.], [5., 6., 7.]]]), \
b =ivy.array([[[7., 8., 9.], [10., 11., 12]]]))
>>> filters = ivy.array([[[0., 0.5, 1.], [0.25, 0.5, 0.75], [-0.5, 0., 0.5 ]]])
>>> result= ivy.Container.static_conv1d(x,filters,strides=(1,),padding='VALID')
>>> print(result)
{
a: ivy.array([[[-1.25, 2.5, 6.25], \
[-2., 5.5, 13.]]]), \
b: ivy.array([[[-2.5, 7.5, 17.5], \
[-3.25, 10.5, 24.2]]])
}
"""
return ContainerBase.multi_map_in_static_method(
"conv1d",
x,
filters=filters,
strides=strides,
padding=padding,
data_format=data_format,
dilations=dilations,
out=out,
)

def conv1d(
self: ivy.Container,
filters: Union[ivy.Array, ivy.NativeArray],
strides: int,
padding: str,
data_format: str = "NWC",
dilations: int = 1,
*,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
ivy.Container instance method variant of ivy.conv1d. This method simply
wraps the function, and so the docstring for ivy.conv1d also applies
to this method with minimal changes.
Parameters
----------
x
Input image *[batch_size,w, d_in]*.
filters
Convolution filters *[fw,d_in, d_out]*. (d_in must be the same as d from x)
strides
The stride of the sliding window for each dimension of input.
padding
"SAME" or "VALID" indicating the algorithm, or list indicating the
per-dimension paddings.
data_format
"NWC" or "NCW". Defaults to "NWC".
dilations
The dilation factor for each dimension of input. (Default value = 1)
out
optional output container, for writing the result to. It must have a shape
that the inputs broadcast to.
Returns
-------
ret
The result of the convolution operation.
Examples
--------
>>> x = ivy.Container(a=ivy.array([[[2., 3., 4.], [5., 6., 7.]]]), \
b =ivy.array([[[7., 8., 9.], [10., 11., 12]]]))
>>> filters = ivy.array([[[0., 0.5, 1.], [0.25, 0.5, 0.75], [-0.5, 0., 0.5 ]]])
>>> result= x.conv1d(filters, strides=(1,), padding='VALID')
>>> print(result)
{
a: ivy.array([[[-1.25, 2.5, 6.25], \
[-2., 5.5, 13.]]]), \
b: ivy.array([[[-2.5, 7.5, 17.5], \
[-3.25, 10.5, 24.2]]])
}
"""
return self.static_conv1d(
self, filters, strides, padding, data_format, dilations, out=out
)
24 changes: 24 additions & 0 deletions ivy/functional/ivy/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,36 @@ def conv1d(
Examples
--------
With :code:`ivy.Array` input:
>>> x = ivy.asarray([[[0.], [3.], [0.]]]) #NWC
>>> filters = ivy.array([[[0.]], [[1.]], [[0.]]]) #WIO
>>> result = ivy.conv1d(x, filters, (1,), 'SAME', 'NWC', (1,))
>>> print(result)
ivy.array([[[0.], [3.], [0.]]])
With :code:`ivy.NativeArray` input:
>>> x = ivy.native_array([[[1., 3.], [2., 4.], [5., 7]]])
>>> filters = ivy.native_array([[[0., 1.], [1., 0.]]])
>>> result = ivy.conv1d(x, filters, strides= (2,), padding='VALID')
>>> print(result)
ivy.array([[[3., 1.], \
[7., 5.]]])
With a mix of :code:`ivy.Array` and :code:`ivy.Container` inputs:
>>> x = ivy.Container(a= ivy.array([[[1.2, 3.1, 4.8], [5.9, 2.2, 3.3],\
[10.8, 7.6, 4.9], [6.1, 2.2, 9.5]]]), \
b= ivy.array([[[8.8, 7.7, 6.6], [1.1, 2.2, 3.5]]]))
>>> filters = ivy.array([[[1., 0., 1.], [0., 1., 0.], [1., 1., 0.]]])
>>> result = ivy.conv1d(x, filters, strides= 3, padding='VALID')
>>> print(result)
{
a: ivy.array([[[6., 7.9, 1.2], \
[15.6, 11.7, 6.1]]]), \
b: ivy.array([[[15.4, 14.3, 8.8]]])
}
"""
return current_backend(x).conv1d(
x, filters, strides, padding, data_format, dilations, out=out
Expand Down

0 comments on commit 7ed9d72

Please sign in to comment.