-
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.
- MeanSquaredError - MeanAbsoluteError - BinaryCrossEntropyLoss Added typedefs for some alternative names;
- Loading branch information
Showing
11 changed files
with
147 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,64 @@ | ||
/******************************************************* | ||
* 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 &inputs, | ||
const autograd::Variable &targets) = 0; | ||
|
||
autograd::Variable forward(const autograd::Variable &inputs); | ||
}; | ||
|
||
class MeanSquaredError : public Loss | ||
{ | ||
public: | ||
MeanSquaredError() {} | ||
|
||
autograd::Variable forward(const autograd::Variable &inputs, | ||
const autograd::Variable &targets); | ||
}; | ||
|
||
class MeanAbsoluteError : public Loss | ||
{ | ||
public: | ||
MeanAbsoluteError() {} | ||
|
||
autograd::Variable forward(const autograd::Variable &inputs, | ||
const autograd::Variable &targets); | ||
}; | ||
|
||
class BinaryCrossEntropyLoss : public Loss | ||
{ | ||
public: | ||
BinaryCrossEntropyLoss() {} | ||
|
||
autograd::Variable forward(const autograd::Variable &inputs, | ||
const autograd::Variable &targets); | ||
|
||
autograd::Variable forward(const autograd::Variable &inputs, | ||
const autograd::Variable &targets, | ||
const autograd::Variable &weights); | ||
}; | ||
|
||
typedef MeanSquaredError MSE; | ||
typedef MeanAbsoluteError MAE; | ||
typedef MeanAbsoluteError L1Loss; | ||
typedef BinaryCrossEntropyLoss BCELoss; | ||
} | ||
} |
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,59 @@ | ||
/******************************************************* | ||
* 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 &inputs) | ||
{ | ||
throw af::exception("Loss module requires both inputs and targets"); | ||
} | ||
|
||
autograd::Variable MeanSquaredError::forward(const autograd::Variable &inputs, | ||
const autograd::Variable &targets) | ||
{ | ||
auto df = inputs - targets; | ||
auto res = mean(flat(df * df), {0}); | ||
return res; | ||
} | ||
|
||
autograd::Variable MeanAbsoluteError::forward(const autograd::Variable &inputs, | ||
const autograd::Variable &targets) | ||
{ | ||
auto df = inputs - targets; | ||
auto res = mean(flat(abs(df)), {0}); | ||
} | ||
|
||
static autograd::Variable | ||
binaryCrossEntropy(const autograd::Variable &inputs, | ||
const autograd::Variable &targets) | ||
{ | ||
targets * inputs + (1 - targets) * (1 - inputs); | ||
} | ||
|
||
autograd::Variable BinaryCrossEntropyLoss::forward(const autograd::Variable &inputs, | ||
const autograd::Variable &targets) | ||
{ | ||
return mean(flat(binaryCrossEntropy(inputs, targets)), {0}); | ||
} | ||
|
||
autograd::Variable BinaryCrossEntropyLoss::forward(const autograd::Variable &inputs, | ||
const autograd::Variable &targets, | ||
const autograd::Variable &weights) | ||
{ | ||
return mean(flat(weights * binaryCrossEntropy(inputs, targets)), {0}); | ||
} | ||
} | ||
} |