Skip to content

Commit

Permalink
Merge pull request #661 from karlnapf/master
Browse files Browse the repository at this point in the history
fixes to make MKL working with X-validation
  • Loading branch information
karlnapf committed Jul 20, 2012
2 parents d5d5151 + cea8bcd commit 823ef52
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 18 deletions.
14 changes: 14 additions & 0 deletions src/shogun/evaluation/CrossValidation.cpp
Expand Up @@ -63,6 +63,15 @@ CEvaluationResult* CCrossValidation::evaluate(CModelSelectionOutput* ms_output)
{
SG_DEBUG("entering %s::evaluate()\n", get_name());

REQUIRE(m_machine, "%s::evaluate() is only possible if a machine is "
"attached\n", get_name());

REQUIRE(m_features, "%s::evaluate() is only possible if features are "
"attached\n", get_name());

REQUIRE(m_labels, "%s::evaluate() is only possible if labels are "
"attached\n", get_name());

/* if for some reason the do_unlock_frag is set, unlock */
if (m_do_unlock)
{
Expand Down Expand Up @@ -250,7 +259,9 @@ float64_t CCrossValidation::evaluate_one_run(CModelSelectionOutput* ms_output)
}

/* train machine on training features and remove subset */
SG_DEBUG("starting training\n");
m_machine->train(m_features);
SG_DEBUG("finished training\n");
if (ms_output)
{
ms_output->output_train_indices(inverse_subset_indices);
Expand All @@ -276,7 +287,10 @@ float64_t CCrossValidation::evaluate_one_run(CModelSelectionOutput* ms_output)
}

/* apply machine to test features and remove subset */
SG_DEBUG("starting evaluation\n");
SG_DEBUG("%p\n", m_features);
CLabels* result_labels=m_machine->apply(m_features);
SG_DEBUG("finished evaluation\n");
m_features->remove_subset();
SG_REF(result_labels);

Expand Down
4 changes: 2 additions & 2 deletions src/shogun/labels/Labels.cpp
Expand Up @@ -6,7 +6,7 @@
*
* Written (W) 1999-2009 Soeren Sonnenburg
* Written (W) 1999-2008 Gunnar Raetsch
* Written (W) 2011 Heiko Strathmann
* Written (W) 2011-2012 Heiko Strathmann
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/

Expand Down Expand Up @@ -54,8 +54,8 @@ void CLabels::remove_all_subsets()

float64_t CLabels::get_confidence(int32_t idx)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
ASSERT(m_confidences.vector && idx<get_num_labels());
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
return m_confidences.vector[real_num];
}

Expand Down
22 changes: 19 additions & 3 deletions src/shogun/machine/KernelMachine.cpp
Expand Up @@ -250,7 +250,10 @@ CBinaryLabels* CKernelMachine::apply_binary(CFeatures* data)

SGVector<float64_t> CKernelMachine::apply_get_outputs(CFeatures* data)
{
REQUIRE(kernel, "No kernel assigned!\n");
SG_DEBUG("entering %s::apply_get_outputs(%s at %p)\n",
get_name(), data ? data->get_name() : "NULL", data);

REQUIRE(kernel, "%s::apply_get_outputs(): No kernel assigned!\n");

if (!kernel->get_num_vec_lhs())
{
Expand All @@ -263,12 +266,22 @@ SGVector<float64_t> CKernelMachine::apply_get_outputs(CFeatures* data)
if (data)
{
CFeatures* lhs=kernel->get_lhs();
REQUIRE(lhs, "%s: No left hand side specified\n", get_name());
REQUIRE(lhs, "%s::apply_get_outputs(): No left hand side specified\n",
get_name());
kernel->init(lhs, data);
SG_UNREF(lhs);
}

int32_t num_vectors=kernel->get_num_vec_rhs();
/* using the features to get num vectors is far safer than using the kernel
* since SHOGUNs kernel num_rhs/num_lhs is buggy (CombinedKernel for ex.)
* Might be worth investigating why
* kernel->get_num_rhs() != rhs->get_num_vectors()
* However, the below version works
* TODO Heiko Strathmann
*/
CFeatures* rhs=kernel->get_rhs();
int32_t num_vectors=rhs->get_num_vectors();
SG_UNREF(rhs)

SGVector<float64_t> output(num_vectors);

Expand Down Expand Up @@ -380,6 +393,9 @@ SGVector<float64_t> CKernelMachine::apply_get_outputs(CFeatures* data)
SG_DONE();
}

SG_DEBUG("leaving %s::apply_get_outputs(%s at %p)\n",
get_name(), data ? data->get_name() : "NULL", data);

return output;
}

