openGPMP
Open Source Mathematics Package
activators.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
34 #include <vector>
35 
37  return 1.0 / (1.0 + exp(-z));
38 }
39 
41  double sig = sigmoid(z);
42  return sig * (1.0 - sig);
43 }
44 
45 double gpmp::ml::Activation::relu(double z) {
46  return z > 0 ? z : 0;
47 }
48 
50  return z > 0 ? 1 : 0;
51 }
52 
53 std::vector<double>
54 gpmp::ml::Activation::softmax(const std::vector<double> &inputs) {
55  std::vector<double> result;
56  double sum_exp = 0.0;
57  for (double input : inputs) {
58  sum_exp += exp(input);
59  }
60  for (double input : inputs) {
61  result.push_back(exp(input) / sum_exp);
62  }
63  return result;
64 }
65 
66 std::vector<std::vector<double>>
67 gpmp::ml::Activation::softmax_derivative(const std::vector<double> &inputs) {
68  std::vector<std::vector<double>> result(
69  inputs.size(),
70  std::vector<double>(inputs.size(), 0.0));
71  std::vector<double> softmax_values = softmax(inputs);
72  for (size_t i = 0; i < inputs.size(); ++i) {
73  for (size_t j = 0; j < inputs.size(); ++j) {
74  if (i == j) {
75  result[i][j] = softmax_values[i] * (1.0 - softmax_values[i]);
76  } else {
77  result[i][j] = -softmax_values[i] * softmax_values[j];
78  }
79  }
80  }
81  return result;
82 }
83 
85  return z >= 0 ? 1 : 0;
86 }
87 
88 double gpmp::ml::Activation::tanh(double z) {
89  return std::tanh(z);
90 }
91 
93  double tanh_z = std::tanh(z);
94  return 1.0 - tanh_z * tanh_z;
95 }
96 
97 double gpmp::ml::Activation::smht(double z) {
98  return z / (1.0 + exp(-z));
99 }
100 
102  double smht_z = smht(z);
103  return smht_z * (1.0 - smht_z);
104 }
105 
106 double gpmp::ml::Activation::gelu(double z) {
107  return 0.5 * z *
108  (1.0 + tanh(sqrt(2.0 / M_PI) * (z + 0.044715 * z * z * z)));
109 }
110 
112  double cdf =
113  0.5 * (1.0 + tanh(sqrt(2.0 / M_PI) * (0.044715 * z * z * z + 3 * z)));
114  double pdf = exp(-0.5 * z * z) / sqrt(2.0 * M_PI);
115  return 0.5 * (1.0 + cdf +
116  z * pdf * (1.0 / M_PI) *
117  (0.5 * (1.0 + tanh(sqrt(2.0 / M_PI) *
118  (0.044715 * z * z * z + 3 * z)))) +
119  (1.0 / M_PI) * (1.0 - cdf * cdf));
120 }
121 
123  return log(1.0 + exp(z));
124 }
125 
126 double gpmp::ml::Activation::elu(double z, double alpha) {
127  return z >= 0 ? z : alpha * (exp(z) - 1);
128 }
129 
130 double gpmp::ml::Activation::elu_derivative(double z, double alpha) {
131  return z >= 0 ? 1 : elu(z, alpha) + alpha;
132 }
133 
134 double gpmp::ml::Activation::selu(double z, double alpha, double scale) {
135  return scale * (z >= 0 ? z : alpha * (exp(z) - 1));
136 }
137 
138 double
139 gpmp::ml::Activation::selu_derivative(double z, double alpha, double scale) {
140  return scale * (z >= 0 ? 1 : alpha * exp(z));
141 }
142 
143 double gpmp::ml::Activation::leaky_relu(double z, double alpha) {
144  return z >= 0 ? z : alpha * z;
145 }
146 
147 double gpmp::ml::Activation::leaky_relu_derivative(double z, double alpha) {
148  return z >= 0 ? 1 : alpha;
149 }
150 
151 double gpmp::ml::Activation::prelu(double z, double alpha) {
152  return z >= 0 ? z : alpha * z;
153 }
154 
155 double gpmp::ml::Activation::prelu_derivative(double z, double alpha) {
156  return z >= 0 ? 1 : alpha;
157 }
158 
159 double gpmp::ml::Activation::silu(double z) {
160  return z / (1 + exp(-z));
161 }
162 
164  double sig_z = sigmoid(z);
165  return sig_z + (1 - sig_z) * z * sig_z;
166 }
std::vector< std::vector< double > > softmax_derivative(const std::vector< double > &inputs)
Computes the derivative of the softmax activation function.
Definition: activators.cpp:67
double gelu(double z)
Computes the Gaussian Error Linear Unit (GELU) activation function.
Definition: activators.cpp:106
double gelu_derivative(double z)
Computes the derivative of the Gaussian Error Linear Unit (GELU) activation function.
Definition: activators.cpp:111
double binary_step(double z)
Computes the binary step activation function.
Definition: activators.cpp:84
double sigmoid_derivative(double z)
Computes the derivative of the sigmoid activation function.
Definition: activators.cpp:40
double silu_derivative(double z)
Computes the derivative of the Sigmoid Linear Unit (SiLU or Swish) activation function.
Definition: activators.cpp:163
double smht_derivative(double z)
Computes the derivative of the Soboleva modified hyperbolic tangent (smht) activation function.
Definition: activators.cpp:101
double relu_derivative(double z)
Computes the derivative of the ReLU (Rectified Linear Unit) activation function.
Definition: activators.cpp:49
double tanh_derivative(double z)
Computes the derivative of the hyperbolic tangent (tanh) activation function.
Definition: activators.cpp:92
double silu(double z)
Computes the Sigmoid Linear Unit (SiLU or Swish) activation function.
Definition: activators.cpp:159
double softplus(double z)
Computes the softplus activation function.
Definition: activators.cpp:122
double sigmoid(double z)
Computes the sigmoid activation function.
Definition: activators.cpp:36
double leaky_relu(double z, double alpha=001)
Computes the Leaky Rectified Linear Unit (Leaky ReLU) activation function.
Definition: activators.cpp:143
double leaky_relu_derivative(double z, double alpha=001)
Computes the derivative of the Leaky Rectified Linear Unit (Leaky ReLU) activation function.
Definition: activators.cpp:147
double selu(double z, double alpha=167326, double scale=10507)
Computes the Scaled Exponential Linear Unit (SELU) activation function.
Definition: activators.cpp:134
double prelu(double z, double alpha)
Computes the Parametric Rectified Linear Unit (PReLU) activation function.
Definition: activators.cpp:151
double elu(double z, double alpha=10)
Computes the Exponential Linear Unit (ELU) activation function.
Definition: activators.cpp:126
double selu_derivative(double z, double alpha=167326, double scale=10507)
Computes the derivative of the Scaled Exponential Linear Unit (SELU) activation function.
Definition: activators.cpp:139
double smht(double z)
Computes the Soboleva modified hyperbolic tangent (smht) activation function.
Definition: activators.cpp:97
double tanh(double z)
Computes the hyperbolic tangent (tanh) activation function.
Definition: activators.cpp:88
std::vector< double > softmax(const std::vector< double > &inputs)
Computes the softmax activation function.
Definition: activators.cpp:54
double relu(double z)
Computes the ReLU (Rectified Linear Unit) activation function.
Definition: activators.cpp:45
double elu_derivative(double z, double alpha=10)
Computes the derivative of the Exponential Linear Unit (ELU) activation function.
Definition: activators.cpp:130
double prelu_derivative(double z, double alpha)
Computes the derivative of the Parametric Rectified Linear Unit (PReLU) activation function.
Definition: activators.cpp:155