Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
268 additions
and
42 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
examples/undocumented/python_modular/classifier_multiclassliblinear_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
traindat = lm.load_numbers('../data/fm_train_real.dat') | ||
testdat = lm.load_numbers('../data/fm_test_real.dat') | ||
label_traindat = lm.load_labels('../data/label_train_multiclass.dat') | ||
|
||
parameter_list = [[traindat,testdat,label_traindat,2.1,1,1e-5],[traindat,testdat,label_traindat,2.2,1,1e-5]] | ||
|
||
def classifier_multiclassliblinear_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,width=2.1,C=1,epsilon=1e-5): | ||
from shogun.Features import RealFeatures, Labels | ||
from shogun.Classifier import MulticlassLibLinear | ||
|
||
feats_train=RealFeatures(fm_train_real) | ||
feats_test=RealFeatures(fm_test_real) | ||
|
||
labels=Labels(label_train_multiclass) | ||
|
||
classifier = MulticlassLibLinear(C,feats_train,labels) | ||
classifier.train() | ||
|
||
out = classifier.apply(feats_test).get_labels() | ||
return out | ||
|
||
if __name__=='__main__': | ||
print 'MulticlassLibLinear' | ||
classifier_multiclassliblinear_modular(*parameter_list[0]) |
32 changes: 32 additions & 0 deletions
32
examples/undocumented/python_modular/classifier_multiclassmachine_modular.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from tools.load import LoadMatrix | ||
lm=LoadMatrix() | ||
|
||
traindat = lm.load_numbers('../data/fm_train_real.dat') | ||
testdat = lm.load_numbers('../data/fm_test_real.dat') | ||
label_traindat = lm.load_labels('../data/label_train_multiclass.dat') | ||
|
||
parameter_list = [[traindat,testdat,label_traindat,2.1,1,1e-5],[traindat,testdat,label_traindat,2.2,1,1e-5]] | ||
|
||
def classifier_multiclassmachine_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,width=2.1,C=1,epsilon=1e-5): | ||
from shogun.Features import RealFeatures, Labels | ||
from shogun.Kernel import GaussianKernel | ||
from shogun.Classifier import LibSVM, KernelMulticlassMachine, ONE_VS_REST_STRATEGY | ||
|
||
feats_train=RealFeatures(fm_train_real) | ||
feats_test=RealFeatures(fm_test_real) | ||
kernel=GaussianKernel(feats_train, feats_train, width) | ||
|
||
labels=Labels(label_train_multiclass) | ||
|
||
classifier = LibSVM(C, kernel, labels) | ||
classifier.set_epsilon(epsilon) | ||
mc_classifier = KernelMulticlassMachine(ONE_VS_REST_STRATEGY,kernel,classifier,labels) | ||
mc_classifier.train() | ||
|
||
kernel.init(feats_train, feats_test) | ||
out = mc_classifier.apply().get_labels() | ||
return out | ||
|
||
if __name__=='__main__': | ||
print 'MulticlassMachine' | ||
classifier_multiclassmachine_modular(*parameter_list[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Sergey Lisitsyn | ||
* Copyright (C) 2012 Sergey Lisitsyn | ||
*/ | ||
|
||
|
||
#include <shogun/classifier/svm/MulticlassLibLinear.h> | ||
#include <shogun/mathematics/Math.h> | ||
|
||
using namespace shogun; | ||
|
||
bool CMulticlassLibLinear::train_machine(CFeatures* data) | ||
{ | ||
if (data) | ||
set_features((CDotFeatures*)data); | ||
|
||
int32_t num_vectors = m_features->get_num_vectors(); | ||
int32_t num_classes = labels->get_num_classes(); | ||
|
||
problem mc_problem; | ||
mc_problem.l = num_vectors; | ||
mc_problem.n = m_features->get_dim_feature_space(); | ||
mc_problem.y = SG_MALLOC(int32_t, mc_problem.l); | ||
for (int32_t i=0; i<num_vectors; i++) | ||
mc_problem.y[i] = labels->get_int_label(i); | ||
|
||
mc_problem.x = m_features; | ||
mc_problem.use_bias = true; | ||
|
||
float64_t* w = SG_MALLOC(float64_t, mc_problem.n*num_classes); | ||
float64_t* C = SG_MALLOC(float64_t, num_vectors); | ||
for (int32_t i=0; i<num_vectors; i++) | ||
C[i] = m_C; | ||
|
||
Solver_MCSVM_CS solver(&mc_problem,num_classes,C,m_epsilon,m_max_iter); | ||
solver.Solve(w); | ||
|
||
m_machines.destroy_vector(); | ||
m_machines = SGVector<CMachine*>(num_classes); | ||
for (int32_t i=0; i<num_classes; i++) | ||
{ | ||
CLinearMachine* machine = new CLinearMachine(); | ||
SGVector<float64_t> cw(mc_problem.n); | ||
for (int32_t j=0; j<mc_problem.n; j++) | ||
cw[j] = w[j*num_classes+i]; | ||
machine->set_w(cw); | ||
|
||
m_machines[i] = machine; | ||
} | ||
|
||
SG_FREE(C); | ||
SG_FREE(w); | ||
SG_FREE(mc_problem.y); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Written (W) 2012 Sergey Lisitsyn | ||
* Copyright (C) 2012 Sergey Lisitsyn | ||
*/ | ||
|
||
#ifndef _MULTICLASSLIBLINEAR_H___ | ||
#define _MULTICLASSLIBLINEAR_H___ | ||
|
||
#include <shogun/lib/common.h> | ||
#include <shogun/features/DotFeatures.h> | ||
#include <shogun/classifier/svm/SVM_linear.h> | ||
#include <shogun/machine/LinearMulticlassMachine.h> | ||
|
||
namespace shogun | ||
{ | ||
|
||
/** @brief multiclass LibLinear wrapper */ | ||
class CMulticlassLibLinear : public CLinearMulticlassMachine | ||
{ | ||
public: | ||
/** default constructor */ | ||
CMulticlassLibLinear() : CLinearMulticlassMachine() | ||
{ | ||
} | ||
|
||
/** standard constructor | ||
* @param C C regularication constant value | ||
* @param features features | ||
* @param labs labels | ||
*/ | ||
CMulticlassLibLinear(float64_t C, CDotFeatures* features, CLabels* labs) : | ||
CLinearMulticlassMachine(ONE_VS_REST_STRATEGY,features,NULL,labs), m_C(C) | ||
{ | ||
set_epsilon(1e-2); | ||
set_max_iter(10000); | ||
} | ||
|
||
/** destructor */ | ||
virtual ~CMulticlassLibLinear() | ||
{ | ||
} | ||
|
||
/** get name */ | ||
virtual const char* get_name() const | ||
{ | ||
return "MulticlassLibLinear"; | ||
} | ||
|
||
/** set C | ||
* @param C C value | ||
*/ | ||
inline void set_C(int32_t C) | ||
{ | ||
ASSERT(C>0); | ||
m_C = C; | ||
} | ||
/** get C | ||
* @return C value | ||
*/ | ||
inline int32_t get_C() const { return m_C; } | ||
|
||
/** set epsilon | ||
* @param epsilon epsilon value | ||
*/ | ||
inline void set_epsilon(float64_t epsilon) | ||
{ | ||
ASSERT(epsilon>0); | ||
m_epsilon = epsilon; | ||
} | ||
/** get epsilon | ||
* @return epsilon value | ||
*/ | ||
inline float64_t get_epsilon() const { return m_epsilon; } | ||
|
||
/** set max iter | ||
* @param max_iter max iter value | ||
*/ | ||
inline void set_max_iter(int32_t max_iter) | ||
{ | ||
ASSERT(max_iter>0); | ||
m_max_iter = max_iter; | ||
} | ||
/** get max iter | ||
* @return max iter value | ||
*/ | ||
inline int32_t get_max_iter() const { return m_max_iter; } | ||
|
||
|
||
protected: | ||
|
||
/** train machine */ | ||
virtual bool train_machine(CFeatures* data = NULL); | ||
|
||
protected: | ||
|
||
/** regularization constant for each machine */ | ||
float64_t m_C; | ||
|
||
/** tolerance */ | ||
float64_t m_epsilon; | ||
|
||
/** max number of iterations */ | ||
int32_t m_max_iter; | ||
}; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.