Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/vigsterkr/shogun
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Apr 10, 2012
2 parents a690dd7 + 67aca90 commit 73db76e
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/modular/Preprocessor.i
Expand Up @@ -17,6 +17,7 @@
%rename(PruneVarSubMean) CPruneVarSubMean;
%rename(RandomFourierGaussPreproc) CRandomFourierGaussPreproc;
%rename(HomogeneousKernelMap) CHomogeneousKernelMap;
%rename(PNorm) CPNorm;

%rename(DimensionReductionPreprocessor) CDimensionReductionPreprocessor;
%rename(PCA) CPCA;
Expand Down Expand Up @@ -100,6 +101,7 @@ namespace shogun
%include <shogun/preprocessor/PruneVarSubMean.h>
%include <shogun/preprocessor/RandomFourierGaussPreproc.h>
%include <shogun/preprocessor/HomogeneousKernelMap.h>
%include <shogun/preprocessor/PNorm.h>

%include <shogun/preprocessor/PCA.h>
%include <shogun/preprocessor/KernelPCA.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Preprocessor_includes.i
Expand Up @@ -11,6 +11,7 @@
#include <shogun/preprocessor/PruneVarSubMean.h>
#include <shogun/preprocessor/RandomFourierGaussPreproc.h>
#include <shogun/preprocessor/HomogeneousKernelMap.h>
#include <shogun/preprocessor/PNorm.h>

#include <shogun/preprocessor/DimensionReductionPreprocessor.h>
#include <shogun/preprocessor/PCA.h>
Expand Down
11 changes: 11 additions & 0 deletions src/shogun/mathematics/Math.cpp
Expand Up @@ -593,3 +593,14 @@ float32_t CMath::dot(const float32_t* v1, const float32_t* v2, int32_t n)
#endif
return r;
}

float64_t CMath::twonorm (const float64_t* v, int32_t n)
{
float64_t norm = 0.0;
#ifdef HAVE_LAPACK
norm = cblas_dnrm2 (n, v, 1);
#else
norm = CMath::sqrt (CMath::dot (vec, vec, vec_len));
#endif
return norm;
}
4 changes: 3 additions & 1 deletion src/shogun/mathematics/Math.h
Expand Up @@ -253,13 +253,15 @@ class CMath : public CSGObject
return CMath::sqrt(result);
}

static float64_t twonorm(const float64_t* v, int32_t n);

/// || x ||_q^q
template <class T>
static inline T qsq(T* x, int32_t len, float64_t q)
{
float64_t result=0;
for (int32_t i=0; i<len; i++)
result+=CMath::pow(x[i], q);
result+=CMath::pow(fabs(x[i]), q);

return result;
}
Expand Down
136 changes: 136 additions & 0 deletions src/shogun/preprocessor/PNorm.cpp
@@ -0,0 +1,136 @@
/*
* 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 Viktor Gal
* Copyright (C) 2012 Viktor Gal
*/

#include <shogun/preprocessor/PNorm.h>
#include <shogun/preprocessor/SimplePreprocessor.h>
#include <shogun/mathematics/Math.h>
#include <shogun/features/Features.h>
#include <shogun/features/SimpleFeatures.h>

#ifdef HAVE_LAPACK
#include <shogun/mathematics/lapack.h>
#endif

using namespace shogun;

CPNorm::CPNorm ()
: CSimplePreprocessor<float64_t>(),
m_p (2.0)
{
register_param ();
}

CPNorm::CPNorm (double p)
: CSimplePreprocessor<float64_t>(),
m_p (p)
{
ASSERT (m_p >= 1.0);
register_param ();
}

CPNorm::~CPNorm ()
{
}

/// initialize preprocessor from features
bool CPNorm::init (CFeatures* features)
{
ASSERT(features->get_feature_class()==C_SIMPLE);
ASSERT(features->get_feature_type()==F_DREAL);

return true;
}

/// clean up allocated memory
void CPNorm::cleanup ()
{
}

/// initialize preprocessor from file
bool CPNorm::load (FILE* f)
{
SG_SET_LOCALE_C;
SG_RESET_LOCALE;
return false;
}

/// save preprocessor init-data to file
bool CPNorm::save (FILE* f)
{
SG_SET_LOCALE_C;
SG_RESET_LOCALE;
return false;
}

