-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7063 from kshitij12345/add-losses
[chainerx] add basic loss functions
- Loading branch information
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
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
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,27 @@ | ||
#include "chainerx/routines/loss.h" | ||
|
||
#include "chainerx/array.h" | ||
#include "chainerx/routines/creation.h" | ||
#include "chainerx/routines/explog.h" | ||
#include "chainerx/routines/indexing.h" | ||
#include "chainerx/routines/misc.h" | ||
#include "chainerx/scalar.h" | ||
|
||
namespace chainerx { | ||
|
||
Array AbsoluteError(const Array& x1, const Array& x2) { return Absolute(x1 - x2); } | ||
|
||
Array SquaredError(const Array& x1, const Array& x2) { return SquaredDifference(x1, x2); } | ||
|
||
Array GaussianKLDivergence(const Array& mean, const Array& ln_var) { return (Square(mean) + Exp(ln_var) - ln_var - 1) * 0.5; } | ||
|
||
Array HuberLoss(const Array& x1, const Array& x2, Scalar delta) { | ||
Array a = x1 - x2; | ||
Array abs_a = Absolute(a); | ||
Array delta_array = chainerx::FullLike(a, delta, a.device()); | ||
|
||
// TODO(kshitij12345) : use Array < Scalar when implemented. | ||
return Where(abs_a < delta_array, 0.5 * Square(a), delta * (abs_a - Scalar{0.5} * delta)); | ||
} | ||
|
||
} // namespace chainerx |
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,16 @@ | ||
#pragma once | ||
|
||
#include "chainerx/array.h" | ||
#include "chainerx/scalar.h" | ||
|
||
namespace chainerx { | ||
|
||
Array AbsoluteError(const Array& x1, const Array& x2); | ||
|
||
Array SquaredError(const Array& x1, const Array& x2); | ||
|
||
Array GaussianKLDivergence(const Array& mean, const Array& ln_var); | ||
|
||
Array HuberLoss(const Array& x1, const Array& x2, Scalar delta); | ||
|
||
} // namespace chainerx |
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.