softmax derivative c++ code example

Example: softmax derivative c++

// this function calculates the the softmax function.
// @param size is the size of the input vector.
// @param z is the input vector.
// @return buff is the output vector.
double* softmax(const int size, double* z)
{
  double* buff = new double[size];
  double sum = 0;
  for (int i = 0; i < size; i++)
    sum += Exp(z[i]);

  for (int i = 0; i < size; i++)
    buff[i] = Exp(z[i]) / sum;
  
  return buff;
}

// this function calculates the derivative of the softmax function.
// @param size is the size of the input vector.
// @param z is the input vector.
// @return buff is the output vector.
double* softmaxDerivative(const int size, double* z)
{
  double* buff = new double[size];
  double* act = softmax(size, z);
  for (int i = 0; i < size; i++) {
    buff[i] = act[i] * (1. - act[i]);
  }
  delete[] act;
  return buff;
}

Tags:

Misc Example