Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #551 from iglesias/so
SO Initiation
  • Loading branch information
Soeren Sonnenburg committed May 24, 2012
2 parents 95c124d + 0f4e4c7 commit 8b0279c
Show file tree
Hide file tree
Showing 28 changed files with 1,235 additions and 83 deletions.
4 changes: 3 additions & 1 deletion src/shogun/labels/LabelTypes.h
Expand Up @@ -15,6 +15,8 @@ enum ELabelType
/// multi-class labels 0,1,...
LT_MULTICLASS = 1,
/// real valued labels (e.g. for regression, classifier outputs)
LT_REGRESSION = 3
LT_REGRESSION = 3,
/// structured labels (e.g. sequences, trees) used in Structured Output problems
LT_STRUCTURED = 4
};
#endif
59 changes: 59 additions & 0 deletions src/shogun/labels/StructuredLabels.cpp
@@ -0,0 +1,59 @@
/*
* 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) 2012 Fernando José Iglesias García
* Copyright (C) 2012 Fernando José Iglesias García
*/

#include <shogun/labels/StructuredLabels.h>

using namespace shogun;

CStructuredLabels::CStructuredLabels()
: CLabels()
{
init();
}

CStructuredLabels::CStructuredLabels(int32_t num_labels)
: CLabels()
{
init();
m_labels = new CDynamicObjectArray(num_labels);
SG_REF(m_labels);
}

CStructuredLabels::~CStructuredLabels()
{
SG_UNREF(m_labels);
}

void CStructuredLabels::ensure_valid(const char* context)
{
if ( m_labels == NULL )
SG_ERROR("Non-valid StructuredLabels");
}

CDynamicObjectArray* CStructuredLabels::get_labels() const
{
SG_REF(m_labels);
return m_labels;
}

int32_t CStructuredLabels::get_num_labels()
{
if ( m_labels == NULL )
return 0;
else
return m_labels->get_num_elements();
}

void CStructuredLabels::init()
{
SG_ADD((CSGObject**) &m_labels, "m_labels", "The labels", MS_NOT_AVAILABLE);

m_labels = NULL;
}
80 changes: 80 additions & 0 deletions src/shogun/labels/StructuredLabels.h
@@ -0,0 +1,80 @@
/*
* 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) 2012 Fernando José Iglesias García
* Copyright (C) 2012 Fernando José Iglesias García
*/

#ifndef _STRUCTURED_LABELS__H__
#define _STRUCTURED_LABELS__H__

#include <shogun/labels/Labels.h>
#include <shogun/labels/LabelTypes.h>
#include <shogun/lib/DynamicObjectArray.h>
#include <shogun/lib/StructuredData.h>

namespace shogun {

class CStructuredLabels : public CLabels
{

public:
/** default constructor */
CStructuredLabels();

/** constructor
*
* @param num_labels number of labels
*/
CStructuredLabels(int32_t num_labels);

/** destructor */
~CStructuredLabels();

/** check if labeling is valid
*
* possible with subset
*
* @return if labeling is valid
*/
virtual void ensure_valid(const char* context = NULL);

/** get labels
*
* not possible with subset
*
* @return labels
*/
CDynamicObjectArray* get_labels() const;

/** get number of labels, depending on wheter a subset is set
*
* @return number of labels
*/
virtual int32_t get_num_labels();

/** @return object name */
virtual const char* get_name() const { return "StructuredLabels"; }

/** get label type
*
* @return label type LT_STRUCTURED
*/
virtual ELabelType get_label_type() { return LT_STRUCTURED; }

private:
/** internal initialization */
void init();

private:
/** the vector of labels */
CDynamicObjectArray* m_labels;

}; /* class CStructuredLabels */

} /* namespace shogun */

#endif /* _STRUCTUREDLABELS_H__ */
23 changes: 23 additions & 0 deletions src/shogun/lib/StructuredData.cpp
@@ -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) 2012 Fernando José Iglesias García
* Copyright (C) 2012 Fernando José Iglesias García
*/

#include <shogun/lib/StructuredData.h>

using namespace shogun;

/* TODO */
CStructuredData::CStructuredData()
{
}

/* TODO */
CStructuredData::~CStructuredData()
{
}
36 changes: 36 additions & 0 deletions src/shogun/lib/StructuredData.h
@@ -0,0 +1,36 @@
/*
* 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) 2012 Fernando José Iglesias García
* Copyright (C) 2012 Fernando José Iglesias García
*/

#ifndef _STRUCTUREDDATA_H__
#define _STRUCTUREDDATA_H__

#include <shogun/base/SGObject.h>

