svc.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "svm_model.h"
20 #include "svm_parameter.h"
21 
22 #include <cuml/common/logger.hpp>
23 
24 #include <raft/core/handle.hpp>
25 #include <raft/distance/distance_types.hpp>
26 
27 // namespace raft {
28 // class handle_t;
29 // }
30 
31 namespace ML {
32 namespace SVM {
33 
34 // Forward declarations of the stateless API
55 template <typename math_t>
56 void svcFit(const raft::handle_t& handle,
57  math_t* input,
58  int n_rows,
59  int n_cols,
60  math_t* labels,
61  const SvmParameter& param,
62  raft::distance::kernels::KernelParams& kernel_params,
63  SvmModel<math_t>& model,
64  const math_t* sample_weight);
65 
88 template <typename math_t>
89 void svcFitSparse(const raft::handle_t& handle,
90  int* indptr,
91  int* indices,
92  math_t* data,
93  int n_rows,
94  int n_cols,
95  int nnz,
96  math_t* labels,
97  const SvmParameter& param,
98  raft::distance::kernels::KernelParams& kernel_params,
99  SvmModel<math_t>& model,
100  const math_t* sample_weight);
101 
131 template <typename math_t>
132 void svcPredict(const raft::handle_t& handle,
133  math_t* input,
134  int n_rows,
135  int n_cols,
136  raft::distance::kernels::KernelParams& kernel_params,
137  const SvmModel<math_t>& model,
138  math_t* preds,
139  math_t buffer_size,
140  bool predict_class);
141 
173 template <typename math_t>
174 void svcPredictSparse(const raft::handle_t& handle,
175  int* indptr,
176  int* indices,
177  math_t* data,
178  int n_rows,
179  int n_cols,
180  int nnz,
181  raft::distance::kernels::KernelParams& kernel_params,
182  const SvmModel<math_t>& model,
183  math_t* preds,
184  math_t buffer_size,
185  bool predict_class);
186 
193 template <typename math_t>
194 void svmFreeBuffers(const raft::handle_t& handle, SvmModel<math_t>& m);
195 
215 template <typename math_t>
216 class SVC {
217  public:
218  // Public members for easier access during testing from Python.
219 
220  raft::distance::kernels::KernelParams kernel_params;
234  SVC(raft::handle_t& handle,
235  math_t C = 1,
236  math_t tol = 1.0e-3,
237  raft::distance::kernels::KernelParams kernel_params =
238  raft::distance::kernels::KernelParams{raft::distance::kernels::LINEAR, 3, 1, 0},
239  math_t cache_size = 200,
240  int max_iter = -1,
241  int nochange_steps = 1000,
242  int verbosity = CUML_LEVEL_INFO);
243 
244  ~SVC();
245 
258  void fit(
259  math_t* input, int n_rows, int n_cols, math_t* labels, const math_t* sample_weight = nullptr);
260 
270  void predict(math_t* input, int n_rows, int n_cols, math_t* preds);
271 
281  void decisionFunction(math_t* input, int n_rows, int n_cols, math_t* preds);
282 
283  private:
284  const raft::handle_t& handle;
285 };
286 
287 }; // end namespace SVM
288 }; // end namespace ML
C-Support Vector Classification.
Definition: svc.hpp:216
SvmModel< math_t > model
Definition: svc.hpp:222
raft::distance::kernels::KernelParams kernel_params
Definition: svc.hpp:220
SvmParameter param
Definition: svc.hpp:221
void fit(math_t *input, int n_rows, int n_cols, math_t *labels, const math_t *sample_weight=nullptr)
Fit a support vector classifier to the training data.
void decisionFunction(math_t *input, int n_rows, int n_cols, math_t *preds)
Calculate decision function value for samples in input.
void predict(math_t *input, int n_rows, int n_cols, math_t *preds)
Predict classes for samples in input.
SVC(raft::handle_t &handle, math_t C=1, math_t tol=1.0e-3, raft::distance::kernels::KernelParams kernel_params=raft::distance::kernels::KernelParams{raft::distance::kernels::LINEAR, 3, 1, 0}, math_t cache_size=200, int max_iter=-1, int nochange_steps=1000, int verbosity=CUML_LEVEL_INFO)
Constructs a support vector classifier.
#define CUML_LEVEL_INFO
Definition: log_levels.hpp:28
void buffer_size(int n, int batch_size, int frequency, int *start_leveltrend_len, int *start_season_len, int *components_len, int *error_len, int *leveltrend_coef_shift, int *season_coef_shift)
void svcFitSparse(const raft::handle_t &handle, int *indptr, int *indices, math_t *data, int n_rows, int n_cols, int nnz, math_t *labels, const SvmParameter &param, raft::distance::kernels::KernelParams &kernel_params, SvmModel< math_t > &model, const math_t *sample_weight)
Fit a support vector classifier to the training data.
void svcFit(const raft::handle_t &handle, math_t *input, int n_rows, int n_cols, math_t *labels, const SvmParameter &param, raft::distance::kernels::KernelParams &kernel_params, SvmModel< math_t > &model, const math_t *sample_weight)
Fit a support vector classifier to the training data.
void svcPredict(const raft::handle_t &handle, math_t *input, int n_rows, int n_cols, raft::distance::kernels::KernelParams &kernel_params, const SvmModel< math_t > &model, math_t *preds, math_t buffer_size, bool predict_class)
Predict classes or decision function value for samples in input.
void svcPredictSparse(const raft::handle_t &handle, int *indptr, int *indices, math_t *data, int n_rows, int n_cols, int nnz, raft::distance::kernels::KernelParams &kernel_params, const SvmModel< math_t > &model, math_t *preds, math_t buffer_size, bool predict_class)
Predict classes or decision function value for samples in input.
void svmFreeBuffers(const raft::handle_t &handle, SvmModel< math_t > &m)
Definition: dbscan.hpp:30
Definition: svm_model.h:35
Definition: svm_parameter.h:34
@ LINEAR
Definition: svm_api.h:24