-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathact.cpp
47 lines (38 loc) · 1.27 KB
/
act.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
//
// Created by Izzat on 11/26/2023.
//
#include "nn.h"
#include <valarray>
#include <numeric>
namespace nn::act {
Function step{
[](double x) -> double { return x >= 0 ? 1 : 0; },
[](double y) -> double { return 0; }
};
Function sign{
[](double x) -> double { return x >= 0 ? 1 : -1; },
[](double y) -> double { return 0; }
};
Function linear{
[](double x) -> double { return x; },
[](double y) -> double { return 1; }
};
Function relu{
[](double x) -> double { return x > 0 ? x : 0; },
[](double y) -> double { return y > 0 ? 1 : 0; }
};
Function sigmoid{
[](double x) -> double { return 1 / (1 + std::exp(-x)); },
[](double y) -> double { return y * (1 - y); }
};
Function tanh{
[](double x) -> double { return std::tanh(x); },
[](double y) -> double { return 1 - y * y; }
};
vd_t softmax(const vd_t &x) {
auto sum = std::accumulate(x.begin(), x.end(), 0.0, [](auto t, auto i) { return t + std::exp(i); });
vd_t outputs(x);
std::transform(outputs.begin(), outputs.end(), outputs.begin(), [sum](auto i) { return std::exp(i) / sum; });
return outputs;
}
}