-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6161cd
commit 4174e0d
Showing
2 changed files
with
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#ifndef MEAN_SQUARED_ERROR_H | ||
#define MEAN_SQUARED_ERROR_H | ||
|
||
#ifdef __cplusplus | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
#include <cmath> | ||
#include <cassert> | ||
#endif | ||
|
||
|
||
namespace losses { | ||
float mean_squared_error(std::vector<float> const& y, std::vector<float> const& y_hat) { | ||
assert(y.size() == y_hat.size()); | ||
size_t n = y.size(); | ||
float mse = 0.0; | ||
for(size_t i = 0; i<n; i++) { | ||
mse += std::powf(y[i] - y_hat[i], 2); | ||
} | ||
return mse / float(n); | ||
} | ||
|
||
float root_mean_squared_error(std::vector<float> const& y, std::vector<float> const& y_hat) { | ||
return std::sqrt(mean_squared_error(y, y_hat)); | ||
} | ||
|
||
float mean_absolute_error(std::vector<float> const& y, std::vector<float> const& y_hat) { | ||
assert(y.size() == y_hat.size()); | ||
size_t n = y.size(); | ||
float mae = 0.0; | ||
for(size_t i = 0; i<n; i++) { | ||
mae += std::abs(y[i] - y_hat[i]); | ||
} | ||
return mae / float(n); | ||
} | ||
|
||
float binary_crossentropy_loss(std::vector<float> const& y, std::vector<float> const& y_hat) { | ||
assert(y.size() == y_hat.size()); | ||
size_t n = y.size(); | ||
float bce = 0.0; | ||
for(size_t i = 0; i<n; i++) { | ||
bce += (y[i]*std::log(y_hat[i]) + (1-y[i])*std::log(1 - y_hat[i])); | ||
} | ||
return -(1/float(n))*bce; | ||
} | ||
|
||
float crossentropy_loss(std::vector<float> const& y, std::vector<float> const& y_hat) { | ||
assert(y.size() == y_hat.size()); | ||
size_t n = y.size(); | ||
float ce = 0.0; | ||
for(size_t i = 0; i<n; i++) { | ||
ce += y[i]*std::log(y_hat[i]); | ||
} | ||
return -(1/float(n))*ce; | ||
} | ||
} | ||
|
||
|
||
#endif | ||
|