linear.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2023, 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 <raft/core/handle.hpp>
20 
21 namespace ML {
22 namespace SVM {
23 
26  enum Penalty {
28  L1,
30  L2
31  };
33  enum Loss {
42  };
43 
49  bool fit_intercept = true;
53  bool penalized_intercept = false;
55  bool probability = false;
57  int max_iter = 1000;
65  int lbfgs_memory = 5;
67  int verbose = 0;
72  double C = 1.0;
74  double grad_tol = 0.0001;
76  double change_tol = 0.00001;
78  double epsilon = 0.0;
79 };
80 
81 template <typename T>
89  T* w;
91  T* classes = nullptr;
99  T* probScale = nullptr;
101  std::size_t nClasses = 0;
103  std::size_t coefRows;
104 
106  inline std::size_t coefCols() const { return nClasses <= 2 ? 1 : nClasses; }
107 
121  static LinearSVMModel<T> fit(const raft::handle_t& handle,
122  const LinearSVMParams& params,
123  const T* X,
124  const std::size_t nRows,
125  const std::size_t nCols,
126  const T* y,
127  const T* sampleWeight);
128 
138  static LinearSVMModel<T> allocate(const raft::handle_t& handle,
139  const LinearSVMParams& params,
140  const std::size_t nCols,
141  const std::size_t nClasses = 0);
142 
144  static void free(const raft::handle_t& handle, LinearSVMModel<T>& model);
145 
157  static void predict(const raft::handle_t& handle,
158  const LinearSVMParams& params,
159  const LinearSVMModel<T>& model,
160  const T* X,
161  const std::size_t nRows,
162  const std::size_t nCols,
163  T* out);
164 
176  static void decisionFunction(const raft::handle_t& handle,
177  const LinearSVMParams& params,
178  const LinearSVMModel<T>& model,
179  const T* X,
180  const std::size_t nRows,
181  const std::size_t nCols,
182  T* out);
183 
196  static void predictProba(const raft::handle_t& handle,
197  const LinearSVMParams& params,
198  const LinearSVMModel<T>& model,
199  const T* X,
200  const std::size_t nRows,
201  const std::size_t nCols,
202  const bool log,
203  T* out);
204 };
205 
206 } // namespace SVM
207 } // namespace ML
Definition: params.hpp:34
Definition: dbscan.hpp:30
penalty
Definition: params.hpp:34
Definition: linear.hpp:82
static void decisionFunction(const raft::handle_t &handle, const LinearSVMParams &params, const LinearSVMModel< T > &model, const T *X, const std::size_t nRows, const std::size_t nCols, T *out)
Calculate decision function value for samples in input.
std::size_t coefCols() const
Definition: linear.hpp:106
std::size_t nClasses
Definition: linear.hpp:101
static void predictProba(const raft::handle_t &handle, const LinearSVMParams &params, const LinearSVMModel< T > &model, const T *X, const std::size_t nRows, const std::size_t nCols, const bool log, T *out)
For SVC, predict the probabilities for each outcome.
static void predict(const raft::handle_t &handle, const LinearSVMParams &params, const LinearSVMModel< T > &model, const T *X, const std::size_t nRows, const std::size_t nCols, T *out)
Predict using the trained LinearSVM model.
static LinearSVMModel< T > fit(const raft::handle_t &handle, const LinearSVMParams &params, const T *X, const std::size_t nRows, const std::size_t nCols, const T *y, const T *sampleWeight)
Allocate and fit the LinearSVM model.
T * w
Definition: linear.hpp:89
static void free(const raft::handle_t &handle, LinearSVMModel< T > &model)
Free the allocated memory. The model is not usable after the call of this method.
static LinearSVMModel< T > allocate(const raft::handle_t &handle, const LinearSVMParams &params, const std::size_t nCols, const std::size_t nClasses=0)
Explicitly allocate the data for the model without training it.
T * probScale
Definition: linear.hpp:99
T * classes
Definition: linear.hpp:91
std::size_t coefRows
Definition: linear.hpp:103
Definition: linear.hpp:24
bool probability
Definition: linear.hpp:55
Loss
Definition: linear.hpp:33
@ EPSILON_INSENSITIVE
Definition: linear.hpp:39
@ HINGE
Definition: linear.hpp:35
@ SQUARED_HINGE
Definition: linear.hpp:37
@ SQUARED_EPSILON_INSENSITIVE
Definition: linear.hpp:41
double epsilon
Definition: linear.hpp:78
bool fit_intercept
Definition: linear.hpp:49
double change_tol
Definition: linear.hpp:76
int linesearch_max_iter
Definition: linear.hpp:61
double C
Definition: linear.hpp:72
int max_iter
Definition: linear.hpp:57
int lbfgs_memory
Definition: linear.hpp:65
int verbose
Definition: linear.hpp:67
Penalty
Definition: linear.hpp:26
@ L2
Definition: linear.hpp:30
@ L1
Definition: linear.hpp:28
bool penalized_intercept
Definition: linear.hpp:53
double grad_tol
Definition: linear.hpp:74
Loss loss
Definition: linear.hpp:47