Skip to content

Commit

Permalink
Refactor MulticlassMultipleOutputLabels
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiaolong committed Mar 29, 2014
1 parent 37c1804 commit 9a502ba
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 217 deletions.
Expand Up @@ -6,7 +6,7 @@
parameter_list = [[traindat,testdat,label_traindat,label_testdat,2.1,1,1e-5],[traindat,testdat,label_traindat,label_testdat,2.2,1,1e-5]]

def classifier_multiclassmultipleoutputliblinear_modular (fm_train_real=traindat,fm_test_real=testdat,label_train_multiclass=label_traindat,label_test_multiclass=label_testdat,width=2.1,C=1,epsilon=1e-5):
from modshogun import RealFeatures, MulticlassLabels, MulticlassMultipleOutputLabels
from modshogun import RealFeatures, MulticlassLabels, MultilabelLabels
from modshogun import MulticlassLibLinear

feats_train=RealFeatures(fm_train_real)
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/modular/Features.i
Expand Up @@ -80,7 +80,7 @@ PROTOCOLS_DENSELABELS(CRegressionLabels, RegressionLabels, float64_t, "d\0", NPY

%rename(StructuredLabels) CStructuredLabels;
%rename(LatentLabels) CLatentLabels;
%rename(MulticlassMultipleOutputLabels) CMulticlassMultipleOutputLabels;
%rename(MultilabelLabels) CMultilabelLabels;
%rename(RealFileFeatures) CRealFileFeatures;
%rename(FKFeatures) CFKFeatures;
%rename(TOPFeatures) CTOPFeatures;
Expand Down Expand Up @@ -503,7 +503,7 @@ namespace shogun
%include <shogun/labels/MulticlassLabels.h>
%include <shogun/labels/RegressionLabels.h>
%include <shogun/labels/StructuredLabels.h>
%include <shogun/labels/MulticlassMultipleOutputLabels.h>
%include <shogun/labels/MultilabelLabels.h>

%include <shogun/features/RealFileFeatures.h>
%include <shogun/features/FKFeatures.h>
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/modular/Features_includes.i
Expand Up @@ -32,7 +32,7 @@
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/labels/RegressionLabels.h>
#include <shogun/labels/StructuredLabels.h>
#include <shogun/labels/MulticlassMultipleOutputLabels.h>
#include <shogun/labels/MultilabelLabels.h>
#include <shogun/features/RealFileFeatures.h>
#include <shogun/features/RealFileFeatures.h>
#include <shogun/features/FKFeatures.h>
Expand Down
16 changes: 8 additions & 8 deletions src/shogun/labels/LabelsFactory.cpp
Expand Up @@ -5,7 +5,7 @@
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/labels/RegressionLabels.h>
#include <shogun/labels/StructuredLabels.h>
#include <shogun/labels/MulticlassMultipleOutputLabels.h>
//#include <shogun/labels/MultilabelLabels.h>
#include <shogun/structure/MulticlassSOLabels.h>

using namespace shogun;
Expand Down Expand Up @@ -65,24 +65,24 @@ CStructuredLabels* CLabelsFactory::to_structured(CLabels* base_labels)
return NULL;
}


CMulticlassMultipleOutputLabels* CLabelsFactory::to_multiclass_multiple_output(CLabels* base_labels)
/*
CMultilabelLabels* CLabelsFactory::to_multiclass_multiple_output(CLabels* base_labels)
{
ASSERT(base_labels != NULL)
if (base_labels->get_label_type() == LT_MULTICLASS_MULTIPLE_OUTPUT)
return static_cast<CMulticlassMultipleOutputLabels*>(base_labels);
if (base_labels->get_label_type() == LT_SPARSE_MULTILABEL)
return static_cast<CMultilabelLabels*>(base_labels);
else
SG_SERROR("base_labels must be of dynamic type CMulticlassMultipleOutputLabels\n")
SG_SERROR("base_labels must be of dynamic type CMultilabelLabels\n")
return NULL;
}
}*/

CMulticlassSOLabels* CLabelsFactory::to_multiclass_structured(CLabels* base_labels)
{
ASSERT(base_labels != NULL)
CMulticlassSOLabels* labels = dynamic_cast<CMulticlassSOLabels*>(base_labels);
if (labels == NULL)
SG_SERROR("base_labels must be of dynamic type CMulticlassMultipleOutputLabels\n")
SG_SERROR("base_labels must be of dynamic type CMulticlassSOLabels\n")

return labels;
}
8 changes: 4 additions & 4 deletions src/shogun/labels/LabelsFactory.h
Expand Up @@ -23,7 +23,7 @@ namespace shogun
class CMulticlassLabels;
class CRegressionLabels;
class CStructuredLabels;
class CMulticlassMultipleOutputLabels;
//class CMultilabelLabels;
class CMulticlassSOLabels;

/** @brief The helper class to specialize base class instances of labels
Expand Down Expand Up @@ -61,11 +61,11 @@ class CLabelsFactory : public CSGObject
*/
static CStructuredLabels* to_structured(CLabels* base_labels);

