Machine Learning实验6 理解核函数

#include"stdio.h"
#include <math.h>
double function(double matrix[5][4],double theta[3],int sample_i)
{
        double ret=0.0;
        for(int i=0;i<3;++i)
        {
                ret+=matrix[sample_i][i]*theta[i];
        }
        return ret;
}
double theta[3]={1,1,1};
int main(void)
{
        double matrix[5][4]={
                        {1,1,1,1},
                        {1,1,4,2},
                        {1,1,9,3},
                        {1,1,16,4},
                        {1,1,25,5},
                    };

        double alfa = 0.1;
        double c = 0;
        double d = 0.5;

        double loss = 0.0;
        for(int j = 0;j<15;++j)
        {
                double sum=function(matrix,theta,j);
                loss += pow((pow(sum+c,d)-matrix[j][3]),2);
        }
        printf("loss : %lf\n",loss);
        for(int i=0;i<200;++i)
        {
                for(int sample_i = 0; sample_i<5;sample_i++)
                {
                        double result = function(matrix,theta,sample_i)+c;
                        for(int j=0;j<3;++j)
                        {
                                theta[j] = theta[j] - alfa*(pow(result,d)-matrix[sample_i][3])*d*pow((result),d-1)*matrix[sample_i][j];
                        }
                }
                double loss = 0.0;
                for(int j = 0;j<5;++j)
                {
                        double sum=function(matrix,theta,j);
                        loss += pow((pow(sum+c,d)-matrix[j][3]),2);
                }
                printf("%d,loss  now: %lf,%lf,%lf,%lf\n",i,loss,theta[0],theta[1],theta[2]);

        }
        return 0;
}

上面这个是多项式核函数的实验,暂时不解释。未来整体写一个博客来解释。

通过本实验加强对向量点乘的理解。

以上实验受到Multivariate calculus课程的影响 

网易公开课看到这个课程:http://v.163.com/special/opencourse/multivariable.html
MIT地址:http://ocw.mit.edu/courses/mathematics/18-02sc-multivariable-calculus-fall-2010/


我要啦免费统计


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值