Expand Down
13 changes: 11 additions & 2 deletions src/shogun/machine/KernelMulticlassMachine.h
Expand Up @@ -5,6 +5,7 @@
* (at your option) any later version.
*
* Written (W) 1999-2012 Soeren Sonnenburg and Sergey Lisitsyn
* Written (W) 2012 Heiko Strathmann
* Copyright (C) 1999-2012 Fraunhofer Institute FIRST and Max-Planck-Society
*/

Expand Down Expand Up @@ -104,14 +105,22 @@ class CKernelMulticlassMachine : public CMulticlassMachine
virtual bool init_machines_for_apply(CFeatures* data)
{
if (data)
m_kernel->init(m_kernel->get_lhs(),data);
{
/* set data to rhs for this kernel */
CFeatures* lhs=m_kernel->get_lhs();
m_kernel->init(lhs, data);
SG_UNREF(lhs);
}

/* set kernel to all sub-machines */
for (int32_t i=0; i<m_machines->get_num_elements(); i++)
{
CKernelMachine *machine = (CKernelMachine*)m_machines->get_element(i);
CKernelMachine *machine=
(CKernelMachine*)m_machines->get_element(i);
machine->set_kernel(m_kernel);
SG_UNREF(machine);
}

return true;
}

Expand Down
27 changes: 21 additions & 6 deletions src/shogun/machine/Machine.cpp
Expand Up @@ -160,19 +160,34 @@ void CMachine::data_unlock()

CLabels* CMachine::apply(CFeatures* data)
{
SG_DEBUG("entering %s::apply(%s at %p)\n",
get_name(), data ? data->get_name() : "NULL", data);

CLabels* result=NULL;

switch (get_machine_problem_type())
{
case PT_BINARY:
return apply_binary(data);
result=apply_binary(data);
break;
case PT_REGRESSION:
return apply_regression(data);
result=apply_regression(data);
break;
case PT_MULTICLASS:
return apply_multiclass(data);
result=apply_multiclass(data);
break;
case PT_STRUCTURED:
return apply_structured(data);
default: SG_ERROR("Unknown problem type");
result=apply_structured(data);
break;
default:
SG_ERROR("Unknown problem type");
break;
}
return NULL;

SG_DEBUG("leaving %s::apply(%s at %p)\n",
get_name(), data ? data->get_name() : "NULL", data);

return result;
}

CLabels* CMachine::apply_locked(SGVector<index_t> indices)
Expand Down
20 changes: 15 additions & 5 deletions src/shogun/machine/MulticlassMachine.cpp
Expand Up @@ -91,14 +91,22 @@ float64_t CMulticlassMachine::get_submachine_output(int32_t i, int32_t num)

CMulticlassLabels* CMulticlassMachine::apply_multiclass(CFeatures* data)
{
SG_DEBUG("entering %s::apply_multiclass(%s at %p)\n",
get_name(), data ? data->get_name() : "NULL", data);

CMulticlassLabels* return_labels=NULL;

if (data)
init_machines_for_apply(data);
else
init_machines_for_apply(NULL);

if (is_ready())
{
int32_t num_vectors=get_num_rhs_vectors();
/* num vectors depends on whether data is provided */
int32_t num_vectors=data ? data->get_num_vectors() :
get_num_rhs_vectors();

int32_t num_machines=m_machines->get_num_elements();
if (num_machines <= 0)
SG_ERROR("num_machines = %d, did you train your machine?", num_machines);
Expand All @@ -123,13 +131,15 @@ CMulticlassLabels* CMulticlassMachine::apply_multiclass(CFeatures* data)

SG_FREE(outputs);

return result;
return_labels=result;
}
else
{
SG_ERROR("Not ready");
return NULL;
}


SG_DEBUG("leaving %s::apply_multiclass(%s at %p)\n",
get_name(), data ? data->get_name() : "NULL", data);
return return_labels;
}

bool CMulticlassMachine::train_machine(CFeatures* data)
Expand Down

0 comments on commit 823ef52

Please sign in to comment.