-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivations.cpp
59 lines (46 loc) · 1.5 KB
/
activations.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
#include <math.h>
#include "activations.hpp"
const std::string Sigmoid::mName = "sigmoid";
const std::string Identity::mName = "identity";
double Sigmoid::Value(const double value) const {
return 1.0 / (1.0 + exp(-value));
}
Matrix Sigmoid::Value(const Matrix& value) const {
auto result = value;
for (int i = 0; i < result.n_rows; ++i)
for (int j = 0; j < result.n_cols; ++j)
result(i, j) = this->Value(value(i, j));
return result;
}
double Sigmoid::Deriv(const double value) const {
const double sigmoid = this->Value(value);
return sigmoid * (1 - sigmoid);
}
Matrix Sigmoid::Deriv(const Matrix& value) const {
auto result = value;
for (int i = 0; i < result.n_rows; ++i)
for (int j = 0; j < result.n_cols; ++j)
result(i, j) = this->Deriv(value(i, j));
return result;
}
double Identity::Value(const double value) const {
return value;
}
Matrix Identity::Value(const Matrix& value) const {
return value;
}
double Identity::Deriv(const double value) const {
return 1.0;
}
Matrix Identity::Deriv(const Matrix& value) const {
return arma::ones(value.n_rows, value.n_cols);
}
std::unique_ptr<Activation> make_activation(const std::string& actName) {
if (actName == "sigmoid") {
return std::unique_ptr<Activation>(new Sigmoid());
} else if (actName == "identity") {
return std::unique_ptr<Activation>(new Identity());
} else {
throw std::logic_error("Unknown activation function name");
}
}