Skip to content

Commit

Permalink
Split up CLabels into various classes:
Browse files Browse the repository at this point in the history
CLabels -> CDenseLabels -> CRealLabels
						-> CBinaryLabels
						-> CMulticlassLabels
						-> CRealLabels

- put labels under shogun/labels/
- change include paths in all files
- CMachine still returns a generic CLabels object in its apply()
	function - but submachines create customized CRealLabels,
	CMulticlassLabels etc objects.
- all machines now check if their train function is called with matching
label type
- type of label object can be determined via label->get_label_type()
- introduce confidences in labels
- introduce set_to_const function in labels
  • Loading branch information
Soeren Sonnenburg committed May 20, 2012
1 parent 33fc1e4 commit da70cf4
Show file tree
Hide file tree
Showing 131 changed files with 1,234 additions and 548 deletions.
2 changes: 1 addition & 1 deletion src/README.developer
Expand Up @@ -51,7 +51,7 @@ To finally see some action one has to include the appropriate header files,
e.g. we create some features and a gaussian kernel


#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/classifier/svm/LibSVM.h>
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/modular/Evaluation_includes.i
@@ -1,5 +1,5 @@
%{
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/evaluation/Evaluation.h>
#include <shogun/evaluation/BinaryClassEvaluation.h>
#include <shogun/evaluation/ClusteringEvaluation.h>
Expand Down
10 changes: 9 additions & 1 deletion src/interfaces/modular/Features.i
Expand Up @@ -33,6 +33,10 @@
%rename(CombinedFeatures) CCombinedFeatures;
%rename(CombinedDotFeatures) CCombinedDotFeatures;
%rename(Labels) CLabels;
%rename(DenseLabels) CDenseLabels;
%rename(BinaryLabels) CBinaryLabels;
%rename(MulticlassLabels) CMulticlassLabels;
%rename(RegressionLabels) CRegressionLabels;
%rename(RealFileFeatures) CRealFileFeatures;
%rename(FKFeatures) CFKFeatures;
%rename(TOPFeatures) CTOPFeatures;
Expand Down Expand Up @@ -356,7 +360,11 @@ namespace shogun
%include <shogun/features/CombinedFeatures.h>
%include <shogun/features/CombinedDotFeatures.h>

%include <shogun/features/Labels.h>
%include <shogun/labels/Labels.h>
%include <shogun/labels/DenseLabels.h>
%include <shogun/labels/BinaryLabels.h>
%include <shogun/labels/MulticlassLabels.h>
%include <shogun/labels/RealLabels.h>
%include <shogun/features/RealFileFeatures.h>
%include <shogun/features/FKFeatures.h>
%include <shogun/features/TOPFeatures.h>
Expand Down
7 changes: 6 additions & 1 deletion src/interfaces/modular/Features_includes.i
Expand Up @@ -18,7 +18,12 @@
#include <shogun/features/Alphabet.h>
#include <shogun/features/CombinedFeatures.h>
#include <shogun/features/CombinedDotFeatures.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/labels/DenseLabels.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/labels/RealLabels.h>
#include <shogun/features/RealFileFeatures.h>
#include <shogun/features/RealFileFeatures.h>
#include <shogun/features/FKFeatures.h>
#include <shogun/features/TOPFeatures.h>
Expand Down
7 changes: 5 additions & 2 deletions src/shogun/classifier/AveragedPerceptron.cpp
Expand Up @@ -8,8 +8,9 @@
*/

#include <shogun/classifier/AveragedPerceptron.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/labels/BinaryLabels.h>

using namespace shogun;

Expand All @@ -32,6 +33,8 @@ CAveragedPerceptron::~CAveragedPerceptron()
bool CAveragedPerceptron::train(CFeatures* data)
{
ASSERT(m_labels);
ASSERT(m_labels->get_label_type() == LT_BINARY);

if (data)
{
if (!data->has_property(FP_DOT))
Expand All @@ -41,7 +44,7 @@ bool CAveragedPerceptron::train(CFeatures* data)
ASSERT(features);
bool converged=false;
int32_t iter=0;
SGVector<int32_t> train_labels=m_labels->get_int_labels();
SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
int32_t num_feat=features->get_dim_feature_space();
int32_t num_vec=features->get_num_vectors();

Expand Down
9 changes: 6 additions & 3 deletions src/shogun/classifier/GaussianNaiveBayes.cpp
Expand Up @@ -11,7 +11,9 @@
#include <shogun/classifier/GaussianNaiveBayes.h>
#include <shogun/machine/Machine.h>
#include <shogun/features/Features.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/labels/RealLabels.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/lib/Signal.h>

Expand Down Expand Up @@ -71,7 +73,8 @@ bool CGaussianNaiveBayes::train(CFeatures* data)

// get int labels to train_labels and check length equality
ASSERT(m_labels);
SGVector<int32_t> train_labels = m_labels->get_int_labels();
ASSERT(m_labels->get_label_type() == LT_MULTICLASS);
SGVector<int32_t> train_labels = ((CMulticlassLabels*) m_labels)->get_int_labels();
ASSERT(m_features->get_num_vectors()==train_labels.vlen);

// init min_label, max_label and loop variables
Expand Down Expand Up @@ -176,7 +179,7 @@ CLabels* CGaussianNaiveBayes::apply()
int32_t num_vectors = m_features->get_num_vectors();

// init result labels
CLabels* result = new CLabels(num_vectors);
CRealLabels* result = new CRealLabels(num_vectors);

// classify each example of data
SG_PROGRESS(0, 0, num_vectors);
Expand Down
5 changes: 3 additions & 2 deletions src/shogun/classifier/LDA.cpp
Expand Up @@ -14,7 +14,8 @@
#include <shogun/machine/Machine.h>
#include <shogun/machine/LinearMachine.h>
#include <shogun/classifier/LDA.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/lapack.h>

Expand Down Expand Up @@ -47,7 +48,7 @@ bool CLDA::train_machine(CFeatures* data)
set_features((CDotFeatures*) data);
}
ASSERT(features);
SGVector<int32_t> train_labels=m_labels->get_int_labels();
SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
ASSERT(train_labels.vector);

int32_t num_feat=features->get_dim_feature_space();
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/LPBoost.cpp
Expand Up @@ -13,7 +13,7 @@
#ifdef USE_CPLEX

#include <shogun/classifier/LPBoost.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Cplex.h>
#include <shogun/lib/DynamicArray.h>
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/LPM.cpp
Expand Up @@ -13,7 +13,7 @@
#ifdef USE_CPLEX

#include <shogun/classifier/LPM.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Cplex.h>

Expand Down
8 changes: 5 additions & 3 deletions src/shogun/classifier/NearestCentroid.cpp
Expand Up @@ -8,6 +8,7 @@
*/

#include <shogun/classifier/NearestCentroid.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/features/Features.h>
#include <shogun/features/FeatureTypes.h>

Expand Down Expand Up @@ -48,6 +49,7 @@ namespace shogun{
bool CNearestCentroid::train_machine(CFeatures* data)
{
ASSERT(m_labels);
ASSERT(m_labels->get_label_type() == LT_MULTICLASS);
ASSERT(distance);
ASSERT( data->get_feature_class() == C_DENSE)
if (data)
Expand All @@ -61,8 +63,8 @@ namespace shogun{
data = distance->get_lhs();
}
int32_t num_vectors = data->get_num_vectors();
int32_t num_classes = m_labels->get_num_classes();
int32_t num_feats = ((CDenseFeatures<float64_t>*)data)->get_num_features();
int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
int32_t num_feats = ((CDenseFeatures<float64_t>*) data)->get_num_features();
SGMatrix<float64_t> centroids(num_feats,num_classes);
centroids.zero();

Expand All @@ -79,7 +81,7 @@ namespace shogun{
{
int32_t current_len;
bool current_free;
int32_t current_class = m_labels->get_label(idx);
int32_t current_class = ((CMulticlassLabels*) m_labels)->get_label(idx);
float64_t* target = centroids.matrix + num_feats*current_class;
float64_t* current = ((CDenseFeatures<float64_t>*)data)->get_feature_vector(idx,current_len,current_free);
CMath::add(target,1.0,target,1.0,current,current_len);
Expand Down
8 changes: 6 additions & 2 deletions src/shogun/classifier/Perceptron.cpp
Expand Up @@ -9,7 +9,8 @@
*/

#include <shogun/classifier/Perceptron.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;
Expand All @@ -33,16 +34,19 @@ CPerceptron::~CPerceptron()
bool CPerceptron::train_machine(CFeatures* data)
{
ASSERT(m_labels);
ASSERT(m_labels->get_label_type() == LT_BINARY);

if (data)
{
if (!data->has_property(FP_DOT))
SG_ERROR("Specified features are not of type CDotFeatures\n");
set_features((CDotFeatures*) data);
}

ASSERT(features);
bool converged=false;
int32_t iter=0;
SGVector<int32_t> train_labels=m_labels->get_int_labels();
SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
int32_t num_feat=features->get_dim_feature_space();
int32_t num_vec=features->get_num_vectors();

Expand Down
9 changes: 6 additions & 3 deletions src/shogun/classifier/PluginEstimate.cpp
Expand Up @@ -11,7 +11,9 @@
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <shogun/features/StringFeatures.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/labels/RealLabels.h>
#include <shogun/distributions/LinearHMM.h>
#include <shogun/classifier/PluginEstimate.h>

Expand Down Expand Up @@ -46,6 +48,7 @@ CPluginEstimate::~CPluginEstimate()
bool CPluginEstimate::train_machine(CFeatures* data)
{
ASSERT(m_labels);
ASSERT(m_labels->get_label_type() == LT_BINARY);
if (data)
{
if (data->get_feature_class() != C_STRING ||
Expand Down Expand Up @@ -77,7 +80,7 @@ bool CPluginEstimate::train_machine(CFeatures* data)

for (int32_t i=0; i<m_labels->get_num_labels(); i++)
{
if (m_labels->get_label(i) > 0)
if (((CBinaryLabels*) m_labels)->get_label(i) > 0)
pos_indizes[pos_idx++]=i;
else
neg_indizes[neg_idx++]=i;
Expand All @@ -96,7 +99,7 @@ bool CPluginEstimate::train_machine(CFeatures* data)
CLabels* CPluginEstimate::apply()
{
ASSERT(features);
CLabels* result=new CLabels(features->get_num_vectors());
CRealLabels* result=new CRealLabels(features->get_num_vectors());
ASSERT(result->get_num_labels()==features->get_num_vectors());

for (int32_t vec=0; vec<features->get_num_vectors(); vec++)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/PluginEstimate.h
Expand Up @@ -13,7 +13,7 @@

#include <shogun/machine/Machine.h>
#include <shogun/features/StringFeatures.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>
#include <shogun/distributions/LinearHMM.h>

namespace shogun
Expand Down
9 changes: 6 additions & 3 deletions src/shogun/classifier/QDA.cpp
Expand Up @@ -14,6 +14,9 @@

#include <shogun/classifier/QDA.h>
#include <shogun/features/Features.h>
#include <shogun/labels/Labels.h>
#include <shogun/labels/RealLabels.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/machine/Machine.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/lapack.h>
Expand Down Expand Up @@ -120,7 +123,7 @@ CLabels* CQDA::apply()
norm2[i + k*num_vecs] *= -0.5;
}

CLabels* out = new CLabels(num_vecs);
CRealLabels* out = new CRealLabels(num_vecs);

for ( i = 0 ; i < num_vecs ; ++i )
out->set_label(i, CMath::arg_max(norm2.vector+i, num_vecs, m_num_classes));
Expand Down Expand Up @@ -157,13 +160,13 @@ bool CQDA::train_machine(CFeatures* data)
}
if ( !m_features )
SG_ERROR("No features allocated in QDA training\n");
SGVector< int32_t > train_labels = m_labels->get_int_labels();
SGVector< int32_t > train_labels = ((CMulticlassLabels*) m_labels)->get_int_labels();
if ( !train_labels.vector )
SG_ERROR("No train_labels allocated in QDA training\n");

cleanup();

m_num_classes = m_labels->get_num_classes();
m_num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
m_dim = m_features->get_dim_feature_space();
int32_t num_vec = m_features->get_num_vectors();
if ( num_vec != train_labels.vlen )
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/SubGradientLPM.cpp
Expand Up @@ -20,7 +20,7 @@
#include <shogun/classifier/SubGradientLPM.h>
#include <shogun/classifier/svm/QPBSVMLib.h>
#include <shogun/features/DotFeatures.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>

using namespace shogun;

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/SubGradientLPM.h
Expand Up @@ -21,7 +21,7 @@

#include <shogun/machine/LinearMachine.h>
#include <shogun/features/Features.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>

namespace shogun
{
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/mkl/MKLClassification.cpp
Expand Up @@ -34,5 +34,5 @@ float64_t CMKLClassification::compute_sum_alpha()

void CMKLClassification::init_training()
{
ASSERT(m_labels && m_labels->get_num_labels() && m_labels->is_two_class_labeling());
ASSERT(m_labels && m_labels->get_num_labels() && m_labels->get_label_type() == LT_BINARY);
}
12 changes: 7 additions & 5 deletions src/shogun/classifier/mkl/MKLMulticlass.cpp
Expand Up @@ -11,6 +11,7 @@
#include <shogun/multiclass/MulticlassOneVsRestStrategy.h>
#include <shogun/classifier/mkl/MKLMulticlass.h>
#include <shogun/io/SGIO.h>
#include <shogun/labels/MulticlassLabels.h>

using namespace shogun;

Expand Down Expand Up @@ -239,13 +240,13 @@ void CMKLMulticlass::addingweightsstep( const std::vector<float64_t> &
float64_t CMKLMulticlass::getsumofsignfreealphas()
{
std::vector<int> trainlabels2(m_labels->get_num_labels());
SGVector<int32_t> lab=m_labels->get_int_labels();
SGVector<int32_t> lab=((CMulticlassLabels*) m_labels)->get_int_labels();
std::copy(lab.vector,lab.vector+lab.vlen, trainlabels2.begin());

ASSERT (trainlabels2.size()>0);
float64_t sum=0;

for (int32_t nc=0; nc< m_labels->get_num_classes();++nc)
for (int32_t nc=0; nc< ((CMulticlassLabels*) m_labels)->get_num_classes();++nc)
{
CSVM * sm=svm->get_svm(nc);

Expand All @@ -261,7 +262,7 @@ float64_t CMKLMulticlass::getsumofsignfreealphas()

for (size_t lb=0; lb< trainlabels2.size();++lb)
{
for (int32_t nc=0; nc< m_labels->get_num_classes();++nc)
for (int32_t nc=0; nc< ((CMulticlassLabels*) m_labels)->get_num_classes();++nc)
{
CSVM * sm=svm->get_svm(nc);

Expand Down Expand Up @@ -289,7 +290,7 @@ float64_t CMKLMulticlass::getsquarenormofprimalcoefficients(

float64_t tmp=0;

for (int32_t classindex=0; classindex< m_labels->get_num_classes();
for (int32_t classindex=0; classindex< ((CMulticlassLabels*) m_labels)->get_num_classes();
++classindex)
{
CSVM * sm=svm->get_svm(classindex);
Expand Down Expand Up @@ -320,9 +321,10 @@ float64_t CMKLMulticlass::getsquarenormofprimalcoefficients(

bool CMKLMulticlass::train_machine(CFeatures* data)
{
int numcl=m_labels->get_num_classes();
ASSERT(m_kernel);
ASSERT(m_labels && m_labels->get_num_labels());
ASSERT(m_labels->get_label_type() == LT_MULTICLASS);
int numcl=((CMulticlassLabels*) m_labels)->get_num_classes();

if (data)
{
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/svm/CPLEXSVM.cpp
Expand Up @@ -15,7 +15,7 @@
#include <shogun/io/SGIO.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Cplex.h>
#include <shogun/features/Labels.h>
#include <shogun/labels/Labels.h>

using namespace shogun;

Expand Down

0 comments on commit da70cf4

Please sign in to comment.