Skip to content

Commit

Permalink
Merge pull request #547 from karlnapf/master
Browse files Browse the repository at this point in the history
apply_locked label case distinctions
  • Loading branch information
karlnapf committed May 22, 2012
2 parents 4d58627 + 18894b6 commit 2a49637
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 24 deletions.
20 changes: 14 additions & 6 deletions examples/undocumented/libshogun/kernel_machine_train_locked.cpp
Expand Up @@ -28,7 +28,7 @@ void test()
index_t num_vectors=6;
index_t num_features=2;

/* data means -1, 1 in all components, std deviation of 3 */
/* data means -1, 1 in all components, small std deviation */
SGVector<float64_t> mean_1(num_features);
SGVector<float64_t> mean_2(num_features);
CMath::fill_vector(mean_1.vector, mean_1.vlen, -10.0);
Expand Down Expand Up @@ -80,20 +80,23 @@ void test()
svm->set_epsilon(svm_eps);

/* now train a few times on different subsets on data and assert that
* results are correc (data linear separable) */
* results are correct (data linear separable) */

svm->data_lock(labels, features);

SGVector<index_t> indices(4);
SGVector<index_t> indices(5);
indices.vector[0]=1;
indices.vector[1]=2;
indices.vector[2]=3;
indices.vector[3]=4;
indices.vector[4]=5;
CMath::display_vector(indices.vector, indices.vlen, "training indices");
svm->train_locked(indices);
CBinaryLabels* output=CBinaryLabels::obtain_from_generic(svm->apply());
ASSERT(eval->evaluate(output, labels)==1);
CMath::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output");
CMath::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels");
SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels));
ASSERT(eval->evaluate(output, labels)==1);
SG_UNREF(output);

SG_SPRINT("\n\n");
Expand All @@ -103,8 +106,10 @@ void test()
indices.vector[2]=3;
CMath::display_vector(indices.vector, indices.vlen, "training indices");
output=CBinaryLabels::obtain_from_generic(svm->apply());
ASSERT(eval->evaluate(output, labels)==1);
CMath::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output");
CMath::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels");
SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels));
ASSERT(eval->evaluate(output, labels)==1);
SG_UNREF(output);

SG_SPRINT("\n\n");
Expand All @@ -113,8 +118,10 @@ void test()
CMath::display_vector(indices.vector, indices.vlen, "training indices");
svm->train_locked(indices);
output=CBinaryLabels::obtain_from_generic(svm->apply());
ASSERT(eval->evaluate(output, labels)==1);
CMath::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output");
CMath::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels");
SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels));
ASSERT(eval->evaluate(output, labels)==1);
SG_UNREF(output);

SG_SPRINT("normal train\n");
Expand All @@ -123,6 +130,7 @@ void test()
output=CBinaryLabels::obtain_from_generic(svm->apply());
ASSERT(eval->evaluate(output, labels)==1);
CMath::display_vector(output->get_labels().vector, output->get_num_labels(), "output");
CMath::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels");
SG_UNREF(output);

/* clean up */
Expand Down
Expand Up @@ -128,6 +128,8 @@ int main(int argc, char **argv)
CCrossValidation* cross=new CCrossValidation(classifier, features, labels,
splitting_strategy, evaluation_criterium);
cross->set_num_runs(1);
/* note that this automatically is not necessary since done automatically */
cross->set_autolock(true);

/* print all parameter available for modelselection
* Dont worry if yours is not included, simply write to the mailing list */
Expand All @@ -144,7 +146,6 @@ int main(int argc, char **argv)
bool print_state=true;
CParameterCombination* best_combination=grid_search->select_model(
print_state);
SG_SPRINT("best parameter(s):\n");
best_combination->print_tree();

best_combination->apply_to_machine(classifier);
Expand All @@ -156,6 +157,14 @@ int main(int argc, char **argv)
SG_SPRINT("result: ");
result.print_result();

/* now again but unlocked */
SG_UNREF(best_combination);
cross->set_autolock(true);
best_combination=grid_search->select_model(print_state);
best_combination->apply_to_machine(classifier);
result=cross->evaluate();
SG_SPRINT("result (unlocked): ");

/* clean up destroy result parameter */
SG_UNREF(best_combination);
SG_UNREF(grid_search);
Expand Down
24 changes: 16 additions & 8 deletions src/shogun/machine/KernelMachine.cpp
Expand Up @@ -250,12 +250,6 @@ CBinaryLabels* CKernelMachine::apply_binary(CFeatures* data)

SGVector<float64_t> CKernelMachine::apply_get_outputs(CFeatures* data)
{
if (is_data_locked())
{
SG_ERROR("CKernelMachine::apply(CFeatures*) cannot be called when "
"data_lock was called before. Call data_unlock to allow.");
}

if (!kernel)
SG_ERROR("No kernel assigned!\n");

Expand Down Expand Up @@ -498,7 +492,21 @@ bool CKernelMachine::train_locked(SGVector<index_t> indices)
return result;
}