/// apply preproc on feature matrix
/// result in feature matrix
/// return pointer to feature_matrix, i.e. f->get_feature_matrix();
SGMatrix<float64_t> CPNorm::apply_to_feature_matrix (CFeatures* features)
{
SGMatrix<float64_t> feature_matrix=((CSimpleFeatures<float64_t>*)features)->get_feature_matrix();

for (int32_t i=0; i<feature_matrix.num_cols; i++)
{
float64_t* vec= &(feature_matrix.matrix[i*feature_matrix.num_rows]);
float64_t norm = get_pnorm (vec, feature_matrix.num_rows);
CMath::scale_vector(1.0/norm, vec, feature_matrix.num_rows);
}
return feature_matrix;
}

/// apply preproc on single feature vector
/// result in feature matrix
SGVector<float64_t> CPNorm::apply_to_feature_vector (SGVector<float64_t> vector)
{
float64_t* normed_vec = SG_MALLOC(float64_t, vector.vlen);
float64_t norm = get_pnorm (vector.vector, vector.vlen);

for (int32_t i=0; i<vector.vlen; i++)
normed_vec[i]=vector.vector[i]/norm;

return SGVector<float64_t>(normed_vec,vector.vlen);
}

void CPNorm::set_pnorm (double pnorm)
{
ASSERT (pnorm >= 1.0);
m_p = pnorm;
register_param ();
}

double CPNorm::get_pnorm () const
{
return m_p;
}

void CPNorm::register_param ()
{
m_parameters->add (&m_p, "norm", "P-norm parameter");
}

inline float64_t CPNorm::get_pnorm (float64_t* vec, int32_t vec_len) const
{
float64_t norm;
if (m_p == 1.0)
{
for (int i = 0; i < vec_len; ++i)
norm += fabs (vec[i]);
}
else if (m_p == 2.0)
{
norm = CMath::twonorm (vec, vec_len);
}
else
{
norm = CMath::qnorm (vec, vec_len, m_p);
}

return norm;
}
86 changes: 86 additions & 0 deletions src/shogun/preprocessor/PNorm.h
@@ -0,0 +1,86 @@
/*
* 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 Viktor Gal
* Copyright (C) 2012 Viktor Gal
*/

#ifndef _PNORM_ONE__H__
#define _PNORM_ONE__H__

#include <shogun/preprocessor/SimplePreprocessor.h>
#include <shogun/features/Features.h>
#include <shogun/lib/common.h>

#include <stdio.h>

namespace shogun
{
/** @brief Preprocessor PNorm, normalizes vectors to have p-norm.
*
* Formally, it computes
*
* \f[
* {\bf x} \leftarrow \frac{{\bf x}}{||{\bf x}||_p}
* \f]
*
*/
class CPNorm : public CSimplePreprocessor<float64_t>
{
public:
/** default PNorm Constructor */
CPNorm ();

CPNorm (double p);

/** destructor */
virtual ~CPNorm ();

/// initialize preprocessor from features
virtual bool init (CFeatures* features);
/// cleanup
virtual void cleanup ();
/// initialize preprocessor from file
virtual bool load (FILE* f);
/// save preprocessor init-data to file
virtual bool save (FILE* f);

/// apply preproc on feature matrix
/// result in feature matrix
/// return pointer to feature_matrix, i.e. f->get_feature_matrix();
virtual SGMatrix<float64_t> apply_to_feature_matrix (CFeatures* features);

/// apply preproc on single feature vector
/// result in feature matrix
virtual SGVector<float64_t> apply_to_feature_vector (SGVector<float64_t> vector);

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

/// return a type of preprocessor
virtual inline EPreprocessorType get_type () const { return P_PNORM; }

/**
* Set norm
* @param p norm value
*/
void set_pnorm (double pnorm);

/**
* Get norm
* @return norm
*/
double get_pnorm () const;

private:
void register_param ();
inline float64_t get_pnorm (float64_t* vec, int32_t vec_len) const;

private:
double m_p;
};
}
#endif /* _PNORM_ONE__H__ */
3 changes: 2 additions & 1 deletion src/shogun/preprocessor/Preprocessor.h
Expand Up @@ -47,7 +47,8 @@ enum EPreprocessorType
P_NORMDERIVATIVELEM3=150,
P_DIMENSIONREDUCTIONPREPROCESSOR=160,
P_SUMONE=170,
P_HOMOGENEOUSKERNELMAP = 180
P_HOMOGENEOUSKERNELMAP = 180,
P_PNORM = 190
};

/** @brief Class Preprocessor defines a preprocessor interface.
Expand Down

0 comments on commit 73db76e

Please sign in to comment.