Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Various changes for latent variable learning
  * changed CLatentData to CData in order to try to remove code code
    duplication (see CStructuredData).
  * added missing doxygen documentation to all latent code
  * added latent variable learning to modular interface
  • Loading branch information
vigsterkr committed Aug 13, 2012
1 parent c87bf5e commit bbb7d9b
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -70,6 +70,7 @@ configure.log
/src/interfaces/*_modular/Transfer.i
/src/interfaces/*_modular/Loss.i
/src/interfaces/*_modular/Statistics.i
/src/interfaces/*_modular/Latent.i

# particular modular ones
/src/interfaces/csharp_modular/*.cs
Expand Down
10 changes: 5 additions & 5 deletions examples/undocumented/libshogun/classifier_latent_svm.cpp
Expand Up @@ -12,19 +12,19 @@ using namespace shogun;
#define MAX_LINE_LENGTH 4096
#define HOG_SIZE 1488

struct CBoundingBox : public CLatentData
struct CBoundingBox : public CData
{
CBoundingBox(int32_t x, int32_t y) : CLatentData(), x_pos(x), y_pos(y) {};
CBoundingBox(int32_t x, int32_t y) : CData(), x_pos(x), y_pos(y) {};

int32_t x_pos, y_pos;

/** @return name of SGSerializable */
virtual const char* get_name() const { return "BoundingBox"; }
};

struct CHOGFeatures : public CLatentData
struct CHOGFeatures : public CData
{
CHOGFeatures(int32_t w, int32_t h) : CLatentData(), width(w), height(h) {};
CHOGFeatures(int32_t w, int32_t h) : CData(), width(w), height(h) {};

int32_t width, height;
float64_t ***hog;
Expand Down Expand Up @@ -55,7 +55,7 @@ class CObjectDetector: public CLatentModel
return psi_v;
}

virtual CLatentData* infer_latent_variable(const SGVector<float64_t>& w, index_t idx)
virtual CData* infer_latent_variable(const SGVector<float64_t>& w, index_t idx)
{
int32_t pos_x = 0, pos_y = 0;
float64_t max_score;
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/modular/Features.i
Expand Up @@ -64,6 +64,7 @@ PYPROTO_DENSELABELS(CRegressionLabels, RegressionLabels, float64_t, "d\0", NPY_F
%rename(RegressionLabels) CRegressionLabels;

%rename(StructuredLabels) CStructuredLabels;
%rename(LatentLabels) CLatentLabels;
%rename(MulticlassMultipleOutputLabels) CMulticlassMultipleOutputLabels;
%rename(RealFileFeatures) CRealFileFeatures;
%rename(FKFeatures) CFKFeatures;
Expand All @@ -78,6 +79,7 @@ PYPROTO_DENSELABELS(CRegressionLabels, RegressionLabels, float64_t, "d\0", NPY_F
%rename(ExplicitSpecFeatures) CExplicitSpecFeatures;
%rename(ImplicitWeightedSpecFeatures) CImplicitWeightedSpecFeatures;
%rename(DataGenerator) CDataGenerator;
%rename(LatentFeatures) CLatentFeatures;

/* Include Class Headers to make them visible from within the target language */
%include <shogun/features/FeatureTypes.h>
Expand Down Expand Up @@ -458,6 +460,7 @@ namespace shogun
%include <shogun/labels/Labels.h>
%include <shogun/labels/DenseLabels.h>
%include <shogun/labels/BinaryLabels.h>
%include <shogun/labels/LatentLabels.h>
%include <shogun/labels/MulticlassLabels.h>
%include <shogun/labels/RegressionLabels.h>
%include <shogun/labels/StructuredLabels.h>
Expand All @@ -474,3 +477,4 @@ namespace shogun
%include <shogun/features/LBPPyrDotFeatures.h>
%include <shogun/features/ExplicitSpecFeatures.h>
%include <shogun/features/ImplicitWeightedSpecFeatures.h>
%include <shogun/features/LatentFeatures.h>
2 changes: 2 additions & 0 deletions src/interfaces/modular/Features_includes.i
Expand Up @@ -23,6 +23,7 @@
#include <shogun/labels/Labels.h>
#include <shogun/labels/DenseLabels.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/labels/LatentLabels.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/labels/RegressionLabels.h>
#include <shogun/labels/StructuredLabels.h>
Expand All @@ -41,4 +42,5 @@
#include <shogun/features/ExplicitSpecFeatures.h>
#include <shogun/features/ImplicitWeightedSpecFeatures.h>
#include <shogun/features/DataGenerator.h>
#include <shogun/features/LatentFeatures.h>
%}
23 changes: 23 additions & 0 deletions src/interfaces/modular/Latent.i
@@ -0,0 +1,23 @@
/*
* 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) 2009 Soeren Sonnenburg, 2012 Fernando José Iglesias García
* Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/

/* Remove C Prefix */
%rename(LatentModel) CLatentModel;

%rename(LinearLatentMachine) CLinearLatentMachine;

%rename(LatentSVM) CLatentSVM;

/* Include Class Headers to make them visible from within the target language */
%include <shogun/latent/LatentModel.h>

%include <shogun/machine/LinearLatentMachine.h>

%include <shogun/latent/LatentSVM.h>
8 changes: 8 additions & 0 deletions src/interfaces/modular/Latent_includes.i
@@ -0,0 +1,8 @@
%{
#include <shogun/latent/LatentModel.h>

#include <shogun/machine/LinearLatentMachine.h>

#include <shogun/latent/LatentSVM.h>
%}

2 changes: 2 additions & 0 deletions src/interfaces/modular/Library.i
Expand Up @@ -26,6 +26,7 @@
%rename(IndexBlockRelation) CIndexBlockRelation;
%rename(IndexBlockGroup) CIndexBlockGroup;
%rename(IndexBlockTree) CIndexBlockTree;
%rename(Data) CData;

%ignore RADIX_STACK_SIZE;
%ignore NUMTRAPPEDSIGS;
Expand Down Expand Up @@ -468,3 +469,4 @@ namespace shogun
%include <shogun/lib/IndexBlockRelation.h>
%include <shogun/lib/IndexBlockGroup.h>
%include <shogun/lib/IndexBlockTree.h>
%include <shogun/lib/Data.h>
1 change: 1 addition & 0 deletions src/interfaces/modular/Library_includes.i
Expand Up @@ -25,4 +25,5 @@
#include <shogun/lib/IndexBlockRelation.h>
#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/lib/IndexBlockTree.h>
#include <shogun/lib/Data.h>
%}
2 changes: 2 additions & 0 deletions src/interfaces/modular/modshogun.i
Expand Up @@ -63,6 +63,7 @@
%include "Transfer_includes.i"
%include "Loss_includes.i"
%include "Statistics_includes.i"
%include "Latent_includes.i"

%include "Machine.i"
%include "SGBase.i"
Expand All @@ -85,3 +86,4 @@
%include "Transfer.i"
%include "Loss.i"
%include "Statistics.i"
%include "Latent.i"
6 changes: 3 additions & 3 deletions src/shogun/features/LatentFeatures.cpp
Expand Up @@ -58,7 +58,7 @@ int32_t CLatentFeatures::get_size() const
return sizeof(float64_t);
}

