kernel.cpp

来自「使用KernelBDA实现三维模型相关反馈的算法注:1、使用本系统必须在本地计算」· C++ 代码 · 共 131 行

CPP
131
字号
#include "StdAfx.h"
#include ".\kernel.h"

//CKernel::CKernel(void)
//{
//}
//
//CKernel::~CKernel(void)
//{
//}

Kernel::Kernel(int l, svm_node * const * x_, const svm_parameter& param):kernel_type(param.kernel_type), degree(param.degree),gamma(param.gamma), coef0(param.coef0){	switch(kernel_type)	{	case LINEAR:		kernel_function = &Kernel::kernel_linear;		break;	case POLY:		kernel_function = &Kernel::kernel_poly;		break;	case RBF:		kernel_function = &Kernel::kernel_rbf;		break;	case SIGMOID:		kernel_function = &Kernel::kernel_sigmoid;		break;	}	clone(x,x_,l);	if(kernel_type == RBF)	{		x_square = new double[l];		for(int i=0;i<l;i++)			x_square[i] = dot(x[i],x[i]);	}	else		x_square = 0;}Kernel::~Kernel(){	delete[] x;	delete[] x_square;}double Kernel::dot(const svm_node *px, const svm_node *py){	double sum = 0;	while(px->index != -1 && py->index != -1)	{		if(px->index == py->index)		{			sum += px->value * py->value;			++px;			++py;		}		else		{			if(px->index > py->index)				++py;			else				++px;		}				}	return sum;}//////////////////////////////////////////////////////////////////////////
//k_function和kernel_function是一样的,不同在于k_function用于预测时的单个处理double Kernel::k_function(const svm_node *x, const svm_node *y,						  const svm_parameter& param){	switch(param.kernel_type)	{	case LINEAR:		return dot(x,y);	case POLY:		return pow(param.gamma*dot(x,y)+param.coef0,param.degree);	case RBF:		{			double sum = 0;			while(x->index != -1 && y->index !=-1)			{				if(x->index == y->index)				{					double d = x->value - y->value;					sum += d*d;					++x;					++y;				}				else				{					if(x->index > y->index)					{							sum += y->value * y->value;						++y;					}					else					{						sum += x->value * x->value;						++x;					}				}			}			while(x->index != -1)			{				sum += x->value * x->value;				++x;			}			while(y->index != -1)			{				sum += y->value * y->value;				++y;			}			return exp(-param.gamma*sum);		}	case SIGMOID:		return tanh(param.gamma*dot(x,y)+param.coef0);	default:		return 0;	/* Unreachable */	}}

⌨️ 快捷键说明

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