-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlosses.cpp
60 lines (56 loc) · 1.85 KB
/
losses.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <vector>
#include <math.h>
#include <cmath>
double BCELoss(std::vector<double> true_label, std::vector<double> pred_prob)
{ /**
* Binary Cross Entropy Loss
* @param true_label true labels of the data
* @param pred_prob predicted probabilities
* @return binary cross entropy loss
*/
double sum = 0;
for (int i = 0; i < pred_prob.size(); i++)
{
sum += true_label[i] * log(pred_prob[i]) + (1 - true_label[i]) * log((1 - pred_prob[i]));
}
int size = true_label.size();
double loss = -(1.0 / size) * sum;
return loss;
}
std::vector<double> BCELossDerivative(std::vector<double> true_label, std::vector<double> pred_prob)
{ /**
* Compute derivative of binary cross entropy loss
* @param true_label true labels of the data
* @param pred_prob predicted probabilities
* @return derivative of binary cross entropy loss
*/
std::vector<double> dev = {(pred_prob[0] - true_label[0]) / ((pred_prob[0]) * (1 - pred_prob[0]))};
return dev;
}
double MSELoss(std::vector<double> true_label, std::vector<double> pred)
{ /**
* Mean Squared Error Loss
* @param true_label true labels of the data
* @param pred predicted values
* @return mean squared error loss
*/
double sum = 0;
for (int i = 0; i < true_label.size(); i++)
{
sum += pow(true_label[i] - pred[i], 2.0);
}
int size = true_label.size();
double loss = (1.0 / size) * sum;
return loss;
}
std::vector<double> MSELossDerivative(std::vector<double> true_label, std::vector<double> pred)
{ /**
* Compute derivative of mean squared error loss
* @param true_label true labels of the data
* @param pred predicted values
* @return derivative of mean squared error loss
*/
std::vector<double> sub = subtract(pred, true_label);
std::vector<double> dev = scalarVectorMultiplication(sub, 2);
return dev;
}