Skip to content

Commit

Permalink
Added virtual method indicating whether machine requires labels for t…
Browse files Browse the repository at this point in the history
…raining,

made clustering algorithms do not require labels
  • Loading branch information
lisitsyn committed May 22, 2012
1 parent 1cc1e70 commit 4ead6f0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
13 changes: 0 additions & 13 deletions src/shogun/clustering/Hierarchical.cpp
Expand Up @@ -195,16 +195,3 @@ void CHierarchical::store_model_features()
{
/* TODO. Currently does nothing since apply methods are not implemented. */
}

CLabels* CHierarchical::apply(CFeatures* data)
{
SG_ERROR("apply(...) not implemented for %s!\n", get_name());
return NULL;
}

float64_t CHierarchical::apply(int32_t num)
{
apply();
return 0;
}

8 changes: 2 additions & 6 deletions src/shogun/clustering/Hierarchical.h
Expand Up @@ -117,12 +117,8 @@ class CHierarchical : public CDistanceMachine
* Currently: does nothing.
* */
virtual void store_model_features();

/** NOT IMPLEMENTED */
virtual CLabels* apply(CFeatures* data=NULL);

/** NOT IMPLEMENTED */
virtual float64_t apply(int32_t num);

virtual bool train_require_labels() const { return false; }

protected:
/// the number of merges in hierarchical clustering
Expand Down
2 changes: 2 additions & 0 deletions src/shogun/clustering/KMeans.h
Expand Up @@ -135,6 +135,8 @@ class CKMeans : public CDistanceMachine

/** Ensures cluster centers are in lhs of underlying distance */
virtual void store_model_features();

virtual bool train_require_labels() const { return false; }

private:
void init();
Expand Down
10 changes: 7 additions & 3 deletions src/shogun/machine/Machine.cpp
Expand Up @@ -56,9 +56,13 @@ bool CMachine::train(CFeatures* data)
get_name());
}

if (m_labels == NULL)
SG_ERROR("%s@%p: No labels given", get_name(), this);
m_labels->ensure_valid(get_name());
if (train_require_labels())
{
if (m_labels == NULL)
SG_ERROR("%s@%p: No labels given", get_name(), this);

m_labels->ensure_valid(get_name());
}

bool result = train_machine(data);

Expand Down
26 changes: 14 additions & 12 deletions src/shogun/machine/Machine.h
Expand Up @@ -104,7 +104,7 @@ enum EProblemType

/** @brief A generic learning machine interface.
*
* A machine takes as input CFeatures and (optionally) CLabels.
* A machine takes as input CFeatures and CLabels (by default).
* Later subclasses may specialize the machine to e.g. require labels
* and a kernel or labels and (real-valued) features.
*
Expand Down Expand Up @@ -253,7 +253,7 @@ class CMachine : public CSGObject
SG_NOTIMPLEMENTED;
return PT_BINARY;
}

virtual const char* get_name() const { return "Machine"; }

protected:
Expand Down Expand Up @@ -291,16 +291,18 @@ class CMachine : public CSGObject
" work though.\n", get_name());
}

/** check whether the labels is valid.
*
* Subclasses can override this to implement their check of label types.
*
* @param lab the labels being checked, guaranteed to be non-NULL
*/
virtual bool is_label_valid(CLabels *lab) const
{
return true;
}
/** check whether the labels is valid.
*
* Subclasses can override this to implement their check of label types.
*
* @param lab the labels being checked, guaranteed to be non-NULL
*/
virtual bool is_label_valid(CLabels *lab) const
{
return true;
}

virtual bool train_require_labels() const { return true; }

protected:
/** maximum training time */
Expand Down

0 comments on commit 4ead6f0

Please sign in to comment.