softmax

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

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

原文地址:https://www.cnblogs.com/ytjjyy/p/4675004.html