diff --git a/src/shogun/lib/exception/MachineNotTrainedException.h b/src/shogun/lib/exception/NotFittedException.h similarity index 69% rename from src/shogun/lib/exception/MachineNotTrainedException.h rename to src/shogun/lib/exception/NotFittedException.h index e02ab3d0b12..922b71d1a04 100644 --- a/src/shogun/lib/exception/MachineNotTrainedException.h +++ b/src/shogun/lib/exception/NotFittedException.h @@ -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) { } @@ -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) { } diff --git a/src/shogun/preprocessor/KernelPCA.cpp b/src/shogun/preprocessor/KernelPCA.cpp index 32f734b13ea..b0345a604c8 100644 --- a/src/shogun/preprocessor/KernelPCA.cpp +++ b/src/shogun/preprocessor/KernelPCA.cpp @@ -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(); m_bias_vector = SGVector(); @@ -59,7 +59,7 @@ void CKernelPCA::cleanup() if (m_init_features) SG_UNREF(m_init_features); - m_initialized = false; + m_fitted = false; } CKernelPCA::~CKernelPCA() @@ -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); @@ -122,13 +122,13 @@ void CKernelPCA::fit(CFeatures* features) m_bias_vector = SGVector(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(features->clone()); @@ -152,7 +152,7 @@ CFeatures* CKernelPCA::transform(CFeatures* features, bool inplace) SGMatrix 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); @@ -172,7 +172,7 @@ SGMatrix CKernelPCA::apply_to_feature_matrix(CFeatures* features) SGVector CKernelPCA::apply_to_feature_vector(SGVector vector) { - ASSERT(m_initialized) + check_fitted(); CFeatures* features = new CDenseFeatures(SGMatrix(vector)); @@ -186,7 +186,7 @@ SGVector CKernelPCA::apply_to_feature_vector(SGVector vect CDenseFeatures* 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; diff --git a/src/shogun/preprocessor/KernelPCA.h b/src/shogun/preprocessor/KernelPCA.h index 099e8131985..3e7b8457ca4 100644 --- a/src/shogun/preprocessor/KernelPCA.h +++ b/src/shogun/preprocessor/KernelPCA.h @@ -122,9 +122,6 @@ class CKernelPCA : public CPreprocessor /** bias vector */ SGVector m_bias_vector; - /** true when already initialized */ - bool m_initialized; - /** target dimension */ int32_t m_target_dim; diff --git a/src/shogun/preprocessor/PCA.cpp b/src/shogun/preprocessor/PCA.cpp index 1f611d9ec43..b0340b2f5e3 100644 --- a/src/shogun/preprocessor/PCA.cpp +++ b/src/shogun/preprocessor/PCA.cpp @@ -44,7 +44,6 @@ void CPCA::init() m_mean_vector = SGVector(); m_eigenvalues_vector = SGVector(); num_dim = 0; - m_initialized = false; m_whitening = false; m_mode = FIXED_NUMBER; m_thresh = 1e-6; @@ -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); @@ -82,7 +79,7 @@ CPCA::~CPCA() void CPCA::fit(CFeatures* features) { - if (m_initialized) + if (m_fitted) cleanup(); auto feature_matrix = @@ -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& feature_matrix, int32_t max_dim_allowed) @@ -284,12 +281,12 @@ void CPCA::cleanup() m_transformation_matrix=SGMatrix(); m_mean_vector = SGVector(); m_eigenvalues_vector = SGVector(); - m_initialized = false; + m_fitted = false; } SGMatrix CPCA::apply_to_matrix(SGMatrix matrix) { - ASSERT(m_initialized) + check_fitted(); auto num_vectors = matrix.num_cols; auto num_features = matrix.num_rows; diff --git a/src/shogun/preprocessor/PCA.h b/src/shogun/preprocessor/PCA.h index 76749858ce8..e1a13cde929 100644 --- a/src/shogun/preprocessor/PCA.h +++ b/src/shogun/preprocessor/PCA.h @@ -206,8 +206,6 @@ class CPCA : public CDensePreprocessor SGVector m_mean_vector; /** eigenvalues vector */ SGVector m_eigenvalues_vector; - /** initialized */ - bool m_initialized; /** whitening */ bool m_whitening; /** PCA mode */ diff --git a/src/shogun/preprocessor/PruneVarSubMean.cpp b/src/shogun/preprocessor/PruneVarSubMean.cpp index 27313678f8c..3ca4e190267 100644 --- a/src/shogun/preprocessor/PruneVarSubMean.cpp +++ b/src/shogun/preprocessor/PruneVarSubMean.cpp @@ -29,7 +29,7 @@ CPruneVarSubMean::~CPruneVarSubMean() void CPruneVarSubMean::fit(CFeatures* features) { - if (m_initialized) + if (m_fitted) cleanup(); auto simple_features = features->as>(); @@ -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 @@ -96,13 +96,13 @@ void CPruneVarSubMean::cleanup() m_idx=SGVector(); m_mean=SGVector(); m_std=SGVector(); - m_initialized = false; + m_fitted = false; } SGMatrix CPruneVarSubMean::apply_to_matrix(SGMatrix matrix) { - REQUIRE(m_initialized, "Transformer has not been fitted.\n"); + check_fitted(); auto num_vectors = matrix.num_cols; auto result = matrix; @@ -132,7 +132,7 @@ CPruneVarSubMean::apply_to_matrix(SGMatrix matrix) /// result in feature matrix SGVector CPruneVarSubMean::apply_to_feature_vector(SGVector vector) { - REQUIRE(m_initialized, "Transformer has not been fitted.\n"); + check_fitted(); SGVector out(m_num_idx); @@ -148,7 +148,7 @@ SGVector CPruneVarSubMean::apply_to_feature_vector(SGVector(); @@ -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); diff --git a/src/shogun/preprocessor/PruneVarSubMean.h b/src/shogun/preprocessor/PruneVarSubMean.h index 10f5d06eb5a..98a9ef1e8c3 100644 --- a/src/shogun/preprocessor/PruneVarSubMean.h +++ b/src/shogun/preprocessor/PruneVarSubMean.h @@ -69,9 +69,6 @@ class CPruneVarSubMean : public CDensePreprocessor int32_t m_num_idx; /** divide by std */ bool m_divide_by_std; - - /// true when already initialized - bool m_initialized; }; } #endif diff --git a/src/shogun/preprocessor/RescaleFeatures.cpp b/src/shogun/preprocessor/RescaleFeatures.cpp index efaee64a729..b47747e7938 100644 --- a/src/shogun/preprocessor/RescaleFeatures.cpp +++ b/src/shogun/preprocessor/RescaleFeatures.cpp @@ -11,9 +11,7 @@ using namespace shogun; -CRescaleFeatures::CRescaleFeatures() - : CDensePreprocessor(), - m_initialized(false) +CRescaleFeatures::CRescaleFeatures() : CDensePreprocessor() { register_parameters(); } @@ -25,7 +23,7 @@ CRescaleFeatures::~CRescaleFeatures() void CRescaleFeatures::fit(CFeatures* features) { - if (m_initialized) + if (m_fitted) cleanup(); auto simple_features = features->as>(); @@ -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 CRescaleFeatures::apply_to_matrix(SGMatrix matrix) { - ASSERT(m_initialized); + check_fitted(); ASSERT(matrix.num_rows == m_min.vlen); @@ -92,7 +90,7 @@ CRescaleFeatures::apply_to_matrix(SGMatrix matrix) SGVector CRescaleFeatures::apply_to_feature_vector(SGVector vector) { - ASSERT(m_initialized); + check_fitted(); ASSERT(m_min.vlen == vector.vlen); float64_t* ret = SG_MALLOC(float64_t, vector.vlen); @@ -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); } diff --git a/src/shogun/preprocessor/RescaleFeatures.h b/src/shogun/preprocessor/RescaleFeatures.h index aaee042f96d..5825fbe412f 100644 --- a/src/shogun/preprocessor/RescaleFeatures.h +++ b/src/shogun/preprocessor/RescaleFeatures.h @@ -73,8 +73,6 @@ namespace shogun SGVector m_min; /** 1.0/(max[i]-min[i]) */ SGVector m_range; - /** true when already initialized */ - bool m_initialized; }; } diff --git a/src/shogun/transformer/Transformer.cpp b/src/shogun/transformer/Transformer.cpp new file mode 100644 index 00000000000..13a9523284d --- /dev/null +++ b/src/shogun/transformer/Transformer.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +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") + } +} diff --git a/src/shogun/transformer/Transformer.h b/src/shogun/transformer/Transformer.h index 13513c3c6f1..03fc5d27ae5 100644 --- a/src/shogun/transformer/Transformer.h +++ b/src/shogun/transformer/Transformer.h @@ -41,9 +41,7 @@ namespace shogun { public: /** constructor */ - CTransformer() : CSGObject() - { - } + CTransformer(); /** destructor */ virtual ~CTransformer() @@ -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_ */