zoukankan      html  css  js  c++  java
  • SoftMax regression


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

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

  • 相关阅读:
    With在sql server 2005中的用法
    相互关联子查询在项目中的用法
    存储过程中@@Identity全局变量
    sql server 2008 5120错误
    如何启用 FILESTREAM
    表变量在存储过程或sql server中的运用
    Union ,Union ALL的用法
    数据移植(利用Ado.Net功能实现)
    Foreign Key ,NO action,Cascade的用法
    EXISTS 在SQL 中的用法
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205229.html
Copyright © 2011-2022 走看看