Skip to content

Commit

Permalink
Make transformer throw NotFittedException if not fitted
Browse files Browse the repository at this point in the history
  • Loading branch information
vinx13 authored and vigsterkr committed Jul 11, 2018
1 parent d53dc1c commit a218726
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 50 deletions.
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
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
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
11 changes: 4 additions & 7 deletions src/shogun/preprocessor/PCA.cpp
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
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
15 changes: 6 additions & 9 deletions src/shogun/preprocessor/PruneVarSubMean.cpp
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)
{
REQUIRE(m_initialized, "Transformer has not been fitted.\n");
check_fitted();

auto num_vectors = matrix.num_cols;
auto result = matrix;
Expand Down Expand Up @@ -132,7 +132,7 @@ CPruneVarSubMean::apply_to_matrix(SGMatrix<float64_t> matrix)
/// result in feature matrix
SGVector<float64_t> CPruneVarSubMean::apply_to_feature_vector(SGVector<float64_t> vector)
{
REQUIRE(m_initialized, "Transformer has not been fitted.\n");
check_fitted();

SGVector<float64_t> out(m_num_idx);

Expand All @@ -148,7 +148,7 @@ SGVector<float64_t> CPruneVarSubMean::apply_to_feature_vector(SGVector<float64_t

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 @@ -158,9 +158,6 @@ void CPruneVarSubMean::init()

void CPruneVarSubMean::register_parameters()
{
SG_ADD(
&m_initialized, "initialized", "The preprocessor 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
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
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
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
@@ -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.",
MS_NOT_AVAILABLE);
}

void CTransformer::check_fitted() const
{
REQUIRE_E(
m_fitted, NotFittedException, "Transformer has not been fitted.\n")
}
}
12 changes: 9 additions & 3 deletions src/shogun/transformer/Transformer.h
Expand Up @@ -41,9 +41,7 @@ namespace shogun
{
public:
/** constructor */
CTransformer() : CSGObject()
{
}
CTransformer();

/** destructor */
virtual ~CTransformer()
Expand Down Expand Up @@ -101,6 +99,14 @@ namespace shogun
{
return false;
}

protected:
/** Check current transformer has been fitted, throw NotFittedException
* otherwise.
*/
void check_fitted() const;

bool m_fitted;
};
}
#endif /* TRANSFORMER_H_ */

0 comments on commit a218726

Please sign in to comment.