Skip to content

Commit

Permalink
Merge pull request #258 from karlnapf/master
Browse files Browse the repository at this point in the history
model selection now really works on model_selection_parameters
  • Loading branch information
Soeren Sonnenburg committed Aug 2, 2011
2 parents a2be4dd + 8c24d9e commit 6387718
Show file tree
Hide file tree
Showing 29 changed files with 266 additions and 142 deletions.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -17,6 +17,7 @@ TARGETS = basic_minimal classifier_libsvm classifier_minimal_svm \
library_dyn_int library_gc_array library_indirect_object \
library_hash parameter_set_from_parameters \
parameter_iterate_float64 parameter_iterate_sgobject \
parameter_modsel_parameters \
modelselection_parameter_combination_test \
modelselection_model_selection_parameters_test \
modelselection_parameter_tree \
Expand Down
Expand Up @@ -76,7 +76,7 @@ void apply_parameter_tree(CDynamicObjectArray<CParameterCombination>* combinatio
CParameterCombination* current_combination=combinations->get_element(i);
current_combination->print_tree();
Parameter* current_parameters=svm->m_parameters;
current_combination->apply_to_parameter(current_parameters);
current_combination->apply_to_modsel_parameter(current_parameters);
SG_UNREF(current_combination);

/* get kernel to set features, get_kernel SG_REF's the kernel */
Expand Down
Expand Up @@ -139,7 +139,6 @@ int main(int argc, char **argv)
SG_UNREF(best_combination);
SG_UNREF(grid_search);

SG_SPRINT("\nEND\n");
exit_shogun();

return 0;
Expand Down
Expand Up @@ -97,7 +97,6 @@ int main(int argc, char **argv)
SG_UNREF(best_combination);
SG_UNREF(grid_search);

SG_SPRINT("\nEND\n");
exit_shogun();

return 0;
Expand Down
Expand Up @@ -210,8 +210,6 @@ int main(int argc, char **argv)
test_get_combinations(tree);
SG_UNREF(tree);

SG_SPRINT("END\n");

exit_shogun();

return 0;
Expand Down
Expand Up @@ -159,7 +159,6 @@ int main(int argc, char **argv)

test_parameter_set_multiplication();
test_leaf_sets_multiplication();
SG_SPRINT("END\n");

exit_shogun();

Expand Down
Expand Up @@ -29,7 +29,7 @@ CModelSelectionParameters* create_param_tree()

CModelSelectionParameters* c=new CModelSelectionParameters("C");
root->append_child(c);
c->build_values(1, 11, R_EXP);
c->build_values(1, 2, R_EXP);

CPowerKernel* power_kernel=new CPowerKernel();
CModelSelectionParameters* param_power_kernel=
Expand All @@ -39,7 +39,7 @@ CModelSelectionParameters* create_param_tree()

CModelSelectionParameters* param_power_kernel_degree=
new CModelSelectionParameters("degree");
param_power_kernel_degree->build_values(1, 1, R_EXP);
param_power_kernel_degree->build_values(1, 2, R_EXP);
param_power_kernel->append_child(param_power_kernel_degree);

CMinkowskiMetric* m_metric=new CMinkowskiMetric(10);
Expand All @@ -50,7 +50,7 @@ CModelSelectionParameters* create_param_tree()

CModelSelectionParameters* param_power_kernel_metric1_k=
new CModelSelectionParameters("k");
param_power_kernel_metric1_k->build_values(1, 12, R_LINEAR);
param_power_kernel_metric1_k->build_values(1, 2, R_LINEAR);
param_power_kernel_metric1->append_child(param_power_kernel_metric1_k);

CGaussianKernel* gaussian_kernel=new CGaussianKernel();
Expand Down Expand Up @@ -110,8 +110,6 @@ int main(int argc, char **argv)
/* delete example tree */
SG_UNREF(tree);

SG_SPRINT("END\n");

exit_shogun();

return 0;
Expand Down
83 changes: 83 additions & 0 deletions examples/undocumented/libshogun/parameter_modsel_parameters.cpp
@@ -0,0 +1,83 @@
/*
* 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) 2011 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

#include <shogun/classifier/svm/LibSVM.h>
#include <shogun/classifier/svm/LibLinear.h>
#include <shogun/kernel/DistantSegmentsKernel.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/kernel/PowerKernel.h>
#include <shogun/distance/MinkowskiMetric.h>

using namespace shogun;

void print_message(FILE* target, const char* str)
{
fprintf(target, "%s", str);
}

void print_modsel_parameters(CSGObject* object)
{
SGVector<char*> modsel_params=object->get_modelsel_names();

SG_SPRINT("Parameters of %s available for model selection:\n",
object->get_name());

char* type_string=SG_MALLOC(char, 100);
for (index_t i=0; i<modsel_params.vlen; ++i)
{
/* extract current name, ddescription and type, and print them */
const char* name=modsel_params.vector[i];
index_t index=object->get_modsel_param_index(name);
TSGDataType type=object->m_model_selection_parameters->get_parameter(
index)->m_datatype;
type.to_string(type_string, 100);
SG_SPRINT("\"%s\": \"%s\", %s\n", name,
object->get_modsel_param_descr(name), type_string);
}
SG_FREE(type_string);

