-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
119 lines (103 loc) · 3.27 KB
/
utils.cpp
File metadata and controls
119 lines (103 loc) · 3.27 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <vector>
#include <cmath>
#include <algorithm>
#include <random>
#include <chrono>
double dot_product(const std::vector<double>& v1, const std::vector<double>& v2) {
double result = 0.0;
for (size_t i = 0; i < v1.size(); i++) {
result += v1[i] * v2[i];
}
return result;
}
std::vector<double> vector_add(const std::vector<double>& v1, const std::vector<double>& v2) {
std::vector<double> result;
result.reserve(v1.size());
for (size_t i = 0; i < v1.size(); i++) {
result.push_back(v1[i] + v2[i]);
}
return result;
}
std::vector<double> vector_subtract(const std::vector<double>& v1, const std::vector<double>& v2) {
std::vector<double> result;
result.reserve(v1.size());
for (size_t i = 0; i < v1.size(); i++) {
result.push_back(v1[i] - v2[i]);
}
return result;
}
std::vector<double> vector_scale(const std::vector<double>& v, double scalar) {
std::vector<double> result;
result.reserve(v.size());
for (double val : v) {
result.push_back(val * scalar);
}
return result;
}
std::vector<double> vector_multiply(const std::vector<double>& v1, const std::vector<double>& v2) {
std::vector<double> result;
result.reserve(v1.size());
for (size_t i = 0; i < v1.size(); i++) {
result.push_back(v1[i] * v2[i]);
}
return result;
}
std::vector<std::vector<double>> matrix_transpose(const std::vector<std::vector<double>>& m) {
if (m.empty()) return {};
std::vector<std::vector<double>> trans(m[0].size(), std::vector<double>(m.size()));
for (size_t i = 0; i < m.size(); i++) {
for (size_t j = 0; j < m[i].size(); j++) {
trans[j][i] = m[i][j];
}
}
return trans;
}
std::vector<std::vector<double>> init_weights(int rows, int cols) {
static std::mt19937 gen(
std::chrono::system_clock::now().time_since_epoch().count()
);
std::uniform_real_distribution<> dis(-1.0, 1.0);
std::vector<std::vector<double>> weights(rows, std::vector<double>(cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
weights[i][j] = dis(gen);
}
}
return weights;
}
std::vector<double> init_bias(int size) {
static std::mt19937 gen(
std::chrono::system_clock::now().time_since_epoch().count()
);
std::uniform_real_distribution<> dis(-0.1, 0.1);
std::vector<double> bias(size);
for (int i = 0; i < size; i++) {
bias[i] = dis(gen);
}
return bias;
}
double random_uniform(double a, double b) {
static std::mt19937 gen(
std::chrono::system_clock::now().time_since_epoch().count()
);
std::uniform_real_distribution<> dis(a, b);
return dis(gen);
}
std::vector<double> softmax(const std::vector<double>& v) {
double max_val = *std::max_element(v.begin(), v.end());
std::vector<double> exp_vals;
double sum = 0.0;
for (double val : v) {
double exp_val = std::exp(val - max_val);
exp_vals.push_back(exp_val);
sum += exp_val;
}
std::vector<double> result;
for (double exp_val : exp_vals) {
result.push_back(exp_val / sum);
}
return result;
}
double clip(double val, double min_val, double max_val) {
return std::max(min_val, std::min(max_val, val));
}