-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding MeanSquaredError, MeanAbsoluteError loss functions.
- Loading branch information
Showing
11 changed files
with
109 additions
and
9 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
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,46 @@ | ||
/******************************************************* | ||
* Copyright (c) 2017, ArrayFire | ||
* All rights reserved. | ||
* | ||
* This file is distributed under 3-clause BSD license. | ||
* The complete license agreement can be obtained at: | ||
* http://arrayfire.com/licenses/BSD-3-Clause | ||
********************************************************/ | ||
#pragma once | ||
|
||
#include <af/nn/Modules/Module.hpp> | ||
|
||
namespace af | ||
{ | ||
namespace nn | ||
{ | ||
class Loss : public Module | ||
{ | ||
public: | ||
Loss() {} | ||
|
||
virtual autograd::Variable forward(const autograd::Variable &input, | ||
const autograd::Variable &target) = 0; | ||
|
||
autograd::Variable forward(const autograd::Variable &input); | ||
}; | ||
|
||
class MeanSquaredError : public Loss | ||
{ | ||
public: | ||
MeanSquaredError() {} | ||
|
||
autograd::Variable forward(const autograd::Variable &input, | ||
const autograd::Variable &target); | ||
}; | ||
|
||
class MeanAbsoluteError : public Loss | ||
{ | ||
public: | ||
MeanAbsoluteError() {} | ||
|
||
autograd::Variable forward(const autograd::Variable &input, | ||
const autograd::Variable &target); | ||
}; | ||
} | ||
} |
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,39 @@ | ||
/******************************************************* | ||
* Copyright (c) 2017, ArrayFire | ||
* All rights reserved. | ||
* | ||
* This file is distributed under 3-clause BSD license. | ||
* The complete license agreement can be obtained at: | ||
* http://arrayfire.com/licenses/BSD-3-Clause | ||
********************************************************/ | ||
#include <af/autograd/Functions.hpp> | ||
|
||
#include <af/nn/Modules/Loss.hpp> | ||
|
||
|
||
namespace af | ||
{ | ||
namespace nn | ||
{ | ||
using namespace autograd; | ||
|
||
autograd::Variable Loss::forward(const autograd::Variable &input) | ||
{ | ||
throw af::exception("Loss module requires both input and targets"); | ||
} | ||
|
||
autograd::Variable MeanSquaredError::forward(const autograd::Variable &input, | ||
const autograd::Variable &target) | ||
{ | ||
auto df = input - target; | ||
return mean(df * df, {0}); | ||
} | ||
|
||
autograd::Variable MeanAbsoluteError::forward(const autograd::Variable &input, | ||
const autograd::Variable &target) | ||
{ | ||
auto df = input - target; | ||
return mean(abs(df), {0}); | ||
} | ||
} | ||
} |