Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/vigsterkr/shogun
- Loading branch information
Showing
7 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters