⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sv_kernel.c

📁 用VC++编写的若干个机器学习中的核函数
💻 C
字号:
/*------------------------------data_instruction-------------------------------------
   excute the mode as:     svkernel(u,v,VT,K,'P',p,Width,Height);

   u and v are kernel arguments;
   VT is the rotation of v;
   K is the output of the kernal;
   'L' or 'P' or 'R' is the different kernel;
   p is the parameter of kernel;
   Width and Height are the Width and the Height of the input-data;
*----------------------------------------------------------------------------------*/ 

#include"stdio.h"
#include"malloc.h"                  //dynamic  storing
#include"stdlib.h"
#include"math.h"

#define linear L
#define poly   P
#define rbf    R

void svkernel(double **,double **,double **,double **,char ch,int p0,int Dx,int Dy);
void linear_op(double **,double **,double **,double **,int Dx,int Dy);
void poly_op(double **,double **,double **,double **,int p1,int Dx,int Dy);
void rbf_op(double **,double **,double **,double **,int p1,int Dx,int Dy);



void svkernel(double **u,double **v,double **VT,double **K,char ch,int p0,int Dx,int Dy)//kernel operation
{
	switch(ch)
	{
	case 'L':
		linear_op(u,v,VT,K,Dx,Dy);//linear kernel
		break;
	case 'P':
		poly_op(u,v,VT,K,p0,Dx,Dy);//polynomial kernel
		break;
	case 'R':
		rbf_op(u,v,VT,K,p0,Dx,Dy);//rbf kernel
		break;

	}

}


void linear_op(double **u ,double **v ,double **VT,double **K,int Dx,int Dy)//linear kernel
{
int i,j,k;
double dot_m=0.0;

for(i=0;i<Dy;i++)//transpose
{
	for(j=0;j<Dx;j++)
	{
		VT[j][i]=v[i][j];
	}
}

for(i=0;i<Dy;i++)
{
	for(j=0;j<Dy;j++)//dot product
	{
		for(k=0;k<Dx;k++)
		{
			dot_m+=u[i][k]*VT[k][j];
		}
		K[i][j]=dot_m;

	}
  }

}


void poly_op(double **u,double **v,double **VT,double **K,int p1,int Dx,int Dy)//polynomial kernel
{
int i,j,k,l;
double dot_m_p=0.0;

linear_op(u,v,VT,K,Dx,Dy);//linear kernel is used


for(l=0;l<p1;l++)
{

for(i=0;i<Dy;i++)//dot product
{
	for(j=0;j<Dy;j++)
	{
		for(k=0;k<Dy;k++)
		{
			dot_m_p+=K[i][k]*K[k][j];
		}
		K[i][j]=dot_m_p;
	}
}
}

}


void rbf_op(double **u,double **v,double **VT,double **K,int p1,int Dx,int Dy)//rbf kernel 
{
int i,j,k;
double dot_m_r=0.0;
double **UV,**UVT;

UV=(double **)malloc(Dy*sizeof(double *));                                //apply for 2d memory space 
for(i=0;i<Dy;i++)
	UV[i]=(double*)malloc(Dx*sizeof(double));

UVT=(double **)malloc(Dx*sizeof(double *));
for(i=0;i<Dx;i++)
	UVT[i]=(double*)malloc(Dy*sizeof(double));

for(i=0;i<Dy;i++)//the deduction of two kinds of data
{ 
	for(j=0;j<Dx;j++)
	{
		UV[i][j]=u[i][j]-v[i][j];
	}
}

for(i=0;i<Dy;i++)//transpose
{
	for(j=0;j<Dx;j++)
	{
		UVT[j][i]=UV[i][j];
	}
}

for(i=0;i<Dy;i++)
{
	for(j=0;j<Dy;j++)
	{
		for(k=0;k<Dx;k++)//dot product
		{
			dot_m_r+=UV[i][k]*UVT[k][j];
		}
		K[i][j]=exp(-dot_m_r)/(2*p1*p1);
	}
}
free(UV);
free(UVT);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -