Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and optimize transformers #4348

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
namespace shogun
{

/** @brief Class MachineNotTrainedException defines an exception which is
/** @brief Class NotFittedException defines an exception which is
* thrown whenever a machine or a transformer in Shogun used but haven't be
* fitted.
*/
class MachineNotTrainedException : public ShogunException
class NotFittedException : public ShogunException
{
public:
/** constructor
*
* @param str exception string
*/
explicit MachineNotTrainedException(const std::string& what_arg)
explicit NotFittedException(const std::string& what_arg)
: ShogunException(what_arg)
{
}
Expand All @@ -32,7 +32,7 @@ namespace shogun
*
* @param str exception string
*/
explicit MachineNotTrainedException(const char* what_arg)
explicit NotFittedException(const char* what_arg)
: ShogunException(what_arg)
{
}
Expand Down
16 changes: 8 additions & 8 deletions src/shogun/preprocessor/KernelPCA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ CKernelPCA::CKernelPCA(CKernel* k) : CPreprocessor()

void CKernelPCA::init()
{
m_initialized = false;
m_fitted = false;
m_init_features = NULL;
m_transformation_matrix = SGMatrix<float64_t>();
m_bias_vector = SGVector<float64_t>();
Expand All @@ -59,7 +59,7 @@ void CKernelPCA::cleanup()
if (m_init_features)
SG_UNREF(m_init_features);

m_initialized = false;
m_fitted = false;
}

CKernelPCA::~CKernelPCA()
Expand All @@ -72,7 +72,7 @@ void CKernelPCA::fit(CFeatures* features)
{
REQUIRE(m_kernel, "Kernel not set\n");

if (m_initialized)
if (m_fitted)
cleanup();

SG_REF(features);
Expand Down Expand Up @@ -122,13 +122,13 @@ void CKernelPCA::fit(CFeatures* features)
m_bias_vector = SGVector<float64_t>(m_target_dim);
linalg::matrix_prod(m_transformation_matrix, bias_tmp, m_bias_vector, true);

m_initialized = true;
m_fitted = true;
SG_INFO("Done\n")
}

CFeatures* CKernelPCA::transform(CFeatures* features, bool inplace)
{
REQUIRE(m_initialized, "Transformer not fitted.\n");
check_fitted();

if (!inplace)
features = dynamic_cast<CFeatures*>(features->clone());
Expand All @@ -152,7 +152,7 @@ CFeatures* CKernelPCA::transform(CFeatures* features, bool inplace)

SGMatrix<float64_t> CKernelPCA::apply_to_feature_matrix(CFeatures* features)
{
ASSERT(m_initialized)
check_fitted();
int32_t n = m_init_features->get_num_vectors();

m_kernel->init(features, m_init_features);
Expand All @@ -172,7 +172,7 @@ SGMatrix<float64_t> CKernelPCA::apply_to_feature_matrix(CFeatures* features)

SGVector<float64_t> CKernelPCA::apply_to_feature_vector(SGVector<float64_t> vector)
{
ASSERT(m_initialized)
check_fitted();

CFeatures* features =
new CDenseFeatures<float64_t>(SGMatrix<float64_t>(vector));
Expand All @@ -186,7 +186,7 @@ SGVector<float64_t> CKernelPCA::apply_to_feature_vector(SGVector<float64_t> vect

CDenseFeatures<float64_t>* CKernelPCA::apply_to_string_features(CFeatures* features)
{
ASSERT(m_initialized)
check_fitted();

int32_t num_vectors = features->get_num_vectors();
int32_t i,j,k;
Expand Down
3 changes: 0 additions & 3 deletions src/shogun/preprocessor/KernelPCA.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ class CKernelPCA : public CPreprocessor
/** bias vector */
SGVector<float64_t> m_bias_vector;

/** true when already initialized */
bool m_initialized;

/** target dimension */
int32_t m_target_dim;

Expand Down
9 changes: 2 additions & 7 deletions src/shogun/preprocessor/NormOne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <shogun/features/Features.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/preprocessor/DensePreprocessor.h>
#include <shogun/preprocessor/NormOne.h>

Expand Down Expand Up @@ -59,11 +60,5 @@ SGMatrix<float64_t> CNormOne::apply_to_matrix(SGMatrix<float64_t> matrix)
/// result in feature matrix
SGVector<float64_t> CNormOne::apply_to_feature_vector(SGVector<float64_t> vector)
{
float64_t* normed_vec = SG_MALLOC(float64_t, vector.vlen);
float64_t norm = std::sqrt(linalg::dot(vector, vector));

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

return SGVector<float64_t>(normed_vec,vector.vlen);
return linalg::scale(vector, 1.0 / linalg::norm(vector));
}
11 changes: 4 additions & 7 deletions src/shogun/preprocessor/PCA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ void CPCA::init()
m_mean_vector = SGVector<float64_t>();
m_eigenvalues_vector = SGVector<float64_t>();
num_dim = 0;
m_initialized = false;
m_whitening = false;
m_mode = FIXED_NUMBER;
m_thresh = 1e-6;
Expand All @@ -59,8 +58,6 @@ void CPCA::init()
SG_ADD(&m_mean_vector, "mean_vector", "Mean Vector.", MS_NOT_AVAILABLE);
SG_ADD(&m_eigenvalues_vector, "eigenvalues_vector",
"Vector with Eigenvalues.", MS_NOT_AVAILABLE);
SG_ADD(&m_initialized, "initalized", "True when initialized.",
MS_NOT_AVAILABLE);
SG_ADD(&m_whitening, "whitening", "Whether data shall be whitened.",
MS_AVAILABLE);
SG_ADD((machine_int_t*) &m_mode, "mode", "PCA Mode.", MS_AVAILABLE);
Expand All @@ -82,7 +79,7 @@ CPCA::~CPCA()

void CPCA::fit(CFeatures* features)
{
if (m_initialized)
if (m_fitted)
cleanup();

auto feature_matrix =
Expand Down Expand Up @@ -119,7 +116,7 @@ void CPCA::fit(CFeatures* features)

// restore feature matrix
fmatrix = fmatrix.colwise() + data_mean;
m_initialized = true;
m_fitted = true;
}

void CPCA::init_with_evd(const SGMatrix<float64_t>& feature_matrix, int32_t max_dim_allowed)
Expand Down Expand Up @@ -284,12 +281,12 @@ void CPCA::cleanup()
m_transformation_matrix=SGMatrix<float64_t>();
m_mean_vector = SGVector<float64_t>();
m_eigenvalues_vector = SGVector<float64_t>();
m_initialized = false;
m_fitted = false;
}

SGMatrix<float64_t> CPCA::apply_to_matrix(SGMatrix<float64_t> matrix)
{
ASSERT(m_initialized)
check_fitted();

auto num_vectors = matrix.num_cols;
auto num_features = matrix.num_rows;
Expand Down
2 changes: 0 additions & 2 deletions src/shogun/preprocessor/PCA.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ class CPCA : public CDensePreprocessor<float64_t>
SGVector<float64_t> m_mean_vector;
/** eigenvalues vector */
SGVector<float64_t> m_eigenvalues_vector;
/** initialized */
bool m_initialized;
/** whitening */
bool m_whitening;
/** PCA mode */
Expand Down
38 changes: 12 additions & 26 deletions src/shogun/preprocessor/PruneVarSubMean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CPruneVarSubMean::~CPruneVarSubMean()

void CPruneVarSubMean::fit(CFeatures* features)
{
if (m_initialized)
if (m_fitted)
cleanup();

auto simple_features = features->as<CDenseFeatures<float64_t>>();
Expand Down Expand Up @@ -87,7 +87,7 @@ void CPruneVarSubMean::fit(CFeatures* features)
m_num_idx = num_ok;
m_mean = new_mean;

m_initialized = true;
m_fitted = true;
}

/// clean up allocated memory
Expand All @@ -96,13 +96,13 @@ void CPruneVarSubMean::cleanup()
m_idx=SGVector<int32_t>();
m_mean=SGVector<float64_t>();
m_std=SGVector<float64_t>();
m_initialized = false;
m_fitted = false;
}

SGMatrix<float64_t>
CPruneVarSubMean::apply_to_matrix(SGMatrix<float64_t> matrix)
{
ASSERT(m_initialized)
check_fitted();

auto num_vectors = matrix.num_cols;
auto result = matrix;
Expand Down Expand Up @@ -132,36 +132,23 @@ CPruneVarSubMean::apply_to_matrix(SGMatrix<float64_t> matrix)
/// result in feature matrix
SGVector<float64_t> CPruneVarSubMean::apply_to_feature_vector(SGVector<float64_t> vector)
{
float64_t* ret=NULL;
check_fitted();

if (m_initialized)
{
ret=SG_MALLOC(float64_t, m_num_idx);
SGVector<float64_t> out(m_num_idx);

if (m_divide_by_std)
{
for (int32_t i=0; i<m_num_idx; i++)
ret[i]=(vector.vector[m_idx[i]]-m_mean[i])/m_std[i];
}
else
{
for (int32_t i=0; i<m_num_idx; i++)
ret[i]=(vector.vector[m_idx[i]]-m_mean[i]);
}
}
else
for (auto i : range(m_num_idx))
{
ret=SG_MALLOC(float64_t, vector.vlen);
for (int32_t i=0; i<vector.vlen; i++)
ret[i]=vector.vector[i];
out[i] = vector[m_idx[i]] - m_mean[i];
if (m_divide_by_std)
out[i] /= m_std[i];
}

return SGVector<float64_t>(ret,m_num_idx);
return out;
}

void CPruneVarSubMean::init()
{
m_initialized = false;
m_fitted = false;
m_divide_by_std = false;
m_num_idx = 0;
m_idx = SGVector<int32_t>();
Expand All @@ -171,7 +158,6 @@ void CPruneVarSubMean::init()

void CPruneVarSubMean::register_parameters()
{
SG_ADD(&m_initialized, "initialized", "The prerpocessor is initialized", MS_NOT_AVAILABLE);
SG_ADD(&m_divide_by_std, "divide_by_std", "Divide by standard deviation", MS_AVAILABLE);
SG_ADD(&m_num_idx, "num_idx", "Number of elements in idx_vec", MS_NOT_AVAILABLE);
SG_ADD(&m_std, "std_vec", "Standard dev vector", MS_NOT_AVAILABLE);
Expand Down
3 changes: 0 additions & 3 deletions src/shogun/preprocessor/PruneVarSubMean.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ class CPruneVarSubMean : public CDensePreprocessor<float64_t>
int32_t m_num_idx;
/** divide by std */
bool m_divide_by_std;

/// true when already initialized
bool m_initialized;
};
}
#endif
15 changes: 6 additions & 9 deletions src/shogun/preprocessor/RescaleFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

using namespace shogun;

CRescaleFeatures::CRescaleFeatures()
: CDensePreprocessor<float64_t>(),
m_initialized(false)
CRescaleFeatures::CRescaleFeatures() : CDensePreprocessor<float64_t>()
{
register_parameters();
}
Expand All @@ -25,7 +23,7 @@ CRescaleFeatures::~CRescaleFeatures()

void CRescaleFeatures::fit(CFeatures* features)
{
if (m_initialized)
if (m_fitted)
cleanup();

auto simple_features = features->as<CDenseFeatures<float64_t>>();
Expand Down Expand Up @@ -65,18 +63,18 @@ void CRescaleFeatures::fit(CFeatures* features)
}
}

m_initialized = true;
m_fitted = true;
}

void CRescaleFeatures::cleanup()
{
m_initialized = false;
m_fitted = false;
}

SGMatrix<float64_t>
CRescaleFeatures::apply_to_matrix(SGMatrix<float64_t> matrix)
{
ASSERT(m_initialized);
check_fitted();

ASSERT(matrix.num_rows == m_min.vlen);

Expand All @@ -92,7 +90,7 @@ CRescaleFeatures::apply_to_matrix(SGMatrix<float64_t> matrix)

SGVector<float64_t> CRescaleFeatures::apply_to_feature_vector(SGVector<float64_t> vector)
{
ASSERT(m_initialized);
check_fitted();
ASSERT(m_min.vlen == vector.vlen);

float64_t* ret = SG_MALLOC(float64_t, vector.vlen);
Expand All @@ -108,5 +106,4 @@ void CRescaleFeatures::register_parameters()
{
SG_ADD(&m_min, "min", "minimum values of each feature", MS_NOT_AVAILABLE);
SG_ADD(&m_range, "range", "Reciprocal of the range of each feature", MS_NOT_AVAILABLE);
SG_ADD(&m_initialized, "initialized", "Indicator of the state of the preprocessor.", MS_NOT_AVAILABLE);
}
2 changes: 0 additions & 2 deletions src/shogun/preprocessor/RescaleFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ namespace shogun
SGVector<float64_t> m_min;
/** 1.0/(max[i]-min[i]) */
SGVector<float64_t> m_range;
/** true when already initialized */
bool m_initialized;
};
}

Expand Down
20 changes: 20 additions & 0 deletions src/shogun/transformer/Transformer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <shogun/base/Parameter.h>
#include <shogun/lib/exception/NotFittedException.h>
#include <shogun/transformer/Transformer.h>

namespace shogun
{

CTransformer::CTransformer() : CSGObject()
{
SG_ADD(
&m_fitted, "fitted", "Whether the transformer has been fitted.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's register this param as is_fitted

MS_NOT_AVAILABLE);
}

void CTransformer::check_fitted() const
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would rather go with the java like: is_fitted or is_fit depending which english we'd like to go with :) but let's stick to british (non north american english as in case of the exception type naming)

{
REQUIRE_E(
m_fitted, NotFittedException, "Transformer has not been fitted.\n")
}
}
Loading