-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivationFunction.cpp
More file actions
82 lines (64 loc) · 1.81 KB
/
ActivationFunction.cpp
File metadata and controls
82 lines (64 loc) · 1.81 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
#include <cmath>
#include <iostream>
#include <utility>
#include "ActivationFunction.h"
vector<double> minMaxScale(vector<double> values)
{
double min = *min_element(values.begin(), values.end());
double max = *max_element(values.begin(), values.end());
for(double &value : values)
{
value = (value - min) / (max - min);
}
return values;
}
/**
* @brief Sigmoid function.
*
* This function calculates the sigmoid value for the given input.
*
* @param neuronOutputs The input value.
* @return The sigmoid value.
*/
double Sigmoid::Function(vector<double> neuronOutputs, int neuronIndex)
{
return 1.0 / (1.0 + std::exp(-neuronOutputs[neuronIndex]));
}
/**
* Calculates the derivative of the sigmoid function at a given input.
*
* @param neuronOutputs The input value to the sigmoid function.
* @return The derivative value of the sigmoid function at the given input.
*/
double Sigmoid::Derivative(vector<double> neuronOutputs, int neuronIndex)
{
double sigmoid = Function(neuronOutputs, neuronIndex);
double output = sigmoid * (1 - sigmoid);
return output;
}
double ReLU::Function(vector<double> neuronOutputs, int neuronIndex)
{
return std::max(0.0, neuronOutputs[neuronIndex]);
}
double ReLU::Derivative(vector<double> neuronOutputs, int neuronIndex)
{
return neuronOutputs[neuronIndex] > 0 ? 1 : 0;
}
double Softmax::Function(vector<double> neuronOutputs, int neuronIndex)
{
double expSum = 0;
for (int i = 0; i < neuronOutputs.size(); i++)
{
expSum += exp(neuronOutputs[i]);
}
return exp(neuronOutputs[neuronIndex]) / expSum;
}
double Softmax::Derivative(vector<double> neuronOutputs, int neuronIndex)
{
double sum = 0;
for (int i = 0; i < neuronOutputs.size(); i++) {
sum += exp(neuronOutputs[i]);
}
double ex = exp(neuronOutputs[neuronIndex]);
return 10 * ex * (sum - ex) / (sum * sum);
}