LPM.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2007-2009 Soeren Sonnenburg
00008  * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include <shogun/lib/config.h>
00012 
00013 #ifdef USE_CPLEX
00014 
00015 #include <shogun/classifier/LPM.h>
00016 #include <shogun/features/Labels.h>
00017 #include <shogun/mathematics/Math.h>
00018 #include <shogun/mathematics/Cplex.h>
00019 
00020 using namespace shogun;
00021 
00022 CLPM::CLPM()
00023 : CLinearClassifier(), C1(1), C2(1), use_bias(true), epsilon(1e-3)
00024 {
00025 }
00026 
00027 
00028 CLPM::~CLPM()
00029 {
00030 }
00031 
00032 bool CLPM::train_machine(CFeatures* data)
00033 {
00034     ASSERT(labels);
00035     if (data)
00036     {
00037         if (!data->has_property(FP_DOT))
00038             SG_ERROR("Specified features are not of type CDotFeatures\n");
00039         set_features((CDotFeatures*) data);
00040     }
00041     ASSERT(features);
00042     int32_t num_train_labels=labels->get_num_labels();
00043     int32_t num_feat=features->get_dim_feature_space();
00044     int32_t num_vec=features->get_num_vectors();
00045 
00046     ASSERT(num_vec==num_train_labels);
00047     SG_FREE(w);
00048     w=SG_MALLOC(float64_t, num_feat);
00049     w_dim=num_feat;
00050 
00051     int32_t num_params=1+2*num_feat+num_vec; //b,w+,w-,xi
00052     float64_t* params=SG_MALLOC(float64_t, num_params);
00053     memset(params,0,sizeof(float64_t)*num_params);
00054 
00055     CCplex solver;
00056     solver.init(E_LINEAR);
00057     SG_INFO("C=%f\n", C1);
00058     solver.setup_lpm(C1, (CSparseFeatures<float64_t>*) features, labels, get_bias_enabled());
00059     if (get_max_train_time()>0)
00060         solver.set_time_limit(get_max_train_time());
00061     bool result=solver.optimize(params);
00062     solver.cleanup();
00063 
00064     set_bias(params[0]);
00065     for (int32_t i=0; i<num_feat; i++)
00066         w[i]=params[1+i]-params[1+num_feat+i];
00067 
00068 //#define LPM_DEBUG
00069 #ifdef LPM_DEBUG
00070     CMath::display_vector(params,num_params, "params");
00071     SG_PRINT("bias=%f\n", bias);
00072     CMath::display_vector(w,w_dim, "w");
00073     CMath::display_vector(&params[1],w_dim, "w+");
00074     CMath::display_vector(&params[1+w_dim],w_dim, "w-");
00075 #endif
00076     SG_FREE(params);
00077 
00078     return result;
00079 }
00080 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation