softmax derivative c 2b 2b

Solutions on MaxInterview for softmax derivative c 2b 2b by the best coders in the world

showing results for - "softmax derivative c 2b 2b"
Adil
07 Feb 2019
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}
similar questions
queries leading to this page
softmax derivative c 2b 2b