modsel_params.destroy_vector();
SG_SPRINT("\n");
}

int main(int argc, char** argv)
{
init_shogun(&print_message);

CSGObject* object;

object=new CLibSVM();
print_modsel_parameters(object);
SG_UNREF(object);

object=new CLibLinear();
print_modsel_parameters(object);
SG_UNREF(object);

object=new CDistantSegmentsKernel();
print_modsel_parameters(object);
SG_UNREF(object);

object=new CGaussianKernel();
print_modsel_parameters(object);
SG_UNREF(object);

object=new CPowerKernel();
print_modsel_parameters(object);
SG_UNREF(object);

object=new CMinkowskiMetric();
print_modsel_parameters(object);
SG_UNREF(object);

exit_shogun();
return 0;
}

2 changes: 2 additions & 0 deletions src/shogun/base/Parameter.cpp
Expand Up @@ -2370,6 +2370,8 @@ void Parameter::set_from_parameters(Parameter* params)
TParameter* current=params->get_parameter(i);
TSGDataType current_type=current->m_datatype;

ASSERT(m_params.get_num_elements());

/* search for own parameter with same name and check types if found */
TParameter* own=NULL;
for (index_t j=0; j<m_params.get_num_elements(); ++j)
Expand Down
50 changes: 48 additions & 2 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -205,8 +205,6 @@ void CSGObject::print_serializable(const char* prefix)
{
SG_PRINT("\n%s\n================================================================================\n", get_name());
m_parameters->print(prefix);
SG_PRINT("\nParameters available for model selection:\n");
m_model_selection_parameters->print(prefix);
}

bool CSGObject::save_serializable(CSerializableFile* file,
Expand Down Expand Up @@ -360,3 +358,51 @@ void CSGObject::init()
m_load_pre_called = false;
m_load_post_called = false;
}

SGVector<char*> CSGObject::get_modelsel_names()
{
SGVector<char*> result=SGVector<char*>(
m_model_selection_parameters->get_num_parameters());

for (index_t i=0; i<result.vlen; ++i)
result.vector[i]=m_model_selection_parameters->get_parameter(i)->m_name;

return result;
}

char* CSGObject::get_modsel_param_descr(const char* param_name)
{
index_t index=get_modsel_param_index(param_name);

if (index<0)
{
SG_ERROR("There is no model selection parameter called \"%s\" for %s",
param_name, get_name());
}

return m_model_selection_parameters->get_parameter(index)->m_description;
}

index_t CSGObject::get_modsel_param_index(const char* param_name)
{
/* use fact that names extracted from below method are in same order than
* in m_model_selection_parameters variable */
SGVector<char*> names=get_modelsel_names();

/* search for parameter with provided name */
index_t index=-1;
for (index_t i=0; i<names.vlen; ++i)
{
TParameter* current=m_model_selection_parameters->get_parameter(i);
if (!strcmp(param_name, current->m_name))
{
index=i;
break;
}
}

/* clean up */
names.destroy_vector();

return index;
}
35 changes: 20 additions & 15 deletions src/shogun/base/SGObject.h
Expand Up @@ -57,21 +57,6 @@ class CSerializableFile;
if (ms_available)\
m_model_selection_parameters->add(param, name, description);\
}

#define SG_ADD_VECTOR(param, length, name, description, ms_available) {\
m_parameters->add_vector(param, length, name, description);\
if (ms_available)\
m_model_selection_parameters->add_vector(param, length, name,\
description);\
}

#define SG_ADD_MATRIX(param, length_y, length_x, name, description,\
ms_available) {\
m_parameters->add_matrix(param, length_y, length_x, name, description);\
if (ms_available)\
m_model_selection_parameters->add_matrix(param, length_y, length_x,\
name, description);\
}
/*******************************************************************************
* End of macros for registering parameters/model selection parameters
******************************************************************************/
Expand Down Expand Up @@ -245,6 +230,26 @@ class CSGObject
*/
Version* get_global_version();

/** @return vector of names of all parameters which are registered for model
* selection */
SGVector<char*> get_modelsel_names();

