SoftMax regression

时间:2021-02-03 01:30:53

最终收敛到这个结果,巨爽。

smaple 0: 0.983690,0.004888,0.011422,likelyhood:-0.016445

smaple 1: 0.940236,0.047957,0.011807,likelyhood:-0.061625

smaple 2: 0.818187,0.001651,0.180162,likelyhood:-0.200665

smaple 3: 0.000187,0.999813,0.000000,likelyhood:-0.000187

smaple 4: 0.007913,0.992087,0.000000,likelyhood:-0.007945

smaple 5: 0.001585,0.998415,0.000000,likelyhood:-0.001587

smaple 6: 0.020159,0.000001,0.979840,likelyhood:-0.020366

smaple 7: 0.018230,0.000000,0.981770,likelyhood:-0.018398

smaple 8: 0.025072,0.000000,0.974928,likelyhood:-0.025392

  1. #include "stdio.h"
  2. #include "math.h"
  3. double matrix[9][4]={{1,47,76,24}, //include x0=1
  4. {1,46,77,23},
  5. {1,48,74,22},
  6. {1,34,76,21},
  7. {1,35,75,24},
  8. {1,34,77,25},
  9. {1,55,76,21},
  10. {1,56,74,22},
  11. {1,55,72,22},
  12. };
  13. double result[]={1,
  14. 1,
  15. 1,
  16. 2,
  17. 2,
  18. 2,
  19. 3,
  20. 3,
  21. 3,};
  22. double theta[2][4]={
  23. {0.3,0.3,0.01,0.01},
  24. {0.5,0.5,0.01,0.01}}; // include theta0
  25. double function_g(double x)
  26. {
  27. double ex = pow(2.718281828,x);
  28. return ex/(1+ex);
  29. }
  30. double function_e(double x)
  31. {
  32. return pow(2.718281828,x);
  33. }
  34. int main(void)
  35. {
  36. double likelyhood = 0.0;
  37. for(int j = 0;j<9;++j)
  38. {
  39. double sum = 1.0; // this is very important, because exp(thetak x)=1
  40. for(int l = 0;l<2;++l)
  41. {
  42. double xi = 0.0;
  43. for(int k=0;k<4;++k)
  44. {
  45. xi += matrix[j][k]*theta[l][k];
  46. }
  47. sum += function_e(xi);
  48. }
  49. double xi = 0.0;
  50. for(int k=0;k<4;++k)
  51. {
  52. xi += matrix[j][k]*theta[0][k];
  53. }
  54. double p1 = function_e(xi)/sum;
  55. xi = 0.0;
  56. for(int k=0;k<4;++k)
  57. {
  58. xi += matrix[j][k]*theta[1][k];
  59. }
  60. double p2 = function_e(xi)/sum;
  61. double p3 = 1-p1-p2;
  62. double ltheta = 0.0;
  63. if(result[j]==1)
  64. ltheta = log(p1);
  65. else if(result[j]==2)
  66. ltheta = log(p2);
  67. else if(result[j]==3)
  68. ltheta = log(p3);
  69. else
  70. {}
  71. printf("smaple %d: %f,%f,%f,likelyhood:%f\n",j,p1,p2,p3,ltheta);
  72. }
  73. for(int i =0 ;i<1000;++i)
  74. {
  75. for(int j=0;j<9;++j)
  76. {
  77. double sum = 1.0; // this is very important, because exp(thetak x)=1
  78. for(int l = 0;l<2;++l)
  79. {
  80. double xi = 0.0;
  81. for(int k=0;k<4;++k)
  82. {
  83. xi += matrix[j][k]*theta[l][k];
  84. }
  85. sum += function_e(xi);
  86. }
  87. double xi = 0.0;
  88. for(int k=0;k<4;++k)
  89. {
  90. xi += matrix[j][k]*theta[0][k];
  91. }
  92. double p1 = function_e(xi)/sum;
  93. xi = 0.0;
  94. for(int k=0;k<4;++k)
  95. {
  96. xi += matrix[j][k]*theta[1][k];
  97. }
  98. double p2 = function_e(xi)/sum;
  99. double p3 = 1-p1-p2;
  100. for(int m = 0; m<4; ++m)
  101. {
  102. if(result[j]==1)
  103. {
  104. theta[0][m] = theta[0][m] + 0.001*(1-p1)*matrix[j][m];
  105. }
  106. else
  107. {
  108. theta[0][m] = theta[0][m] + 0.001*(-p1)*matrix[j][m];
  109. }
  110. if(result[j]==2)
  111. {
  112. theta[1][m] = theta[1][m] + 0.001*(1-p2)*matrix[j][m];
  113. }
  114. else
  115. {
  116. theta[1][m] = theta[1][m] + 0.001*(-p2)*matrix[j][m];
  117. }
  118. }
  119. }
  120. double likelyhood = 0.0;
  121. for(int j = 0;j<9;++j)
  122. {
  123. double sum = 1.0; // this is very important, because exp(thetak x)=1
  124. for(int l = 0;l<2;++l)
  125. {
  126. double xi = 0.0;
  127. for(int k=0;k<4;++k)
  128. {
  129. xi += matrix[j][k]*theta[l][k];
  130. }
  131. sum += function_e(xi);
  132. }
  133. double xi = 0.0;
  134. for(int k=0;k<4;++k)
  135. {
  136. xi += matrix[j][k]*theta[0][k];
  137. }
  138. double p1 = function_e(xi)/sum;
  139. xi = 0.0;
  140. for(int k=0;k<4;++k)
  141. {
  142. xi += matrix[j][k]*theta[1][k];
  143. }
  144. double p2 = function_e(xi)/sum;
  145. double p3 = 1-p1-p2;
  146. double ltheta = 0.0;
  147. if(result[j]==1)
  148. ltheta = log(p1);
  149. else if(result[j]==2)
  150. ltheta = log(p2);
  151. else if(result[j]==3)
  152. ltheta = log(p3);
  153. else
  154. {}
  155. printf("smaple %d: %f,%f,%f,likelyhood:%f\n",j,p1,p2,p3,ltheta);
  156. }
  157. }
  158. return 0;
  159. }