forked from ivy-llc/ivy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: nathzi1505 <[email protected]>
- Loading branch information
1 parent
26b693d
commit f7aaf50
Showing
8 changed files
with
370 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,50 @@ | ||
# global | ||
import abc | ||
from typing import Optional, Union | ||
|
||
# local | ||
import ivy | ||
|
||
|
||
class _ArrayWithLossesExperimental(abc.ABC): | ||
pass | ||
def l1_loss( | ||
self: Union[ivy.Array, ivy.NativeArray], | ||
target: Union[ivy.Array, ivy.NativeArray], | ||
/, | ||
*, | ||
reduction: Optional[str] = "mean", | ||
out: Optional[ivy.Array] = None, | ||
) -> ivy.Array: | ||
""" | ||
ivy.Array instance method variant of ivy.l1_loss. This method simply wraps the | ||
function, and so the docstring for ivy.l1_loss also applies to this method with | ||
minimal changes. | ||
Parameters | ||
---------- | ||
self | ||
input array. | ||
target | ||
input array containing the targeted values. | ||
reduction | ||
``'mean'``: The output will be averaged. | ||
``'sum'``: The output will be summed. | ||
``'none'``: No reduction will be applied to the output. Default: ``'mean'``. | ||
out | ||
optional output array, for writing the result to. It must have a shape that | ||
the inputs broadcast to. | ||
Returns | ||
------- | ||
ret | ||
The L1 loss between the input array and the targeticted values. | ||
Examples | ||
-------- | ||
>>> x = ivy.array([1.0, 2.0, 3.0]) | ||
>>> y = ivy.array([0.7, 1.8, 2.9]) | ||
>>> z = x.l1_loss(y) | ||
>>> print(z) | ||
ivy.array(0.20000000000000004) | ||
""" | ||
return ivy.l1_loss(self._data, target, reduction=reduction, out=out) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
# global | ||
from typing import Optional, Union, List, Dict | ||
|
||
# local | ||
import ivy | ||
from ivy.data_classes.container.base import ContainerBase | ||
|
||
|
||
class _ContainerWithLossesExperimental(ContainerBase): | ||
@staticmethod | ||
def _static_l1_loss( | ||
input: Union[ivy.Container, ivy.Array, ivy.NativeArray], | ||
target: Union[ivy.Container, ivy.Array, ivy.NativeArray], | ||
/, | ||
*, | ||
reduction: Optional[Union[str, ivy.Container]] = "mean", | ||
key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, | ||
to_apply: Union[bool, ivy.Container] = True, | ||
prune_unapplied: Union[bool, ivy.Container] = False, | ||
map_sequences: Union[bool, ivy.Container] = False, | ||
out: Optional[ivy.Container] = None, | ||
) -> ivy.Container: | ||
""" | ||
ivy.Container static method variant of ivy.l1_loss. This method simply wraps the | ||
function, and so the docstring for ivy.l1_loss also applies to this method with | ||
minimal changes. | ||
Parameters | ||
---------- | ||
input | ||
input array or container. | ||
target | ||
input array or container containing the targeted values. | ||
reduction | ||
``'mean'``: The output will be averaged. | ||
``'sum'``: The output will be summed. | ||
``'none'``: No reduction will be applied to the output. Default: ``'mean'``. | ||
key_chains | ||
The key-chains to apply or not apply the method to. Default is ``None``. | ||
to_apply | ||
If input, the method will be applied to key_chains, otherwise key_chains | ||
will be skipped. Default is ``input``. | ||
prune_unapplied | ||
Whether to prune key_chains for which the function was not applied. | ||
Default is ``False``. | ||
map_sequences | ||
Whether to also map method to sequences (lists, tuples). | ||
Default is ``False``. | ||
out | ||
optional output container, for writing the result to. It must have a shape | ||
that the inputs broadcast to. | ||
Returns | ||
------- | ||
ret | ||
The L1 loss between the input array and the targeted values. | ||
Examples | ||
-------- | ||
With :class:`ivy.Container` inputs: | ||
>>> x = ivy.Container(a=ivy.array([1, 2, 3]), b=ivy.array([4, 5, 6])) | ||
>>> y = ivy.Container(a=ivy.array([2, 2, 2]), b=ivy.array([5, 5, 5])) | ||
>>> z = ivy.Container.static_l1_loss(x, y) | ||
>>> print(z) | ||
{ | ||
a: ivy.array(1.), | ||
b: ivy.array(0.) | ||
} | ||
With a mix of :class:`ivy.Array` and :class:`ivy.Container` inputs: | ||
>>> x = ivy.array([1, 2, 3]) | ||
>>> y = ivy.Container(a=ivy.array([2, 2, 2]), b=ivy.array([5, 5, 5])) | ||
>>> z = ivy.Container.static_l1_loss(x, y) | ||
>>> print(z) | ||
{ | ||
a: ivy.array(1.), | ||
b: ivy.array(4.) | ||
} | ||
""" | ||
return ContainerBase.cont_multi_map_in_function( | ||
"l1_loss", | ||
input, | ||
target, | ||
reduction=reduction, | ||
key_chains=key_chains, | ||
to_apply=to_apply, | ||
prune_unapplied=prune_unapplied, | ||
map_sequences=map_sequences, | ||
out=out, | ||
) | ||
|
||
def l1_loss( | ||
self: ivy.Container, | ||
target: Union[ivy.Container, ivy.Array, ivy.NativeArray], | ||
/, | ||
*, | ||
reduction: Optional[Union[str, ivy.Container]] = "mean", | ||
key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, | ||
to_apply: Union[bool, ivy.Container] = True, | ||
prune_unapplied: Union[bool, ivy.Container] = False, | ||
map_sequences: Union[bool, ivy.Container] = False, | ||
out: Optional[ivy.Container] = None, | ||
) -> ivy.Container: | ||
""" | ||
ivy.Container instance method variant of ivy.l1_loss. This method simply wraps | ||
the function, and so the docstring for ivy.l1_loss also applies to this method | ||
with minimal changes. | ||
Parameters | ||
---------- | ||
self | ||
input container. | ||
target | ||
input array or container containing the targeticted values. | ||
reduction | ||
``'mean'``: The output will be averaged. | ||
``'sum'``: The output will be summed. | ||
``'none'``: No reduction will be applied to the output. Default: ``'mean'``. | ||
key_chains | ||
The key-chains to apply or not apply the method to. Default is ``None``. | ||
to_apply | ||
If input, the method will be applied to key_chains, otherwise key_chains | ||
will be skipped. Default is ``input``. | ||
prune_unapplied | ||
Whether to prune key_chains for which the function was not applied. | ||
Default is ``False``. | ||
map_sequences | ||
Whether to also map method to sequences (lists, tuples). | ||
Default is ``False``. | ||
out | ||
optional output container, for writing the result to. It must have a shape | ||
that the inputs broadcast to. | ||
Returns | ||
------- | ||
ret | ||
The L1 loss between the input array and the targeticted values. | ||
Examples | ||
-------- | ||
>>> x = ivy.Container(a=ivy.array([1, 2, 3]), b=ivy.array([4, 5, 6])) | ||
>>> y = ivy.Container(a=ivy.array([2, 2, 2]), b=ivy.array([5, 5, 5])) | ||
>>> z = x.l1_loss(y) | ||
>>> print(z) | ||
{ | ||
a: ivy.array(1.), | ||
b: ivy.array(0.) | ||
} | ||
""" | ||
return self._static_l1_loss( | ||
self, | ||
target, | ||
reduction=reduction, | ||
key_chains=key_chains, | ||
to_apply=to_apply, | ||
prune_unapplied=prune_unapplied, | ||
map_sequences=map_sequences, | ||
out=out, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# global | ||
from typing import Optional | ||
import paddle | ||
import paddle.nn.functional as F | ||
|
||
# local | ||
from ivy.func_wrapper import with_unsupported_device_and_dtypes | ||
from . import backend_version | ||
|
||
|
||
@with_unsupported_device_and_dtypes( | ||
{ | ||
"2.5.1 and below": { | ||
"cpu": ( | ||
"float16", | ||
"int8", | ||
"int16", | ||
"int32", | ||
"int64", | ||
"uint8", | ||
"complex64", | ||
"complex128", | ||
"bool", | ||
) | ||
} | ||
}, | ||
backend_version, | ||
) | ||
def l1_loss( | ||
input: paddle.Tensor, | ||
target: paddle.Tensor, | ||
/, | ||
*, | ||
reduction: Optional[str] = "mean", | ||
) -> paddle.Tensor: | ||
return F.l1_loss(input, target, reduction=reduction) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Optional | ||
import torch | ||
from ivy.func_wrapper import with_unsupported_dtypes | ||
from . import backend_version | ||
|
||
# Assuming ivy and backend_version are imported and defined properly | ||
|
||
|
||
@with_unsupported_dtypes( | ||
{"2.0.1 and below": ("unit8", "int8", "int16", "int32", "int64", "bool")}, | ||
backend_version, | ||
) | ||
def l1_loss( | ||
input: torch.Tensor, | ||
target: torch.Tensor, | ||
/, | ||
*, | ||
reduction: Optional[str] = "mean", | ||
) -> torch.Tensor: | ||
return torch.nn.functional.l1_loss( | ||
input, | ||
target, | ||
reduction=reduction, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.