/** specialize a base class instance to CMulticlassMultipleOutputLabels
/** specialize a base class instance to CMultilabelLabels
*
* @param base_labels its dynamic type must be CMulticlassMultipleOutputLabels
* @param base_labels its dynamic type must be CMultilabelLabels
*/
static CMulticlassMultipleOutputLabels* to_multiclass_multiple_output(CLabels* base_labels);
// static CMultilabelLabels* to_multiclass_multiple_output(CLabels* base_labels);

/** specialize a base class instance to CMulticlassSOLabels
*
Expand Down
86 changes: 0 additions & 86 deletions src/shogun/labels/MulticlassMultipleOutputLabels.cpp

This file was deleted.

107 changes: 0 additions & 107 deletions src/shogun/labels/MulticlassMultipleOutputLabels.h

This file was deleted.

16 changes: 16 additions & 0 deletions src/shogun/labels/MultilabelLabels.cpp
Expand Up @@ -203,6 +203,22 @@ SGVector <int32_t> ** CMultilabelLabels::get_class_labels() const
return labels_list;
}

SGMatrix<int32_t> CMultilabelLabels::get_labels() const
{
if (m_num_labels==0)
return SGMatrix<int32_t>();
int32_t n_outputs = m_labels[0].vlen;
SGMatrix<int32_t> labels(m_num_labels, n_outputs);
for (int32_t i=0; i<m_num_labels; i++)
{
REQUIRE(m_labels[i].vlen==n_outputs,
"This function is valid only for multiclass multiple output lables.");

for (int32_t j=0; j<n_outputs; j++)
labels(i,j) = m_labels[i][j];
}
return labels;
}

SGVector <int32_t> CMultilabelLabels::get_label(int32_t j)
{
Expand Down
11 changes: 9 additions & 2 deletions src/shogun/labels/MultilabelLabels.h
Expand Up @@ -38,6 +38,7 @@
#include <shogun/labels/LabelTypes.h>
#include <shogun/labels/Labels.h>
#include <shogun/lib/SGVector.h>
#include <shogun/lib/SGMatrix.h>

namespace shogun
{
Expand Down Expand Up @@ -115,8 +116,14 @@ class CMultilabelLabels : public CLabels
* @return SGVector<int32_t> **
*/
SGVector<int32_t> ** get_class_labels() const;

/** get sparse assignment for j-th label

/** get sparse assignment for j-th label
*
* @return SGVector<int32_t > sparse label
*/
SGMatrix<int32_t> get_labels() const;

/** get sparse assignment for j-th label
*
* @return SGVector<int32_t > sparse label
*/
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/machine/MulticlassMachine.cpp
Expand Up @@ -16,7 +16,7 @@
#include <shogun/machine/MulticlassMachine.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/mathematics/Statistics.h>
#include <shogun/labels/MulticlassMultipleOutputLabels.h>
//#include <shogun/labels/MultilabelLabels.h>

using namespace shogun;

Expand Down Expand Up @@ -192,9 +192,9 @@ CMulticlassLabels* CMulticlassMachine::apply_multiclass(CFeatures* data)
return return_labels;
}

CMulticlassMultipleOutputLabels* CMulticlassMachine::apply_multiclass_multiple_output(CFeatures* data, int32_t n_outputs)
CMultilabelLabels* CMulticlassMachine::apply_multiclass_multiple_output(CFeatures* data, int32_t n_outputs)
{
CMulticlassMultipleOutputLabels* return_labels=NULL;
CMultilabelLabels* return_labels=NULL;

if (data)
init_machines_for_apply(data);
Expand All @@ -212,7 +212,7 @@ CMulticlassMultipleOutputLabels* CMulticlassMachine::apply_multiclass_multiple_o
SG_ERROR("num_machines = %d, did you train your machine?", num_machines)
REQUIRE(n_outputs<=num_machines,"You request more outputs than machines available")

CMulticlassMultipleOutputLabels* result=new CMulticlassMultipleOutputLabels(num_vectors);
CMultilabelLabels* result=new CMultilabelLabels(num_vectors, n_outputs);
CBinaryLabels** outputs=SG_MALLOC(CBinaryLabels*, num_machines);

for (int32_t i=0; i < num_machines; ++i)
Expand Down
5 changes: 3 additions & 2 deletions src/shogun/machine/MulticlassMachine.h
Expand Up @@ -18,14 +18,15 @@
#include <shogun/machine/BaseMulticlassMachine.h>
#include <shogun/lib/DynamicObjectArray.h>
#include <shogun/multiclass/MulticlassStrategy.h>
#include <shogun/labels/MultilabelLabels.h>

namespace shogun
{

class CFeatures;
class CLabels;
class CMulticlassLabels;
class CMulticlassMultipleOutputLabels;
//class CMultilabelLabels;

/** @brief experimental abstract generic multiclass machine class */
class CMulticlassMachine : public CBaseMulticlassMachine
Expand Down Expand Up @@ -99,7 +100,7 @@ class CMulticlassMachine : public CBaseMulticlassMachine
*
* @return resulting labels
*/
virtual CMulticlassMultipleOutputLabels* apply_multiclass_multiple_output(CFeatures* data=NULL, int32_t n_outputs=5);
virtual CMultilabelLabels* apply_multiclass_multiple_output(CFeatures* data=NULL, int32_t n_outputs=5);

/** classify one example
* @param vec_idx
Expand Down

0 comments on commit 9a502ba

Please sign in to comment.