svc.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, 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 
26 #include <cuvs/distance/distance.hpp>
27 #include <cuvs/distance/grammian.hpp>
28 
29 // namespace raft {
30 // class handle_t;
31 // }
32 
33 namespace ML {
34 namespace SVM {
35 
36 // Forward declarations of the stateless API
57 template <typename math_t>
58 void svcFit(const raft::handle_t& handle,
59  math_t* input,
60  int n_rows,
61  int n_cols,
62  math_t* labels,
63  const SvmParameter& param,
64  cuvs::distance::kernels::KernelParams& kernel_params,
65  SvmModel<math_t>& model,
66  const math_t* sample_weight);
67 
90 template <typename math_t>
91 void svcFitSparse(const raft::handle_t& handle,
92  int* indptr,
93  int* indices,
94  math_t* data,
95  int n_rows,
96  int n_cols,
97  int nnz,
98  math_t* labels,
99  const SvmParameter& param,
100  cuvs::distance::kernels::KernelParams& kernel_params,
101  SvmModel<math_t>& model,
102  const math_t* sample_weight);
103 
133 template <typename math_t>
134 void svcPredict(const raft::handle_t& handle,
135  math_t* input,
136  int n_rows,
137  int n_cols,
138  cuvs::distance::kernels::KernelParams& kernel_params,
139  const SvmModel<math_t>& model,
140  math_t* preds,
141  math_t buffer_size,
142  bool predict_class);
143 
175 template <typename math_t>
176 void svcPredictSparse(const raft::handle_t& handle,
177  int* indptr,
178  int* indices,
179  math_t* data,
180  int n_rows,
181  int n_cols,
182  int nnz,
183  cuvs::distance::kernels::KernelParams& kernel_params,
184  const SvmModel<math_t>& model,
185  math_t* preds,
186  math_t buffer_size,
187  bool predict_class);
188 
195 template <typename math_t>
196 void svmFreeBuffers(const raft::handle_t& handle, SvmModel<math_t>& m);
197 
217 template <typename math_t>
218 class SVC {
219  public:
220  // Public members for easier access during testing from Python.
221 
222  cuvs::distance::kernels::KernelParams kernel_params;
236  SVC(raft::handle_t& handle,
237  math_t C = 1,
238  math_t tol = 1.0e-3,
239  cuvs::distance::kernels::KernelParams kernel_params =
240  cuvs::distance::kernels::KernelParams{cuvs::distance::kernels::LINEAR, 3, 1, 0},
241  math_t cache_size = 200,
242  int max_iter = -1,
243  int nochange_steps = 1000,
244  rapids_logger::level_enum verbosity = rapids_logger::level_enum::info);
245 
246  ~SVC();
247 
260  void fit(
261  math_t* input, int n_rows, int n_cols, math_t* labels, const math_t* sample_weight = nullptr);
262 
272  void predict(math_t* input, int n_rows, int n_cols, math_t* preds);
273 
283  void decisionFunction(math_t* input, int n_rows, int n_cols, math_t* preds);
284 
285  private:
286  const raft::handle_t& handle;
287 };
288 
289 }; // end namespace SVM
290 }; // end namespace ML
C-Support Vector Classification.
Definition: svc.hpp:218
SVC(raft::handle_t &handle, math_t C=1, math_t tol=1.0e-3, cuvs::distance::kernels::KernelParams kernel_params=cuvs::distance::kernels::KernelParams{cuvs::distance::kernels::LINEAR, 3, 1, 0}, math_t cache_size=200, int max_iter=-1, int nochange_steps=1000, rapids_logger::level_enum verbosity=rapids_logger::level_enum::info)
Constructs a support vector classifier.
SvmModel< math_t > model
Definition: svc.hpp:224
cuvs::distance::kernels::KernelParams kernel_params
Definition: svc.hpp:222
SvmParameter param
Definition: svc.hpp:223
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.
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 svcPredictSparse(const raft::handle_t &handle, int *indptr, int *indices, math_t *data, int n_rows, int n_cols, int nnz, cuvs::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 svcFit(const raft::handle_t &handle, math_t *input, int n_rows, int n_cols, math_t *labels, const SvmParameter ¶m, cuvs::distance::kernels::KernelParams &kernel_params, SvmModel< math_t > &model, const math_t *sample_weight)
Fit a support vector classifier to the training data.
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 ¶m, cuvs::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, cuvs::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:36
@ LINEAR
Definition: svm_api.h:24