From 49926e653b7c1c672e22a970d71b675e445dcb3a Mon Sep 17 00:00:00 2001 From: Alexandr-Solovev Date: Wed, 1 Apr 2026 11:21:02 -0700 Subject: [PATCH] fixes --- .../kernel_function/kernel_function.h | 32 +++- cpp/daal/include/algorithms/svm/svm_model.h | 6 +- cpp/daal/include/daal.h | 4 - cpp/daal/include/daal_win.h | 4 - .../kernel_function_csr_base.h | 8 +- .../kernel_function_dense_base.h | 8 +- .../kernel_function_factory.cpp | 54 +++++++ .../kernel_function_linear.cpp | 6 +- .../kernel_function/kernel_function_linear.h | 137 ++++++++++++++++++ .../kernel_function_linear_batch_container.h | 2 +- ...n_linear_csr_fast_batch_fpt_dispatcher.cpp | 6 +- ...ear_dense_default_batch_fpt_dispatcher.cpp | 6 +- .../kernel_function/kernel_function_rbf.cpp | 6 +- .../kernel_function/kernel_function_rbf.h | 136 +++++++++++++++++ .../kernel_function_rbf_base.h | 2 +- .../kernel_function_rbf_batch_container.h | 2 +- ...tion_rbf_csr_fast_batch_fpt_dispatcher.cpp | 6 +- .../kernel_function_rbf_csr_fast_impl.i | 2 +- ...rbf_dense_default_batch_fpt_dispatcher.cpp | 6 +- .../kernel_function_rbf_dense_default_impl.i | 2 +- .../kernel_function/kernel_function_types.cpp | 2 +- .../kernel_function_types_linear.h | 111 ++++++++++++++ .../kernel_function_types_rbf.h | 110 ++++++++++++++ .../algo/svm/backend/kernel_function_impl.hpp | 4 +- .../svm/svm_multi_class_boser_csr_batch.cpp | 2 +- .../svm/svm_multi_class_boser_dense_batch.cpp | 3 +- .../svm/svm_multi_class_model_builder.cpp | 3 +- .../svm/svm_multi_class_thunder_csr_batch.cpp | 2 +- .../svm_multi_class_thunder_dense_batch.cpp | 58 +++++--- .../svm/svm_two_class_boser_csr_batch.cpp | 2 +- .../svm/svm_two_class_boser_dense_batch.cpp | 3 +- .../svm/svm_two_class_model_builder.cpp | 3 +- .../svm/svm_two_class_thunder_csr_batch.cpp | 2 +- .../svm/svm_two_class_thunder_dense_batch.cpp | 41 ++++-- 34 files changed, 688 insertions(+), 93 deletions(-) create mode 100644 cpp/daal/src/algorithms/kernel_function/kernel_function_factory.cpp create mode 100644 cpp/daal/src/algorithms/kernel_function/kernel_function_linear.h create mode 100644 cpp/daal/src/algorithms/kernel_function/kernel_function_rbf.h create mode 100644 cpp/daal/src/algorithms/kernel_function/kernel_function_types_linear.h create mode 100644 cpp/daal/src/algorithms/kernel_function/kernel_function_types_rbf.h diff --git a/cpp/daal/include/algorithms/kernel_function/kernel_function.h b/cpp/daal/include/algorithms/kernel_function/kernel_function.h index 69aa21e3085..a09bf7649dc 100644 --- a/cpp/daal/include/algorithms/kernel_function/kernel_function.h +++ b/cpp/daal/include/algorithms/kernel_function/kernel_function.h @@ -17,7 +17,7 @@ /* //++ -// Implementation of the kernel function interface. +// Public interface for kernel functions. //-- */ @@ -34,12 +34,24 @@ namespace algorithms { namespace kernel_function { -namespace interface1 -{ /** * @addtogroup kernel_function * @{ */ + +/** + * + * \brief Available kernel function types + */ +enum KernelFunctionType +{ + linearKernel = 0, /*!< Linear kernel function: k(X,Y) + b */ + rbfKernel = 1, /*!< Radial Basis Function (RBF) kernel */ + polynomialKernel = 2 /*!< Polynomial kernel function */ +}; + +namespace interface1 +{ /** * * \brief Abstract class that specifies the interface of the algorithms @@ -110,11 +122,21 @@ class KernelIface : public daal::algorithms::Analysis KernelIface & operator=(const KernelIface &); }; typedef services::SharedPtr KernelIfacePtr; -/** @} */ + +/** + * Creates a kernel function of specified type + * \tparam algorithmFPType Data type for the kernel function (float or double) + * \param[in] type Type of the kernel function to create + * \return Pointer to the newly created kernel function + */ +template +DAAL_EXPORT KernelIfacePtr createKernelFunction(KernelFunctionType type); + } // namespace interface1 using interface1::KernelIface; using interface1::KernelIfacePtr; - +using interface1::createKernelFunction; +/** @} */ } // namespace kernel_function } // namespace algorithms } // namespace daal diff --git a/cpp/daal/include/algorithms/svm/svm_model.h b/cpp/daal/include/algorithms/svm/svm_model.h index e9b613896b5..021bbb0eb05 100644 --- a/cpp/daal/include/algorithms/svm/svm_model.h +++ b/cpp/daal/include/algorithms/svm/svm_model.h @@ -28,8 +28,6 @@ #include "data_management/data/csr_numeric_table.h" #include "algorithms/model.h" #include "algorithms/kernel_function/kernel_function.h" -#include "algorithms/kernel_function/kernel_function_linear.h" -#include "algorithms/kernel_function/kernel_function_types.h" #include "algorithms/classifier/classifier_model.h" namespace daal @@ -65,8 +63,8 @@ namespace interface2 /* [Parameter source code] */ struct DAAL_EXPORT Parameter : public classifier::Parameter { - Parameter(const services::SharedPtr & kernelForParameter = - services::SharedPtr(new kernel_function::linear::Batch<>()), + Parameter(const kernel_function::KernelIfacePtr & kernelForParameter = + kernel_function::createKernelFunction(kernel_function::linearKernel), double C = 1.0, double accuracyThreshold = 0.001, double tau = 1.0e-6, size_t maxIterations = 1000000, size_t cacheSize = 8000000, bool doShrinking = true, size_t shrinkingStep = 1000) : C(C), diff --git a/cpp/daal/include/daal.h b/cpp/daal/include/daal.h index 8ad8668ec36..3c107fb0374 100755 --- a/cpp/daal/include/daal.h +++ b/cpp/daal/include/daal.h @@ -78,11 +78,7 @@ #include "algorithms/pca/transform/pca_transform_types.h" #include "algorithms/pca/transform/pca_transform_batch.h" #include "algorithms/kernel_function/kernel_function_types.h" -#include "algorithms/kernel_function/kernel_function_types_linear.h" -#include "algorithms/kernel_function/kernel_function_types_rbf.h" #include "algorithms/kernel_function/kernel_function.h" -#include "algorithms/kernel_function/kernel_function_linear.h" -#include "algorithms/kernel_function/kernel_function_rbf.h" #include "algorithms/svm/svm_model.h" #include "algorithms/svm/svm_model_builder.h" #include "algorithms/svm/svm_train_types.h" diff --git a/cpp/daal/include/daal_win.h b/cpp/daal/include/daal_win.h index 54e681025ca..4d92d39b115 100644 --- a/cpp/daal/include/daal_win.h +++ b/cpp/daal/include/daal_win.h @@ -90,11 +90,7 @@ #include "algorithms/pca/transform/pca_transform_types.h" #include "algorithms/pca/transform/pca_transform_batch.h" #include "algorithms/kernel_function/kernel_function_types.h" -#include "algorithms/kernel_function/kernel_function_types_linear.h" -#include "algorithms/kernel_function/kernel_function_types_rbf.h" #include "algorithms/kernel_function/kernel_function.h" -#include "algorithms/kernel_function/kernel_function_linear.h" -#include "algorithms/kernel_function/kernel_function_rbf.h" #include "algorithms/svm/svm_model.h" #include "algorithms/svm/svm_model_builder.h" #include "algorithms/svm/svm_train_types.h" diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_csr_base.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_csr_base.h index c59c2e177aa..34b69fd14f3 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_csr_base.h +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_csr_base.h @@ -25,10 +25,10 @@ #define __KERNEL_FUNCTION_CSR_BASE_H__ #include "data_management/data/numeric_table.h" -#include "algorithms/kernel_function/kernel_function_types_linear.h" -#include "algorithms/kernel_function/kernel_function_types_rbf.h" -#include "algorithms/kernel_function/kernel_function_linear.h" -#include "algorithms/kernel_function/kernel_function_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_types_linear.h" +#include "src/algorithms/kernel_function/kernel_function_types_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_linear.h" +#include "src/algorithms/kernel_function/kernel_function_rbf.h" #include "src/data_management/service_numeric_table.h" #include "src/algorithms/kernel.h" #include "src/algorithms/kernel_function/kernel_function_dense_base.h" diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_dense_base.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_dense_base.h index 02b77eac8b5..cc46664adf2 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_dense_base.h +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_dense_base.h @@ -25,10 +25,10 @@ #define __KERNEL_FUNCTION_DENSE_BASE_H__ #include "data_management/data/numeric_table.h" -#include "algorithms/kernel_function/kernel_function_types_linear.h" -#include "algorithms/kernel_function/kernel_function_types_rbf.h" -#include "algorithms/kernel_function/kernel_function_linear.h" -#include "algorithms/kernel_function/kernel_function_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_types_linear.h" +#include "src/algorithms/kernel_function/kernel_function_types_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_linear.h" +#include "src/algorithms/kernel_function/kernel_function_rbf.h" #include "src/data_management/service_micro_table.h" #include "src/algorithms/kernel.h" diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_factory.cpp b/cpp/daal/src/algorithms/kernel_function/kernel_function_factory.cpp new file mode 100644 index 00000000000..86acb97ec0f --- /dev/null +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_factory.cpp @@ -0,0 +1,54 @@ +/* file: kernel_function_factory.cpp */ +/******************************************************************************* +* Copyright 2014 Intel Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +/* +//++ +// Implementation of kernel function factory. +//-- +*/ + +#include "algorithms/kernel_function/kernel_function.h" +#include "src/algorithms/kernel_function/kernel_function_linear.h" +#include "src/algorithms/kernel_function/kernel_function_rbf.h" + +namespace daal +{ +namespace algorithms +{ +namespace kernel_function +{ +namespace interface1 +{ +template +DAAL_EXPORT KernelIfacePtr createKernelFunction(KernelFunctionType type) +{ + switch (type) + { + case linearKernel: return KernelIfacePtr(new linear::internal::Batch()); + case rbfKernel: return KernelIfacePtr(new rbf::internal::Batch()); + default: return KernelIfacePtr(); + } +} + +// Explicit instantiations +template DAAL_EXPORT KernelIfacePtr createKernelFunction(KernelFunctionType type); +template DAAL_EXPORT KernelIfacePtr createKernelFunction(KernelFunctionType type); + +} // namespace interface1 +} // namespace kernel_function +} // namespace algorithms +} // namespace daal diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear.cpp b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear.cpp index 4257773e91f..7578daabd1d 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear.cpp +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear.cpp @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_types_linear.h" +#include "src/algorithms/kernel_function/kernel_function_types_linear.h" #include "src/services/service_defines.h" using namespace daal::data_management; @@ -35,7 +35,7 @@ namespace kernel_function { namespace linear { -namespace interface1 +namespace internal { Parameter::Parameter(double k, double b) : ParameterBase(), k(k), b(b) {} @@ -60,7 +60,7 @@ Status Input::check(const daal::algorithms::Parameter * par, int method) const return services::Status(); } -} // namespace interface1 +} // namespace internal } // namespace linear } // namespace kernel_function } // namespace algorithms diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear.h new file mode 100644 index 00000000000..0107a523151 --- /dev/null +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear.h @@ -0,0 +1,137 @@ +/* file: kernel_function_linear.h */ +/******************************************************************************* +* Copyright 2014 Intel Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +/* +//++ +// Implementation of the interface for the linear kernel function algorithm +//-- +*/ + +#ifndef __KERNEL_FUNCTION_LINEAR_H__ +#define __KERNEL_FUNCTION_LINEAR_H__ + +#include "algorithms/algorithm.h" +#include "data_management/data/numeric_table.h" +#include "src/algorithms/kernel_function/kernel_function_types_linear.h" +#include "algorithms/kernel_function/kernel_function.h" + +namespace daal +{ +namespace algorithms +{ +namespace kernel_function +{ +namespace linear +{ +namespace internal +{ +/** + * @defgroup kernel_function_linear_batch Batch + * @ingroup kernel_function_linear + * @{ + */ +/** + * + * \brief Computes a linear kernel function in the batch processing mode. + * + * + * \tparam algorithmFPType Data type to use in intermediate computations of kernel functions, double or float + * \tparam method Computation method of the algorithm, \ref Method + * + * \par Enumerations + * - \ref Method Methods for computing kernel functions + * - \ref InputId Identifiers of input objects for the kernel function algorithm + * - \ref ResultId Identifiers of results of the kernel function algorithm + * + * \par References + * - \ref interface1::Result "Result" class + * + * \DAAL_DEPRECATED + */ +template +class Batch : public KernelIface +{ +public: + typedef KernelIface super; + + typedef algorithms::kernel_function::linear::Input InputType; + typedef algorithms::kernel_function::linear::Parameter ParameterType; + typedef typename super::ResultType ResultType; + + ParameterType parameter; /*!< Parameter of the kernel function*/ + InputType input; /*!< %Input data structure */ + + /** Default constructor */ + DAAL_DEPRECATED Batch(); + + /** + * Constructs linear kernel function algorithm by copying input objects and parameters + * of another linear kernel function algorithm + * \param[in] other An algorithm to be used as the source to initialize the input objects + * and parameters of the algorithm + */ + Batch(const Batch & other); + + /** + * Returns the method of the algorithm + * \return Method of the algorithm + */ + int getMethod() const override { return (int)method; } + + /** + * Get input objects for the kernel function algorithm + * \return %Input objects for the kernel function algorithm + */ + InputType * getInput() override { return &input; } + + /** + * Get parameters of the kernel function algorithm + * \return Parameters of the kernel function algorithm + */ + ParameterBase * getParameter() override { return ¶meter; } + + /** + * Returns a pointer to the newly allocated linear kernel function algorithm with a copy of input objects + * and parameters of this linear kernel function algorithm + * \return Pointer to the newly allocated algorithm + */ + services::SharedPtr > clone() const { return services::SharedPtr >(cloneImpl()); } + +protected: + void initialize(); + + Batch * cloneImpl() const override { return new Batch(*this); } + + services::Status allocateResult() override + { + services::Status s = _result->allocate(&input, ¶meter, (int)method); + _res = _result.get(); + return s; + } + +private: + Batch & operator=(const Batch &); +}; +/** @} */ +} // namespace internal +using internal::Batch; + +} // namespace linear +} // namespace kernel_function +} // namespace algorithms +} // namespace daal +#endif diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_batch_container.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_batch_container.h index 9303fe34bb3..5d926941022 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_batch_container.h +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_batch_container.h @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_linear.h" +#include "src/algorithms/kernel_function/kernel_function_linear.h" #include "src/algorithms/algorithm_dispatch_container_batch.h" #include "src/algorithms/kernel_function/polynomial/kernel_function_polynomial.h" #include "src/algorithms/kernel_function/polynomial/kernel_function_polynomial_dense_default_kernel.h" diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_csr_fast_batch_fpt_dispatcher.cpp b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_csr_fast_batch_fpt_dispatcher.cpp index 56dcca603f3..4e65e4267a2 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_csr_fast_batch_fpt_dispatcher.cpp +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_csr_fast_batch_fpt_dispatcher.cpp @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_linear.h" +#include "src/algorithms/kernel_function/kernel_function_linear.h" #include "src/algorithms/kernel_function/kernel_function_linear_batch_container.h" namespace daal @@ -33,7 +33,7 @@ namespace kernel_function { namespace linear { -namespace interface1 +namespace internal { template <> void Batch::initialize() @@ -55,7 +55,7 @@ DAAL_EXPORT BatchType::Batch(const BatchType & other) : KernelIface(other), para { initialize(); } -} // namespace interface1 +} // namespace internal } // namespace linear } // namespace kernel_function } // namespace algorithms diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_dense_default_batch_fpt_dispatcher.cpp b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_dense_default_batch_fpt_dispatcher.cpp index f5257645060..dc19295dba9 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_dense_default_batch_fpt_dispatcher.cpp +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_linear_dense_default_batch_fpt_dispatcher.cpp @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_linear.h" +#include "src/algorithms/kernel_function/kernel_function_linear.h" #include "src/algorithms/kernel_function/kernel_function_linear_batch_container.h" namespace daal @@ -33,7 +33,7 @@ namespace kernel_function { namespace linear { -namespace interface1 +namespace internal { template <> void Batch::initialize() @@ -55,7 +55,7 @@ DAAL_EXPORT BatchType::Batch(const BatchType & other) : KernelIface(other), para { initialize(); } -} // namespace interface1 +} // namespace internal } // namespace linear } // namespace kernel_function } // namespace algorithms diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf.cpp b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf.cpp index 1f40f6f9a4a..a06b2bac75c 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf.cpp +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf.cpp @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_types_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_types_rbf.h" #include "src/services/service_defines.h" using namespace daal::data_management; @@ -35,7 +35,7 @@ namespace kernel_function { namespace rbf { -namespace interface1 +namespace internal { Parameter::Parameter(double sigma) : ParameterBase(), sigma(sigma) {} @@ -60,7 +60,7 @@ Status Input::check(const daal::algorithms::Parameter * par, int method) const return services::Status(); } -} // namespace interface1 +} // namespace internal } // namespace rbf } // namespace kernel_function } // namespace algorithms diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf.h new file mode 100644 index 00000000000..8ea6172377c --- /dev/null +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf.h @@ -0,0 +1,136 @@ +/* file: kernel_function_rbf.h */ +/******************************************************************************* +* Copyright 2014 Intel Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +/* +//++ +// Implementation of the interface for the radial basis function (RBF) kernel algorithm +//-- +*/ + +#ifndef __KERNEL_FUNCTION_RBF_H__ +#define __KERNEL_FUNCTION_RBF_H__ + +#include "algorithms/algorithm.h" +#include "data_management/data/numeric_table.h" +#include "src/algorithms/kernel_function/kernel_function_types_rbf.h" +#include "algorithms/kernel_function/kernel_function.h" + +namespace daal +{ +namespace algorithms +{ +namespace kernel_function +{ +namespace rbf +{ +namespace internal +{ +/** + * @defgroup kernel_function_rbf_batch Batch + * @ingroup kernel_function_rbf + * @{ + */ +/** + * + * \brief Computes the RBF kernel function in the batch processing mode. + * + * + * \tparam algorithmFPType Data type to use in intermediate computations of kernel functions, double or float + * \tparam method Computation method of the algorithm, \ref Method + * + * \par Enumerations + * - \ref Method Methods for computing kernel functions + * - \ref InputId Identifiers of input objects for the kernel function algorithm + * - \ref ResultId Identifiers of results of the kernel function algorithm + * + * \par References + * - \ref interface1::Result "Result" class + * + * \DAAL_DEPRECATED + */ +template +class Batch : public KernelIface +{ +public: + typedef KernelIface super; + + typedef algorithms::kernel_function::rbf::Input InputType; + typedef algorithms::kernel_function::rbf::Parameter ParameterType; + typedef typename super::ResultType ResultType; + + ParameterType parameter; /*!< Parameter of the kernel function*/ + InputType input; /*!< %Input data structure */ + + /** Default constructor */ + DAAL_DEPRECATED Batch(); + + /** + * Constructs RBF kernel function algorithm by copying input objects and parameters + * of another RBF kernel function algorithm + * \param[in] other An algorithm to be used as the source to initialize the input objects + * and parameters of the algorithm + */ + Batch(const Batch & other); + + /** + * Returns the method of the algorithm + * \return Method of the algorithm + */ + int getMethod() const override { return (int)method; } + + /** + * Get input objects for the kernel function algorithm + * \return %Input objects for the kernel function algorithm + */ + InputType * getInput() override { return &input; } + + /** + * Get parameters of the kernel function algorithm + * \return Parameters of the kernel function algorithm + */ + ParameterBase * getParameter() override { return ¶meter; } + + /** + * Returns a pointer to the newly allocated RBF kernel function algorithm with a copy of input objects + * and parameters of this RBF kernel function algorithm + * \return Pointer to the newly allocated algorithm + */ + services::SharedPtr > clone() const { return services::SharedPtr >(cloneImpl()); } + +protected: + void initialize(); + Batch * cloneImpl() const override { return new Batch(*this); } + + services::Status allocateResult() override + { + services::Status s = _result->allocate(&input, ¶meter, (int)method); + _res = _result.get(); + return s; + } + +private: + Batch & operator=(const Batch &); +}; +/** @} */ +} // namespace internal +using internal::Batch; + +} // namespace rbf +} // namespace kernel_function +} // namespace algorithms +} // namespace daal +#endif diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_base.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_base.h index 34b5fedec3a..fd45aab1083 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_base.h +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_base.h @@ -24,7 +24,7 @@ #ifndef __KERNEL_FUNCTION_RBF_BASE_H__ #define __KERNEL_FUNCTION_RBF_BASE_H__ -#include "algorithms/kernel_function/kernel_function_types_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_types_rbf.h" #include "src/algorithms/kernel.h" namespace daal diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_batch_container.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_batch_container.h index f8e166b03b0..eb79e4b83a3 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_batch_container.h +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_batch_container.h @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_rbf.h" #include "src/algorithms/algorithm_dispatch_container_batch.h" #include "src/algorithms/kernel_function/kernel_function_rbf_dense_default_kernel.h" #include "src/algorithms/kernel_function/kernel_function_rbf_csr_fast_kernel.h" diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_csr_fast_batch_fpt_dispatcher.cpp b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_csr_fast_batch_fpt_dispatcher.cpp index 122721cc5cd..2e91987550b 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_csr_fast_batch_fpt_dispatcher.cpp +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_csr_fast_batch_fpt_dispatcher.cpp @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_rbf.h" #include "src/algorithms/kernel_function/kernel_function_rbf_batch_container.h" #include "src/algorithms/kernel_function/kernel_function_rbf_csr_fast_kernel.h" @@ -34,7 +34,7 @@ namespace kernel_function { namespace rbf { -namespace interface1 +namespace internal { template <> void Batch::initialize() @@ -56,7 +56,7 @@ DAAL_EXPORT BatchType::Batch(const BatchType & other) : KernelIface(other), para { initialize(); } -} // namespace interface1 +} // namespace internal } // namespace rbf } // namespace kernel_function } // namespace algorithms diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_csr_fast_impl.i b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_csr_fast_impl.i index 1ae18a82087..672c44e22f2 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_csr_fast_impl.i +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_csr_fast_impl.i @@ -24,7 +24,7 @@ #ifndef __KERNEL_FUNCTION_RBF_CSR_FAST_IMPL_I__ #define __KERNEL_FUNCTION_RBF_CSR_FAST_IMPL_I__ -#include "algorithms/kernel_function/kernel_function_types_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_types_rbf.h" #include "src/data_management/service_numeric_table.h" #include "src/threading/threading.h" #include "src/externals/service_spblas.h" diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_dense_default_batch_fpt_dispatcher.cpp b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_dense_default_batch_fpt_dispatcher.cpp index e6080ad53bd..94f2a0bdfb5 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_dense_default_batch_fpt_dispatcher.cpp +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_dense_default_batch_fpt_dispatcher.cpp @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_rbf.h" #include "src/algorithms/kernel_function/kernel_function_rbf_batch_container.h" #include "src/algorithms/kernel_function/kernel_function_rbf_dense_default_kernel.h" @@ -34,7 +34,7 @@ namespace kernel_function { namespace rbf { -namespace interface1 +namespace internal { template <> void Batch::initialize() @@ -56,7 +56,7 @@ DAAL_EXPORT BatchType::Batch(const BatchType & other) : KernelIface(other), para { initialize(); } -} // namespace interface1 +} // namespace internal } // namespace rbf } // namespace kernel_function } // namespace algorithms diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_dense_default_impl.i b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_dense_default_impl.i index 0a73fa693da..01f840a6e12 100755 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_dense_default_impl.i +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_rbf_dense_default_impl.i @@ -24,7 +24,7 @@ #ifndef __KERNEL_FUNCTION_RBF_DENSE_DEFAULT_IMPL_I__ #define __KERNEL_FUNCTION_RBF_DENSE_DEFAULT_IMPL_I__ -#include "algorithms/kernel_function/kernel_function_types_rbf.h" +#include "src/algorithms/kernel_function/kernel_function_types_rbf.h" #include "src/data_management/service_numeric_table.h" #include "src/externals/service_math.h" #include "src/externals/service_blas.h" diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_types.cpp b/cpp/daal/src/algorithms/kernel_function/kernel_function_types.cpp index 6575bae60ec..6bed50afc51 100644 --- a/cpp/daal/src/algorithms/kernel_function/kernel_function_types.cpp +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_types.cpp @@ -21,7 +21,7 @@ //-- */ -#include "algorithms/kernel_function/kernel_function_linear.h" +#include "src/algorithms/kernel_function/kernel_function_linear.h" #include "src/algorithms/kernel_function/kernel_function_linear_batch_container.h" namespace daal diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_types_linear.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_types_linear.h new file mode 100644 index 00000000000..924d319f830 --- /dev/null +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_types_linear.h @@ -0,0 +1,111 @@ +/* file: kernel_function_types_linear.h */ +/******************************************************************************* +* Copyright 2014 Intel Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +/* +//++ +// Kernel function parameter structure +//-- +*/ + +#ifndef __KERNEL_FUNCTION_TYPES_LINEAR_H__ +#define __KERNEL_FUNCTION_TYPES_LINEAR_H__ + +#include "algorithms/kernel_function/kernel_function_types.h" + +namespace daal +{ +namespace algorithms +{ +/** + * @defgroup kernel_function_linear Linear Kernel + * \copydoc daal::algorithms::kernel_function::linear + * @ingroup kernel_function + * @{ + */ +/** + * \brief Contains classes for computing kernel functions + */ +namespace kernel_function +{ +/** + * \brief Contains classes for computing linear kernel functions + */ +namespace linear +{ +/** + * + * Method of the kernel function + */ +enum Method +{ + defaultDense = 0, /*!< Default method for computing linear kernel functions */ + fastCSR = 1 /*!< Fast: performance-oriented method. Works with Compressed Sparse Rows (CSR) numeric tables */ +}; + +/** + * \brief Contains version 1.0 of the oneAPI Data Analytics Library interface. + */ +namespace internal +{ +/** + * + * \brief Parameters for the linear kernel function k(X,Y) + b + * + * \snippet kernel_function/kernel_function_types_linear.h Linear input object source code + * + * \DAAL_DEPRECATED + */ +/* [Linear input object source code] */ +struct Parameter : public ParameterBase +{ + DAAL_DEPRECATED Parameter(double k = 1.0, double b = 0.0); + double k; /*!< Linear kernel coefficient k in the k(X,Y) + b model */ + double b; /*!< Linear kernel coefficient b in the k(X,Y) + b model */ +}; +/* [Linear input object source code] */ + +/** + * + * \brief %Input objects for the kernel function linear algorithm + * + * \DAAL_DEPRECATED + */ +class Input : public kernel_function::Input +{ +public: + DAAL_DEPRECATED Input(); + Input(const Input & other); + Input & operator=(const Input & other); + virtual ~Input() {} + + /** + * Checks input objects of the kernel function linear algorithm + * \param[in] par %Input objects of the algorithm + * \param[in] method Computation method of the algorithm + */ + services::Status check(const daal::algorithms::Parameter * par, int method) const override; +}; +/** @} */ +} // namespace internal +using internal::Input; +using internal::Parameter; + +} // namespace linear +} // namespace kernel_function +} // namespace algorithms +} // namespace daal +#endif diff --git a/cpp/daal/src/algorithms/kernel_function/kernel_function_types_rbf.h b/cpp/daal/src/algorithms/kernel_function/kernel_function_types_rbf.h new file mode 100644 index 00000000000..8d3b66560e8 --- /dev/null +++ b/cpp/daal/src/algorithms/kernel_function/kernel_function_types_rbf.h @@ -0,0 +1,110 @@ +/* file: kernel_function_types_rbf.h */ +/******************************************************************************* +* Copyright 2014 Intel Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +/* +//++ +// Kernel function parameter structure +//-- +*/ + +#ifndef __KERNEL_FUNCTION_TYPES_RBF_H__ +#define __KERNEL_FUNCTION_TYPES_RBF_H__ + +#include "algorithms/kernel_function/kernel_function_types.h" + +namespace daal +{ +namespace algorithms +{ +/** + * @defgroup kernel_function_rbf Radial Basis Function Kernel + * \copydoc daal::algorithms::kernel_function::rbf + * @ingroup kernel_function + * @{ + */ +/** + * \brief Contains classes for computing kernel functions + */ +namespace kernel_function +{ +/** + * \brief Contains classes for computing the radial basis function (RBF) kernel + */ +namespace rbf +{ +/** + * + * Method for computing kernel functions + */ +enum Method +{ + defaultDense = 0, /*!< Default method for computing the RBF kernel */ + fastCSR = 1 /*!< Fast: performance-oriented method. Works with Compressed Sparse Rows (CSR) numeric tables */ +}; + +/** + * \brief Contains version 1.0 of the oneAPI Data Analytics Library interface. + */ +namespace internal +{ +/** + * + * \brief Parameters for the radial basis function (RBF) kernel + * + * \snippet kernel_function/kernel_function_types_rbf.h RBF input object source code + * + * \DAAL_DEPRECATED + */ +/* [RBF input object source code] */ +struct Parameter : public ParameterBase +{ + DAAL_DEPRECATED Parameter(double sigma = 1.0); + double sigma; /*!< RBF kernel coefficient */ +}; +/* [RBF input object source code] */ + +/** + * + * \brief %Input objects for the RBF kernel algorithm + * + * \DAAL_DEPRECATED + */ +class Input : public kernel_function::Input +{ +public: + DAAL_DEPRECATED Input(); + Input(const Input & other); + Input & operator=(const Input & other); + virtual ~Input() {} + + /** + * Checks input objects of the RBF kernel algorithm + * \param[in] par %Input objects of the algorithm + * \param[in] method Computation method of the algorithm + */ + services::Status check(const daal::algorithms::Parameter * par, int method) const override; +}; +/** @} */ +} // namespace internal +using internal::Input; +using internal::Parameter; + +} // namespace rbf +} // namespace kernel_function +} // namespace algorithms +} // namespace daal +#endif diff --git a/cpp/oneapi/dal/algo/svm/backend/kernel_function_impl.hpp b/cpp/oneapi/dal/algo/svm/backend/kernel_function_impl.hpp index f59afa79ad1..52a0615c4c5 100644 --- a/cpp/oneapi/dal/algo/svm/backend/kernel_function_impl.hpp +++ b/cpp/oneapi/dal/algo/svm/backend/kernel_function_impl.hpp @@ -18,9 +18,9 @@ #include "oneapi/dal/algo/svm/common.hpp" -#include +#include "daal/src/algorithms/kernel_function/kernel_function_linear.h" #include "daal/src/algorithms/kernel_function/polynomial/kernel_function_polynomial.h" -#include +#include "daal/src/algorithms/kernel_function/kernel_function_rbf.h" namespace oneapi::dal::svm::detail { namespace v1 { diff --git a/examples/daal/cpp/source/svm/svm_multi_class_boser_csr_batch.cpp b/examples/daal/cpp/source/svm/svm_multi_class_boser_csr_batch.cpp index e3d1fed9f81..aeb9dadca16 100644 --- a/examples/daal/cpp/source/svm/svm_multi_class_boser_csr_batch.cpp +++ b/examples/daal/cpp/source/svm/svm_multi_class_boser_csr_batch.cpp @@ -50,7 +50,7 @@ services::SharedPtr > prediction(new svm::prediction::B multi_class_classifier::training::ResultPtr trainingResult; multi_class_classifier::prediction::ResultPtr predictionResult; kernel_function::KernelIfacePtr kernel( - new kernel_function::linear::Batch()); + kernel_function::createKernelFunction(kernel_function::linearKernel)); NumericTablePtr testGroundTruth; void trainModel(); diff --git a/examples/daal/cpp/source/svm/svm_multi_class_boser_dense_batch.cpp b/examples/daal/cpp/source/svm/svm_multi_class_boser_dense_batch.cpp index cd6a739585c..2a47768c9bb 100644 --- a/examples/daal/cpp/source/svm/svm_multi_class_boser_dense_batch.cpp +++ b/examples/daal/cpp/source/svm/svm_multi_class_boser_dense_batch.cpp @@ -48,7 +48,8 @@ services::SharedPtr > prediction(new svm::prediction::B multi_class_classifier::training::ResultPtr trainingResult; multi_class_classifier::prediction::ResultPtr predictionResult; -kernel_function::KernelIfacePtr kernel(new kernel_function::linear::Batch<>()); +kernel_function::KernelIfacePtr kernel( + kernel_function::createKernelFunction(kernel_function::linearKernel)); void trainModel(); void testModel(); diff --git a/examples/daal/cpp/source/svm/svm_multi_class_model_builder.cpp b/examples/daal/cpp/source/svm/svm_multi_class_model_builder.cpp index 40ae2318720..3ca5606ecd0 100644 --- a/examples/daal/cpp/source/svm/svm_multi_class_model_builder.cpp +++ b/examples/daal/cpp/source/svm/svm_multi_class_model_builder.cpp @@ -47,7 +47,8 @@ const size_t nClasses = 3; services::SharedPtr > prediction(new svm::prediction::Batch<>()); classifier::prediction::ResultPtr predictionResult; -kernel_function::KernelIfacePtr kernel(new kernel_function::linear::Batch<>()); +kernel_function::KernelIfacePtr kernel( + kernel_function::createKernelFunction(kernel_function::linearKernel)); NumericTablePtr testGroundTruth; multi_class_classifier::ModelPtr buildModelFromTraining(); diff --git a/examples/daal/cpp/source/svm/svm_multi_class_thunder_csr_batch.cpp b/examples/daal/cpp/source/svm/svm_multi_class_thunder_csr_batch.cpp index e4722dcec0d..486e5697fc3 100644 --- a/examples/daal/cpp/source/svm/svm_multi_class_thunder_csr_batch.cpp +++ b/examples/daal/cpp/source/svm/svm_multi_class_thunder_csr_batch.cpp @@ -50,7 +50,7 @@ services::SharedPtr > prediction(new svm::prediction::B multi_class_classifier::training::ResultPtr trainingResult; multi_class_classifier::prediction::ResultPtr predictionResult; kernel_function::KernelIfacePtr kernel( - new kernel_function::linear::Batch()); + kernel_function::createKernelFunction(kernel_function::linearKernel)); NumericTablePtr testGroundTruth; void trainModel(); diff --git a/examples/daal/cpp/source/svm/svm_multi_class_thunder_dense_batch.cpp b/examples/daal/cpp/source/svm/svm_multi_class_thunder_dense_batch.cpp index ec5ff7f9f66..acef52a2df4 100644 --- a/examples/daal/cpp/source/svm/svm_multi_class_thunder_dense_batch.cpp +++ b/examples/daal/cpp/source/svm/svm_multi_class_thunder_dense_batch.cpp @@ -18,7 +18,7 @@ /* ! Content: ! C++ example of multi-class support vector machine (SVM) classification using -! the Thunder method +! the Thunder method with different kernel functions (Linear and RBF) ! !******************************************************************************/ @@ -42,17 +42,12 @@ const std::string testDatasetLabelFileName = "data/svm_multi_class_test_dense_la const size_t nClasses = 5; -services::SharedPtr > training( - new svm::training::Batch()); -services::SharedPtr > prediction(new svm::prediction::Batch<>()); - multi_class_classifier::training::ResultPtr trainingResult; multi_class_classifier::prediction::ResultPtr predictionResult; -kernel_function::KernelIfacePtr kernel(new kernel_function::linear::Batch<>()); -void trainModel(); -void testModel(); -void printResults(); +void trainModel(const kernel_function::KernelIfacePtr & kernel); +void testModel(const kernel_function::KernelIfacePtr & kernel); +void printResults(const std::string & header); int main(int argc, char* argv[]) { checkArguments(argc, @@ -63,18 +58,26 @@ int main(int argc, char* argv[]) { &testDatasetFileName, &testDatasetLabelFileName); - training->parameter.kernel = kernel; - prediction->parameter.kernel = kernel; + /* Create kernel functions using the factory */ + kernel_function::KernelIfacePtr linearKernel = + kernel_function::createKernelFunction(kernel_function::linearKernel); + kernel_function::KernelIfacePtr rbfKernel = + kernel_function::createKernelFunction(kernel_function::rbfKernel); + + /* Train and test with Linear kernel */ + trainModel(linearKernel); + testModel(linearKernel); + printResults("Linear kernel"); - trainModel(); - testModel(); - printResults(); + /* Train and test with RBF kernel */ + trainModel(rbfKernel); + testModel(rbfKernel); + printResults("RBF kernel"); return 0; } -void trainModel() { - /* Create Numeric Tables for training data and dependent variables */ +void trainModel(const kernel_function::KernelIfacePtr & kernel) { /* Initialize FileDataSource to retrieve the input data * from a .csv file */ FileDataSource trainDataSource(trainDatasetFileName, @@ -87,6 +90,13 @@ void trainModel() { trainDataSource.loadDataBlock(); trainLabelSource.loadDataBlock(); + services::SharedPtr > training( + new svm::training::Batch()); + services::SharedPtr > prediction(new svm::prediction::Batch<>()); + + training->parameter.kernel = kernel; + prediction->parameter.kernel = kernel; + /* Create an algorithm object to train the multi-class SVM model */ multi_class_classifier::training::Batch<> algorithm(nClasses); @@ -104,7 +114,7 @@ void trainModel() { trainingResult = algorithm.getResult(); } -void testModel() { +void testModel(const kernel_function::KernelIfacePtr & kernel) { /* Initialize FileDataSource to retrieve the test data from * a .csv file */ FileDataSource testDataSource(testDatasetFileName, @@ -113,6 +123,13 @@ void testModel() { testDataSource.loadDataBlock(); + services::SharedPtr > training( + new svm::training::Batch()); + services::SharedPtr > prediction(new svm::prediction::Batch<>()); + + training->parameter.kernel = kernel; + prediction->parameter.kernel = kernel; + /* Create an algorithm object to predict multi-class SVM values */ multi_class_classifier::prediction::Batch<> algorithm(nClasses); @@ -131,17 +148,20 @@ void testModel() { predictionResult = algorithm.getResult(); } -void printResults() { +void printResults(const std::string & header) { FileDataSource testLabelSource(testDatasetLabelFileName, DataSource::doAllocateNumericTable, DataSource::doDictionaryFromContext); testLabelSource.loadDataBlock(); + const std::string message = + "Multi-class SVM classification with " + header + " (first 20 observations):"; + printNumericTables( testLabelSource.getNumericTable(), predictionResult->get(multi_class_classifier::prediction::prediction), "Ground truth", "Classification results", - "Multi-class SVM classification sample program results (first 20 observations):", + message, 20); } diff --git a/examples/daal/cpp/source/svm/svm_two_class_boser_csr_batch.cpp b/examples/daal/cpp/source/svm/svm_two_class_boser_csr_batch.cpp index f94cc857d96..f521851c23a 100644 --- a/examples/daal/cpp/source/svm/svm_two_class_boser_csr_batch.cpp +++ b/examples/daal/cpp/source/svm/svm_two_class_boser_csr_batch.cpp @@ -43,7 +43,7 @@ const std::string testLabelsFileName = "data/svm_two_class_test_sparse_labels.cs /* Parameters for the SVM kernel function */ kernel_function::KernelIfacePtr kernel( - new kernel_function::linear::Batch()); + kernel_function::createKernelFunction(kernel_function::linearKernel)); /* Model object for the SVM algorithm */ svm::training::ResultPtr trainingResult; diff --git a/examples/daal/cpp/source/svm/svm_two_class_boser_dense_batch.cpp b/examples/daal/cpp/source/svm/svm_two_class_boser_dense_batch.cpp index 2fdfaa37e20..0696adcdab6 100644 --- a/examples/daal/cpp/source/svm/svm_two_class_boser_dense_batch.cpp +++ b/examples/daal/cpp/source/svm/svm_two_class_boser_dense_batch.cpp @@ -41,7 +41,8 @@ const std::string testDatasetFileName = "data/svm_two_class_test_dense_data.csv" const std::string testDatasetLabelFileName = "data/svm_two_class_test_dense_label.csv"; /* Parameters for the SVM kernel function */ -kernel_function::KernelIfacePtr kernel(new kernel_function::linear::Batch<>()); +kernel_function::KernelIfacePtr kernel( + kernel_function::createKernelFunction(kernel_function::linearKernel)); /* Model object for the SVM algorithm */ svm::training::ResultPtr trainingResult; diff --git a/examples/daal/cpp/source/svm/svm_two_class_model_builder.cpp b/examples/daal/cpp/source/svm/svm_two_class_model_builder.cpp index d14f4294740..80d74bf9366 100644 --- a/examples/daal/cpp/source/svm/svm_two_class_model_builder.cpp +++ b/examples/daal/cpp/source/svm/svm_two_class_model_builder.cpp @@ -43,7 +43,8 @@ const size_t nFeatures = 20; const float bias = -0.562F; /* Parameters for the SVM kernel function */ -kernel_function::KernelIfacePtr kernel(new kernel_function::linear::Batch<>()); +kernel_function::KernelIfacePtr kernel( + kernel_function::createKernelFunction(kernel_function::linearKernel)); void testModel(svm::ModelPtr &); svm::ModelPtr buildModelFromTraining(); diff --git a/examples/daal/cpp/source/svm/svm_two_class_thunder_csr_batch.cpp b/examples/daal/cpp/source/svm/svm_two_class_thunder_csr_batch.cpp index f508a091209..89741a99f3c 100644 --- a/examples/daal/cpp/source/svm/svm_two_class_thunder_csr_batch.cpp +++ b/examples/daal/cpp/source/svm/svm_two_class_thunder_csr_batch.cpp @@ -43,7 +43,7 @@ const std::string testLabelsFileName = "data/svm_two_class_test_sparse_labels.cs /* Parameters for the SVM kernel function */ kernel_function::KernelIfacePtr kernel( - new kernel_function::linear::Batch()); + kernel_function::createKernelFunction(kernel_function::linearKernel)); /* Model object for the SVM algorithm */ svm::training::ResultPtr trainingResult; diff --git a/examples/daal/cpp/source/svm/svm_two_class_thunder_dense_batch.cpp b/examples/daal/cpp/source/svm/svm_two_class_thunder_dense_batch.cpp index c975b82a190..680af336fcf 100644 --- a/examples/daal/cpp/source/svm/svm_two_class_thunder_dense_batch.cpp +++ b/examples/daal/cpp/source/svm/svm_two_class_thunder_dense_batch.cpp @@ -18,7 +18,7 @@ /* ! Content: ! C++ example of two-class support vector machine (SVM) classification using -! the Thunder method +! the Thunder method with different kernel functions (Linear and RBF) ! !******************************************************************************/ @@ -40,16 +40,13 @@ const std::string trainDatasetLabelFileName = "data/svm_two_class_train_dense_la const std::string testDatasetFileName = "data/svm_two_class_test_dense_data.csv"; const std::string testDatasetLabelFileName = "data/svm_two_class_test_dense_label.csv"; -/* Parameters for the SVM kernel function */ -kernel_function::KernelIfacePtr kernel(new kernel_function::linear::Batch<>()); - /* Model object for the SVM algorithm */ svm::training::ResultPtr trainingResult; classifier::prediction::ResultPtr predictionResult; -void trainModel(); -void testModel(); -void printResults(); +void trainModel(const kernel_function::KernelIfacePtr & kernel); +void testModel(const kernel_function::KernelIfacePtr & kernel); +void printResults(const std::string & header); int main(int argc, char* argv[]) { checkArguments(argc, @@ -60,15 +57,26 @@ int main(int argc, char* argv[]) { &testDatasetFileName, &testDatasetLabelFileName); - trainModel(); - testModel(); - printResults(); + /* Create kernel functions using the factory */ + kernel_function::KernelIfacePtr linearKernel = + kernel_function::createKernelFunction(kernel_function::linearKernel); + kernel_function::KernelIfacePtr rbfKernel = + kernel_function::createKernelFunction(kernel_function::rbfKernel); + + /* Train and test with Linear kernel */ + trainModel(linearKernel); + testModel(linearKernel); + printResults("Linear kernel"); + + /* Train and test with RBF kernel */ + trainModel(rbfKernel); + testModel(rbfKernel); + printResults("RBF kernel"); return 0; } -void trainModel() { - /* Create Numeric Tables for training data and dependent variables */ +void trainModel(const kernel_function::KernelIfacePtr & kernel) { /* Initialize FileDataSource to retrieve the input data * from a .csv file */ FileDataSource trainDataSource(trainDatasetFileName, @@ -97,7 +105,7 @@ void trainModel() { trainingResult = algorithm.getResult(); } -void testModel() { +void testModel(const kernel_function::KernelIfacePtr & kernel) { /* Initialize FileDataSource to retrieve the test data from * a .csv file */ FileDataSource testDataSource(testDatasetFileName, @@ -123,16 +131,19 @@ void testModel() { predictionResult = algorithm.getResult(); } -void printResults() { +void printResults(const std::string & header) { FileDataSource testLabelSource(testDatasetLabelFileName, DataSource::doAllocateNumericTable, DataSource::doDictionaryFromContext); testLabelSource.loadDataBlock(); + const std::string message = + "SVM classification with " + header + " (first 20 observations):"; + printNumericTables(testLabelSource.getNumericTable(), predictionResult->get(classifier::prediction::prediction), "Ground truth\t", "Classification results", - "SVM classification results (first 20 observations):", + message, 20); }