-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.cpp
50 lines (40 loc) · 1.47 KB
/
Functions.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
#include "Functions.hpp"
#include <cmath>
#include <stdexcept>
#include <algorithm>
#include "Matrix.hpp"
Matrix Activations::sigmoid(Matrix& x) {
return x.apply_function([](double val) { return 1 / (1 + std::exp(-val)); });
}
Matrix Activations::relu(Matrix& x) {
return x.apply_function([](double val) { return val > 0.0 ? val : 0.01 * val; });
}
Matrix Activations::identity(Matrix& x) {
return x;
}
Matrix Activations::sigmoid_deriv(Matrix& x) {
Matrix sig = sigmoid(x);
Matrix one_minus_sig = sig.scale(-1.0).add_scalar(1.0);
return sig.hadamard(one_minus_sig);
}
Matrix Activations::relu_deriv(Matrix& x) {
return x.apply_function([](double val) { return val > 0.0 ? 1.0 : 0.01; });
}
Matrix Activations::identity_deriv(Matrix& x) {
static Matrix m = Matrix(x.rows, x.cols, 1.0);
return m;
}
Matrix Loss::mean_squared_error(Matrix& predicted, Matrix& target) {
if (predicted.rows != target.rows || predicted.cols != target.cols) {
throw std::invalid_argument("Matrix dimensions do not match for MSE calculation.");
}
Matrix diff = predicted.subtract(target);
Matrix squared = diff.hadamard(diff);
return squared.scale(0.5);
}
Matrix Loss::mean_squared_error_deriv(Matrix& predicted, Matrix& target) {
if (predicted.rows != target.rows || predicted.cols != target.cols) {
throw std::invalid_argument("Matrix dimensions do not match for MSE derivative calculation.");
}
return predicted.subtract(target);
}