-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivation_function.h
executable file
·94 lines (78 loc) · 2.16 KB
/
activation_function.h
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
#ifndef _ACTIVATION_FUNCTION_H_
#define _ACTIVATION_FUNCTION_H_
#include <cstdio>
#include <cmath>
#include <cassert>
#include <vector>
#include <limits>
#include "util.h"
using namespace std;
class ActivationFunction {
public:
virtual double Calculate(double u, vector<struct Neuron> const &neurons) {
return -numeric_limits<double>::max();
}
virtual double CalculateDerivative(double u) {
return -numeric_limits<double>::max();
}
};
class LogisticSigmoid : public ActivationFunction {
public:
virtual double Calculate(double u, vector<struct Neuron> const &neurons) {
return 1/(1 + exp(-u));
}
virtual double CalculateDerivative(double u) {
double fu = 1/(1 + exp(-u));
return fu * (1 - fu);
}
};
class TangentSigmoid : public ActivationFunction {
public:
virtual double Calculate(double u, vector<struct Neuron> const &neurons) {
return tanh(u);
}
virtual double CalculateDerivative(double u) {
return (1 - tanh(u)*tanh(u));
}
};
class RectifiedLinear : public ActivationFunction {
public:
virtual double Calculate(double u, vector<struct Neuron> const &neurons) {
return max(u, 0.0);
}
virtual double CalculateDerivative(double u) {
if (u >= 0) return 1.0;
else return 0.0;
}
};
class Identity : public ActivationFunction {
public:
virtual double Calculate(double u, vector<struct Neuron> const &neurons) {
return u;
}
virtual double CalculateDerivative(double u) {
return 1.0;
}
};
class Softmax : public ActivationFunction {
public:
virtual double Calculate(double u, vector<struct Neuron> const &neurons) {
double denominator = 0;
// overflow taisaku fix later
if( u > 30 ){
for( int i = 0; i < neurons.size(); i++ )
if( u+1 < neurons[i].u ) return 0;
return 1;
}
for (int i=0; i<neurons.size(); i++) {
denominator += exp(neurons[i].u);
}
assert(denominator != 0.0);
return (double)(exp(u)/denominator);
}
virtual double CalculateDerivative(double u) {
assert(0);
return -numeric_limits<double>::max();
}
};
#endif