Skip to content

Commit

Permalink
Remove std:: from powf
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Sep 3, 2024
1 parent f6161cd commit 4174e0d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/machine_learning/loss_functions/losses.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace losses {
size_t n = y.size();
double mse = 0.0;
for(size_t i = 0; i<n; i++) {
mse += std::powf(y[i] - y_hat[i], 2);
mse += powf(y[i] - y_hat[i], 2);
}
return mse / double(n);
}
Expand Down
61 changes: 61 additions & 0 deletions src/machine_learning/loss_functions/mean_squared_error.h
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

0 comments on commit 4174e0d

Please sign in to comment.