namespace shogun
{

class CStructuredData : public CSGObject
{

public:
/** default constructor */
CStructuredData();

/** destructor */
~CStructuredData();

/** @return object name */
inline virtual const char* get_name() const { return "StructuredData"; }

};

} /* namespace shogun */

#endif /* _STRUCTUREDDATA_H__ */
16 changes: 16 additions & 0 deletions src/shogun/loss/HingeLoss.cpp
Expand Up @@ -11,6 +11,7 @@
(at your option) any later version.
Modifications (w) 2011 Shashwat Lal Das
Modifications (w) 2012 Fernando José Iglesias García
*/

#include <shogun/loss/HingeLoss.h>
Expand All @@ -24,16 +25,31 @@ float64_t CHingeLoss::loss(float64_t prediction, float64_t label)
return (e > 0) ? e : 0;
}

float64_t CHingeLoss::loss(float64_t z)
{
return (z < 1) ? 1-z : 0;
}

float64_t CHingeLoss::first_derivative(float64_t prediction, float64_t label)
{
return (label * prediction >= label * label) ? 0 : -label;
}

float64_t CHingeLoss::first_derivative(float64_t z)
{
return (z < 1) ? -1 : 0;
}

float64_t CHingeLoss::second_derivative(float64_t prediction, float64_t label)
{
return 0.;
}

float64_t CHingeLoss::second_derivative(float64_t z)
{
return 0;
}

float64_t CHingeLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm)
{
if (label * prediction >= label * label)
Expand Down
28 changes: 28 additions & 0 deletions src/shogun/loss/HingeLoss.h
Expand Up @@ -11,6 +11,7 @@
(at your option) any later version.
Modifications (w) 2011 Shashwat Lal Das
Modifications (w) 2012 Fernando José Iglesias García
*/

#ifndef _HINGELOSS_H__
Expand Down Expand Up @@ -46,6 +47,15 @@ class CHingeLoss: public CLossFunction
*/
float64_t loss(float64_t prediction, float64_t label);

/**
* Get loss for an example
*
* @param z where to evaluate the loss
*
* @return loss
*/
float64_t loss(float64_t z);

/**
* Get first derivative of the loss function
*
Expand All @@ -56,6 +66,15 @@ class CHingeLoss: public CLossFunction
*/
virtual float64_t first_derivative(float64_t prediction, float64_t label);

/**
* Get first derivative of the loss function
*
* @param z where to evaluate the derivative of the loss
*
* @return first derivative
*/
virtual float64_t first_derivative(float64_t z);

/**
* Get second derivative of the loss function
*
Expand All @@ -66,6 +85,15 @@ class CHingeLoss: public CLossFunction
*/
virtual float64_t second_derivative(float64_t prediction, float64_t label);

/**
* Get second derivative of the loss function
*
* @param z where to evaluate the second derivative of the loss
*
* @return second derivative
*/
virtual float64_t second_derivative(float64_t z);

/**
* Get importance aware weight update for this loss function
*
Expand Down
18 changes: 7 additions & 11 deletions src/shogun/loss/LogLoss.cpp
Expand Up @@ -5,35 +5,31 @@
* (at your option) any later version.
*
* Written (W) 2011 Shashwat Lal Das
* Written (W) 2012 Fernando José Iglesias García
* Copyright (c) 2011 Berlin Institute of Technology and Max-Planck-Society.
*/

#include <shogun/loss/LogLoss.h>

using namespace shogun;

float64_t CLogLoss::loss(float64_t prediction, float64_t label)
float64_t CLogLoss::loss(float64_t z)
{
float64_t z = prediction * label;
if (z >= 0)
return log(1+exp(-z));
return -z + log(1+exp(z));
return (z >= 0) ? log(1 + exp(-z)) : -z + log(1 + exp(z));
}

float64_t CLogLoss::first_derivative(float64_t prediction, float64_t label)
float64_t CLogLoss::first_derivative(float64_t z)
{
float64_t z = prediction * label;
if (z < 0)
return -1 / (exp(z) + 1);

float64_t ez = exp(-z);
return -ez / (ez + 1);
}

float64_t CLogLoss::second_derivative(float64_t prediction, float64_t label)
float64_t CLogLoss::second_derivative(float64_t z)
{
float64_t z = prediction * label;
float64_t ez = exp(z);

return ez / (ez*(ez + 2) + 1);
}

Expand Down Expand Up @@ -65,7 +61,7 @@ float64_t CLogLoss::get_update(float64_t prediction, float64_t label, float64_t

float64_t CLogLoss::get_square_grad(float64_t prediction, float64_t label)
{
float64_t d = first_derivative(prediction, label);
float64_t d = CLossFunction::first_derivative(prediction, label);
return d*d;
}

0 comments on commit 8b0279c

Please sign in to comment.