bool CLatentFeatures::add_sample(CLatentData* example)
bool CLatentFeatures::add_sample(CData* example)
{
ASSERT(m_samples != NULL);
if (m_samples != NULL)
Expand All @@ -70,13 +70,13 @@ bool CLatentFeatures::add_sample(CLatentData* example)
return false;
}

CLatentData* CLatentFeatures::get_sample(index_t idx)
CData* CLatentFeatures::get_sample(index_t idx)
{
ASSERT(m_samples != NULL);
if (idx < 0 || idx >= this->get_num_vectors())
SG_ERROR("Out of index!\n");

return (CLatentData*) m_samples->get_element(idx);
return (CData*) m_samples->get_element(idx);

}

Expand Down
29 changes: 24 additions & 5 deletions src/shogun/features/LatentFeatures.h
Expand Up @@ -12,20 +12,33 @@
#define __LATENTFEATURES_H__

#include <shogun/features/Features.h>
#include <shogun/labels/LatentLabels.h>
#include <shogun/lib/Data.h>

namespace shogun
{
/** @brief Latent Features class
* The class if for representing features for latent learning, e.g. LatentSVM.
* It's basically a very generic way of storing features of any (user-defined) form
* based on CData.
*/
class CLatentFeatures : public CFeatures
{
public:
/** default constructor */
CLatentFeatures();

/** constructor
*
* @param num_samples the number of examples the object will contain
*/
CLatentFeatures(int32_t num_samples);

virtual ~CLatentFeatures();

/** Copy-constructor
*
* @return the copy of the given object
*/
virtual CFeatures* duplicate() const;

/** get feature type
Expand Down Expand Up @@ -54,30 +67,36 @@ namespace shogun
*/
virtual int32_t get_size() const;

/** Returns the name of the SGSerializable instance.
*
* @return name of the SGSerializable
*/
virtual const char* get_name() const { return "LatentFeatures"; }

/** add latent example
*
* @param example the user defined CLatentData
* @param example the user defined CData
*/
bool add_sample(CLatentData* example);
bool add_sample(CData* example);

/** get latent example
*
* @param idx index of the required example
* @return the user defined LatentData at the given index
* @return the user defined CData at the given index
*/
CLatentData* get_sample(index_t idx);
CData* get_sample(index_t idx);

/** helper method used to specialize a base class instance
*
* @param base_feats its dynamic type must be CLatentFeatures
*/
static CLatentFeatures* obtain_from_generic(CFeatures* base_feats);
protected:
/** array of CData */
CDynamicObjectArray* m_samples;

private:
/** init function for the object */
void init();
};
}
Expand Down
18 changes: 4 additions & 14 deletions src/shogun/labels/LatentLabels.cpp
Expand Up @@ -12,16 +12,6 @@

using namespace shogun;

CLatentData::CLatentData()
{

}

CLatentData::~CLatentData()
{

}

CLatentLabels::CLatentLabels()
: CBinaryLabels()
{
Expand Down Expand Up @@ -53,22 +43,22 @@ CDynamicObjectArray* CLatentLabels::get_labels() const
return m_latent_labels;
}

CLatentData* CLatentLabels::get_latent_label(int32_t idx)
CData* CLatentLabels::get_latent_label(int32_t idx)
{
ASSERT(m_latent_labels != NULL);
if (idx < 0 || idx >= get_num_labels())
SG_ERROR("Out of index!\n");

return (CLatentData*) m_latent_labels->get_element(idx);
return (CData*) m_latent_labels->get_element(idx);
}

void CLatentLabels::add_latent_label(CLatentData* label)
void CLatentLabels::add_latent_label(CData* label)
{
ASSERT(m_latent_labels != NULL);
m_latent_labels->push_back(label);
}

bool CLatentLabels::set_latent_label(int32_t idx, CLatentData* label)
bool CLatentLabels::set_latent_label(int32_t idx, CData* label)
{
if (idx < get_num_labels())
{
Expand Down

0 comments on commit bbb7d9b

Please sign in to comment.