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模型中参数优化的迭代公式中是不同!其实,若是没有那个求最大值的过程,直接取指数运算就同样的。而加一个求最大值的好处在于避免数据的绝对值太小,数据绝对值过小可能会致使计算一直停留在零而没法进行。就像对数似然函数,似然函数取对数防止几率太小同样。函数

相关文章
相关标签/搜索