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: Daniel4078 <[email protected]>
- Loading branch information
1 parent
685a484
commit a29ce99
Showing
9 changed files
with
540 additions
and
0 deletions.
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
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,25 @@ | ||
import jax.numpy as jnp | ||
from typing import Optional | ||
from ivy.functional.backends.jax import JaxArray | ||
|
||
|
||
def smooth_l1_loss( | ||
input: JaxArray, | ||
target: JaxArray, | ||
/, | ||
*, | ||
beta: Optional[float] = 1.0, | ||
reduction: Optional[str] = "mean", | ||
) -> JaxArray: | ||
if beta < 1e-5: | ||
loss = jnp.abs(input - target) | ||
else: | ||
diff = jnp.abs(input - target) | ||
loss = jnp.where(diff < beta, 0.5 * diff**2 / beta, diff - 0.5 * beta) | ||
|
||
if reduction == "mean": | ||
return jnp.mean(loss) | ||
elif reduction == "sum": | ||
return jnp.sum(loss) | ||
else: | ||
return loss |
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,30 @@ | ||
import numpy as np | ||
from typing import Optional | ||
from ivy.functional.backends.numpy.helpers import _scalar_output_to_0d_array | ||
from ivy.func_wrapper import with_unsupported_dtypes | ||
from . import backend_version | ||
|
||
|
||
# Implementation of smooth_l1_loss in the given format | ||
@with_unsupported_dtypes({"1.25.2 and below": ("bool",)}, backend_version) | ||
@_scalar_output_to_0d_array | ||
def smooth_l1_loss( | ||
input: np.ndarray, | ||
target: np.ndarray, | ||
/, | ||
*, | ||
beta: Optional[float] = 1.0, | ||
reduction: Optional[str] = "mean", | ||
) -> np.ndarray: | ||
if beta < 1e-5: | ||
loss = np.abs(input - target) | ||
else: | ||
diff = np.abs(input - target) | ||
loss = np.where(diff < beta, 0.5 * diff**2 / beta, diff - 0.5 * beta) | ||
|
||
if reduction == "mean": | ||
return np.mean(loss) | ||
elif reduction == "sum": | ||
return np.sum(loss) | ||
else: | ||
return loss |
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,24 @@ | ||
import tensorflow as tf | ||
from typing import Optional | ||
from ivy.func_wrapper import with_unsupported_dtypes | ||
from . import backend_version | ||
|
||
|
||
@with_unsupported_dtypes({"2.13.0 and below": "bool"}, backend_version) | ||
def smooth_l1_loss( | ||
input: tf.Tensor, | ||
target: tf.Tensor, | ||
/, | ||
*, | ||
beta: Optional[float] = 1.0, | ||
reduction: Optional[str] = "mean", | ||
) -> tf.Tensor: | ||
diff = tf.abs(input - target) | ||
loss = tf.where(diff < beta, 0.5 * diff**2 / beta, diff - 0.5 * beta) | ||
|
||
if reduction == "mean": | ||
return tf.reduce_mean(loss) | ||
elif reduction == "sum": | ||
return tf.reduce_sum(loss) | ||
else: | ||
return loss |
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.