/** Returns description of a given parameter string, if it exists. SG_ERROR
* otherwise
*
* @param modsel_param name of the parameter
* @return description of the parameter
*/
char* get_modsel_param_descr(const char* param_name);

/** Returns index of model selection parameter with provided index
*
* @param param_name name of model selection parameter
* @return index of model selection parameter with provided name,
* -1 if there is no such
*/
index_t get_modsel_param_index(const char* param_name);

#ifdef TRACE_MEMORY_ALLOCS
static void list_memory_allocs()
{
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/classifier/svm/DomainAdaptationSVMLinear.cpp
Expand Up @@ -130,7 +130,8 @@ bool CDomainAdaptationSVMLinear::train_machine(CDotFeatures* train_data)
}

// set linear term for QP
this->set_linear_term(&lin_term[0], lin_term.size());
this->set_linear_term(
SGVector<float64_t>(&lin_term[0], lin_term.size()));

}

Expand Down
41 changes: 19 additions & 22 deletions src/shogun/classifier/svm/LibLinear.cpp
Expand Up @@ -56,22 +56,23 @@ void CLibLinear::init()
C1=1;
C2=1;
set_max_iterations();
m_linear_term=NULL;
m_linear_term_len=0;
epsilon=1e-5;

m_parameters->add(&C1, "C1", "C Cost constant 1.");
m_parameters->add(&C2, "C2", "C Cost constant 2.");
m_parameters->add(&use_bias, "use_bias", "Indicates if bias is used.");
m_parameters->add(&epsilon, "epsilon", "Convergence precision.");
m_parameters->add(&max_iterations, "max_iterations", "Max number of iterations.");
m_parameters->add_vector(&m_linear_term, &m_linear_term_len, "linear_term", "Linear Term");
m_parameters->add((machine_int_t*) &liblinear_solver_type, "liblinear_solver_type", "Type of LibLinear solver.");
SG_ADD(&C1, "C1", "C Cost constant 1.", MS_AVAILABLE);
SG_ADD(&C2, "C2", "C Cost constant 2.", MS_AVAILABLE);
SG_ADD(&use_bias, "use_bias", "Indicates if bias is used.",
MS_NOT_AVAILABLE);
SG_ADD(&epsilon, "epsilon", "Convergence precision.", MS_NOT_AVAILABLE);
SG_ADD(&max_iterations, "max_iterations", "Max number of iterations.",
MS_NOT_AVAILABLE);
SG_ADD(&m_linear_term, "linear_term", "Linear Term", MS_NOT_AVAILABLE);
SG_ADD((machine_int_t*) &liblinear_solver_type, "liblinear_solver_type",
"Type of LibLinear solver.", MS_NOT_AVAILABLE);
}

CLibLinear::~CLibLinear()
{
SG_FREE(m_linear_term);
m_linear_term.destroy_vector();
}

bool CLibLinear::train_machine(CFeatures* data)
Expand Down Expand Up @@ -329,8 +330,8 @@ void CLibLinear::solve_l2r_l1l2_svc(
if (prob->use_bias)
G+=w[n];

if (m_linear_term)
G = G*yi + m_linear_term[i];
if (m_linear_term.vector)
G = G*yi + m_linear_term.vector[i];
else
G = G*yi-1;

Expand Down Expand Up @@ -1144,27 +1145,23 @@ void CLibLinear::solve_l1r_lr(
delete [] xjpos_sum;
}

void CLibLinear::get_linear_term(float64_t** linear_term, int32_t* len)
SGVector<float64_t> CLibLinear::get_linear_term()
{
if (!m_linear_term_len || !m_linear_term)
if (!m_linear_term.vlen || !m_linear_term.vector)
SG_ERROR("Please assign linear term first!\n");

*linear_term=SG_MALLOC(float64_t, m_linear_term_len);

for (int32_t i=0; i<m_linear_term_len; i++)
(*linear_term)[i]=m_linear_term[i];
return m_linear_term;
}

void CLibLinear::init_linear_term()
{
if (!labels)
SG_ERROR("Please assign labels first!\n");

SG_FREE(m_linear_term);
m_linear_term.destroy_vector();

m_linear_term_len=labels->get_num_labels();
m_linear_term = SG_MALLOC(float64_t, m_linear_term_len);
CMath::fill_vector(m_linear_term, m_linear_term_len, -1.0);
m_linear_term=SGVector<float64_t>(labels->get_num_labels());
CMath::fill_vector(m_linear_term.vector, m_linear_term.vlen, -1.0);
}

#endif //HAVE_LAPACK

0 comments on commit 6387718

Please sign in to comment.