CLabels* CKernelMachine::apply_locked(SGVector<index_t> indices)
CBinaryLabels* CKernelMachine::apply_locked_binary(SGVector<index_t> indices)
{
SGVector<float64_t> outputs = apply_locked_get_output(indices);
return new CBinaryLabels(outputs);
}

CRegressionLabels* CKernelMachine::apply_locked_regression(
SGVector<index_t> indices)
{
SGVector<float64_t> outputs = apply_locked_get_output(indices);
return new CRegressionLabels(outputs);
}

SGVector<float64_t> CKernelMachine::apply_locked_get_output(
SGVector<index_t> indices)
{
if (!is_data_locked())
SG_ERROR("CKernelMachine::apply_locked() call data_lock() before!\n");
Expand Down Expand Up @@ -586,7 +594,7 @@ CLabels* CKernelMachine::apply_locked(SGVector<index_t> indices)
#endif
SG_DONE();

return new CRegressionLabels(output);
return output;
}

void CKernelMachine::data_lock(CLabels* labs, CFeatures* features)
Expand Down
21 changes: 20 additions & 1 deletion src/shogun/machine/KernelMachine.h
Expand Up @@ -242,12 +242,31 @@ class CKernelMachine : public CMachine
*/
virtual bool train_locked(SGVector<index_t> indices);

/** Applies a locked machine on a set of indices. Error if machine is
* not locked. Binary case
*
* @param indices index vector (of locked features) that is predicted
* @return resulting labels
*/
virtual CBinaryLabels* apply_locked_binary(SGVector<index_t> indices);

/** Applies a locked machine on a set of indices. Error if machine is
* not locked. Binary case
*
* @param indices index vector (of locked features) that is predicted
* @return resulting labels
*/
virtual CRegressionLabels* apply_locked_regression(
SGVector<index_t> indices);

/** Applies a locked machine on a set of indices. Error if machine is
* not locked
*
* @param indices index vector (of locked features) that is predicted
* @return raw output of machine
*/
virtual CLabels* apply_locked(SGVector<index_t> indices);
virtual SGVector<float64_t> apply_locked_get_output(
SGVector<index_t> indices);

/** Locks the machine on given labels and data. After this call, only
* train_locked and apply_locked may be called.
Expand Down
38 changes: 38 additions & 0 deletions src/shogun/machine/Machine.cpp
Expand Up @@ -173,6 +173,23 @@ CLabels* CMachine::apply(CFeatures* data)
return NULL;
}

CLabels* CMachine::apply_locked(SGVector<index_t> indices)
{
switch (get_machine_problem_type())
{
case PT_BINARY:
return apply_locked_binary(indices);
case PT_REGRESSION:
return apply_locked_regression(indices);
case PT_MULTICLASS:
return apply_locked_multiclass(indices);
default:
SG_ERROR("Unknown problem type");
break;
}
return NULL;
}

CBinaryLabels* CMachine::apply_binary(CFeatures* data)
{
SG_ERROR("This machine does not support apply_binary()\n");
Expand All @@ -191,4 +208,25 @@ CMulticlassLabels* CMachine::apply_multiclass(CFeatures* data)
return NULL;
}

CBinaryLabels* CMachine::apply_locked_binary(SGVector<index_t> indices)
{
SG_ERROR("apply_locked_binary(SGVector<index_t>) is not yet implemented "
"for %s\n", get_name());
return NULL;
}

CRegressionLabels* CMachine::apply_locked_regression(SGVector<index_t> indices)
{
SG_ERROR("apply_locked_regression(SGVector<index_t>) is not yet implemented "
"for %s\n", get_name());
return NULL;
}

CMulticlassLabels* CMachine::apply_locked_multiclass(SGVector<index_t> indices)
{
SG_ERROR("apply_locked_multiclass(SGVector<index_t>) is not yet implemented "
"for %s\n", get_name());
return NULL;
}


15 changes: 7 additions & 8 deletions src/shogun/machine/Machine.h
Expand Up @@ -217,16 +217,15 @@ class CMachine : public CSGObject
/** Applies a locked machine on a set of indices. Error if machine is
* not locked
*
* NOT IMPLEMENTED
*
* @param indices index vector (of locked features) that is predicted
*/
virtual CLabels* apply_locked(SGVector<index_t> indices)
{
SG_ERROR("apply_locked(SGVector<index_t>) is not yet implemented "
"for %s\n", get_name());
return NULL;
}
virtual CLabels* apply_locked(SGVector<index_t> indices);

virtual CBinaryLabels* apply_locked_binary(SGVector<index_t> indices);
virtual CRegressionLabels* apply_locked_regression(
SGVector<index_t> indices);
virtual CMulticlassLabels* apply_locked_multiclass(
SGVector<index_t> indices);

/** Locks the machine on given labels and data. After this call, only
* train_locked and apply_locked may be called
Expand Down

0 comments on commit 2a49637

Please sign in to comment.