1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*********************************************************************
MLDemos: A User-Friendly visualization toolkit for machine learning
Copyright (C) 2010 Basilio Noris
Contact: mldemos@b4silio.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*********************************************************************/
#include <public.h>
#include "classifierMVM.h"
#include <QDebug>
using namespace std;
ClassifierMVM::ClassifierMVM()
{
SVs = 0;
alpha = 0;
svCount = 0;
b = 0;
kernel_type = 0;
degree = 1;
gamma = 0.1;
coef0 = 0.;
}
ClassifierMVM::~ClassifierMVM()
{
if(SVs)
{
FOR(i, svCount)
{
KILL(SVs[i]);
}
KILL(SVs);
}
KILL(alpha);
}
void ClassifierMVM::SetParams(u32 kernelType, float kernelParam, ivec indices, fvec alphas)
{
this->indices = indices;
this->alphas = alphas;
// default values
coef0 = 0;
gamma = 1;
switch(kernelType)
{
case 0:
kernel_type = LINEAR;
degree = 1;
break;
case 1:
kernel_type = POLY;
degree = (u32)kernelParam;
break;
case 2:
kernel_type = RBF;
gamma = kernelParam;
break;
case 3:
kernel_type = SIGMOID;
gamma = kernelParam;
break;
}
}
float Kernel(const float* x, const float *sv, const int dim, const int kernelType, const int degree, const float gamma, const float coef0)
{
float sum = 0.f;
switch(kernelType)
{
case 0: // LINEAR
{
FOR(d, dim) sum += x[d]*sv[d];
}
break;
case 1: // POLY
{
FOR(d, dim) sum += x[d]*sv[d];
sum += coef0;
sum = powf(sum, degree);
}
break;
case 2: // RBF
{
FOR(d, dim)
{
float diff = x[d]-sv[d];
sum += (diff)*(diff)*gamma;
}
sum = expf(-sum);
}
break;
case 3: // SIGMOID
break;
}
return sum;
}
void ClassifierMVM::Train(std::vector< fvec > _samples, ivec _labels)
{
if(!manualSamples.size()) return;
svCount = indices.size();
if(SVs)
{
FOR(i, svCount)
{
KILL(SVs[i]);
}
KILL(SVs);
KILL(alpha);
}
if(!indices.size()) return;
dim = manualSamples[0].size();
SVs = new float*[indices.size()];
alpha = new float[indices.size()];
FOR(i, indices.size())
{
SVs[i] = new float[dim];
FOR(d, dim)
{
SVs[i][d] = manualSamples[indices[i]][d];
}
alpha[i] = alphas[i];
}
// we compute the b;
b = 0;
float sum = 0;
FOR(i, svCount)
{
float y = 0;
FOR(j, svCount)
{
y += alpha[j]*Kernel(SVs[i], SVs[j], dim, kernel_type, degree, gamma, coef0);
}
sum += (y - manualLabels[i]);
}
b = sum / svCount;
}
float ClassifierMVM::Test( const fvec &sample ) const
{
if(!SVs || !svCount) return 0.f;
float estimate = 0;
// we compute the kernel
FOR(i, svCount)
{
estimate += alpha[i]*Kernel(&sample[0], SVs[i], dim, kernel_type, degree, gamma, coef0);
}
return estimate - b;
}
const char *ClassifierMVM::GetInfoString() const
{
char *text = new char[1024];
sprintf(text, "MVM\n");
sprintf(text, "%sKernel: ", text);
switch(kernel_type)
{
case LINEAR:
sprintf(text, "%s linear\n", text);
break;
case POLY:
sprintf(text, "%s polynomial (deg: %d bias: %.3f width: %f)\n", text, degree, coef0, gamma);
break;
case RBF:
sprintf(text, "%s rbf (gamma: %f)\n", text, gamma);
break;
case SIGMOID:
sprintf(text, "%s sigmoid (%f %f)\n", text, gamma, coef0);
break;
}
sprintf(text, "%sSupport Vectors: %d\n", text, svCount);
return text;
}
|