1// this function calculates the the softmax function.
2// @param size is the size of the input vector.
3// @param z is the input vector.
4// @return buff is the output vector.
5double* softmax(const int size, double* z)
6{
7 double* buff = new double[size];
8 double sum = 0;
9 for (int i = 0; i < size; i++)
10 sum += Exp(z[i]);
11
12 for (int i = 0; i < size; i++)
13 buff[i] = Exp(z[i]) / sum;
14
15 return buff;
16}
17
18// this function calculates the derivative of the softmax function.
19// @param size is the size of the input vector.
20// @param z is the input vector.
21// @return buff is the output vector.
22double* softmaxDerivative(const int size, double* z)
23{
24 double* buff = new double[size];
25 double* act = softmax(size, z);
26 for (int i = 0; i < size; i++) {
27 buff[i] = act[i] * (1. - act[i]);
28 }
29 delete[] act;
30 return buff;
31}