softmax

时间:2023-03-09 17:13:33
softmax
  1. void LogisticRegression_softmax(LogisticRegression *this, double *x) {
  2. int i;
  3. double max = 0.0;
  4. double sum = 0.0;
  5. for(i=0; i<this->n_out; i++) if(max < x[i]) max = x[i];
  6. for(i=0; i<this->n_out; i++) {
  7. x[i] = exp(x[i] - max);
  8. sum += x[i];
  9. }
  10. for(i=0; i<this->n_out; i++) x[i] /= sum;
  11. }

发现它和文献中对softmax模型中参数优化的迭代公式中是不一样!其实,如果没有那个求最大值的过程,直接取指数运算就一样的。而加一个求最大值的好处在于避免数据的绝对值过小,数据绝对值太小可能会导致计算一直停留在零而无法进行。就像对数似然函数,似然函数取对数防止概率过小一样。