-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlosses.cpp
More file actions
98 lines (84 loc) · 2.9 KB
/
losses.cpp
File metadata and controls
98 lines (84 loc) · 2.9 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <vector>
#include <cmath>
#include <algorithm>
struct Loss {
double value;
std::vector<double> gradient;
};
Loss compute_mse(const std::vector<double>& pred, const std::vector<double>& target) {
Loss result;
result.value = 0.0;
result.gradient.resize(pred.size());
for (size_t i = 0; i < pred.size(); i++) {
double diff = pred[i] - target[i];
result.value += diff * diff;
result.gradient[i] = 2.0 * diff / pred.size();
}
result.value /= pred.size();
return result;
}
Loss compute_bce(const std::vector<double>& pred, const std::vector<double>& target) {
Loss result;
result.value = 0.0;
result.gradient.resize(pred.size());
const double eps = 1e-7;
for (size_t i = 0; i < pred.size(); i++) {
double p = std::max(eps, std::min(1.0 - eps, pred[i]));
result.value += -(target[i] * std::log(p) + (1.0 - target[i]) * std::log(1.0 - p));
result.gradient[i] = p - target[i];
}
result.value /= pred.size();
return result;
}
Loss compute_ce(const std::vector<double>& pred, const std::vector<double>& target) {
Loss result;
result.value = 0.0;
result.gradient.resize(pred.size());
const double eps = 1e-7;
for (size_t i = 0; i < pred.size(); i++) {
double p = std::max(eps, pred[i]);
result.value += -target[i] * std::log(p);
result.gradient[i] = -target[i] / p;
}
result.value /= pred.size();
return result;
}
double compute_accuracy(const std::vector<std::vector<double>>& predictions,
const std::vector<std::vector<double>>& targets) {
double correct = 0.0;
for (size_t i = 0; i < predictions.size(); i++) {
int pred_idx = 0;
double max_val = predictions[i][0];
for (size_t j = 1; j < predictions[i].size(); j++) {
if (predictions[i][j] > max_val) {
max_val = predictions[i][j];
pred_idx = j;
}
}
int target_idx = 0;
max_val = targets[i][0];
for (size_t j = 1; j < targets[i].size(); j++) {
if (targets[i][j] > max_val) {
max_val = targets[i][j];
target_idx = j;
}
}
if (pred_idx == target_idx) {
correct += 1.0;
}
}
return correct / predictions.size();
}
std::vector<Loss> compute_batch_loss(const std::vector<std::vector<double>>& predictions,
const std::vector<std::vector<double>>& targets,
bool use_bce = true) {
std::vector<Loss> losses;
for (size_t i = 0; i < predictions.size(); i++) {
if (use_bce) {
losses.push_back(compute_bce(predictions[i], targets[i]));
} else {
losses.push_back(compute_mse(predictions[i], targets[i]));
}
}
return losses;
}