📄 kernel.h
字号:
// -*- C++ -*-#ifndef __LEMGA_KERNEL_H__#define __LEMGA_KERNEL_H__/** @file * @brief Kernels for SVM etc. * * $Id: kernel.h 2499 2005-11-13 09:21:35Z ling $ */#include <cmath>#include <numeric>#include "learnmodel.h"#define DOTPROD(x,y) std::inner_product(x.begin(), x.end(), y.begin(), .0)namespace lemga {// only for LIBSVM, see Kernel::set_params.struct SVM_detail;namespace kernel {inline REAL norm_1 (const Input& u, const Input& v) { REAL sum(0); Input::const_iterator x = u.begin(), y = v.begin(); for (; x != u.end(); ++x, ++y) sum += std::fabs(*x - *y); return sum;}inline REAL norm_2 (const Input& u, const Input& v) { REAL sum(0); Input::const_iterator x = u.begin(), y = v.begin(); for (; x != u.end(); ++x, ++y) { REAL d = *x - *y; sum += d * d; } return sum;}/// The operator() gives the inner-product in the transformed space.class Kernel {protected: pDataSet ptd;public: virtual ~Kernel () { /* get rid of GCC warnings */ } /// The inner-product of two input vectors. virtual REAL operator() (const Input&, const Input&) const = 0; /// Store a dataset in order to compute the kernel matrix. virtual void set_data (const pDataSet& pd) { ptd = pd; } /// The inner-product of two stored inputs with index @a i and @a j. virtual REAL matrix (UINT i, UINT j) const { return operator()(ptd->x(i), ptd->x(j)); } /// In order to keep the SVM interface simple and avoid /// member functions specific to kernels (e.g., set_gamma()), /// we use Kernel to pass kernel parameters to SVM_detail. virtual void set_params (SVM_detail*) const = 0;};struct Linear : public Kernel { virtual REAL operator() (const Input& a, const Input& b) const { return DOTPROD(a, b); } virtual void set_params (SVM_detail*) const;};struct Polynomial : public Kernel { UINT degree; REAL gamma, coef0; Polynomial (UINT d, REAL g, REAL c0) : degree(d), gamma(g), coef0(c0) {}; virtual REAL operator() (const Input& a, const Input& b) const { return std::pow(gamma * DOTPROD(a, b) + coef0, (REAL) degree); } virtual void set_params (SVM_detail*) const;};struct Stump : public Kernel { virtual REAL operator() (const Input& a, const Input& b) const { return -norm_1(a, b); } virtual void set_params (SVM_detail*) const;};struct Perceptron : public Kernel {protected: std::vector<REAL> x_norm2; ///< cached inner-product of data inputpublic: virtual REAL operator() (const Input& a, const Input& b) const { return -std::sqrt(norm_2(a, b)); } virtual void set_data (const pDataSet& pd) { Kernel::set_data(pd); const UINT n = ptd->size(); x_norm2.resize(n); for (UINT i = 0; i < n; ++i) x_norm2[i] = DOTPROD(ptd->x(i), ptd->x(i)); } virtual REAL matrix (UINT i, UINT j) const { REAL n2 = x_norm2[i] + x_norm2[j] - 2*DOTPROD(ptd->x(i), ptd->x(j)); return (n2 > 0)? -std::sqrt(n2) : 0; // avoid -0.0 } virtual void set_params (SVM_detail*) const;};struct RBF : public Perceptron { REAL gamma; explicit RBF (REAL g) : gamma(g) {} virtual REAL operator() (const Input& a, const Input& b) const { return std::exp(-gamma * norm_2(a, b)); } virtual REAL matrix (UINT i, UINT j) const { REAL n2 = x_norm2[i] + x_norm2[j] - 2*DOTPROD(ptd->x(i), ptd->x(j)); return std::exp(-gamma * n2); } virtual void set_params (SVM_detail*) const;};struct Sigmoid : public Kernel { REAL gamma, coef0; Sigmoid (REAL g, REAL c0) : gamma(g), coef0(c0) {}; virtual REAL operator() (const Input& a, const Input& b) const { return std::tanh(gamma * DOTPROD(a, b) + coef0); } virtual void set_params (SVM_detail*) const;};}} // namespace lemga::kernel#ifdef __KERNEL_H__#warning "This header file may conflict with another `kernel.h' file."#endif#define __KERNEL_H__#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -