cuML C++ API  23.12
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
learning_rate.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2022, 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 <cuml/solvers/params.hpp>
20 #include <math.h>
21 
22 namespace ML {
23 namespace Solver {
24 
25 template <typename math_t>
26 math_t max(math_t a, math_t b)
27 {
28  return (a < b) ? b : a;
29  ;
30 }
31 
32 template <typename math_t>
33 math_t invScaling(math_t eta, math_t power_t, int t)
34 {
35  return (eta / pow(t, power_t));
36 }
37 
38 template <typename math_t>
39 math_t regDLoss(math_t a, math_t b)
40 {
41  return a - b;
42 }
43 
44 template <typename math_t>
45 math_t calOptimalInit(math_t alpha)
46 {
47  math_t typw = sqrt(math_t(1.0) / sqrt(alpha));
48  math_t initial_eta0 = typw / max(math_t(1.0), regDLoss(-typw, math_t(1.0)));
49  return (math_t(1.0) / (initial_eta0 * alpha));
50 }
51 
52 template <typename math_t>
53 math_t optimal(math_t alpha, math_t optimal_init, int t)
54 {
55  return math_t(1.0) / (alpha * (optimal_init + t - 1));
56 }
57 
58 template <typename math_t>
59 math_t calLearningRate(ML::lr_type lr_type, math_t eta, math_t power_t, math_t alpha, math_t t)
60 {
62  return eta;
63  } else if (lr_type == ML::lr_type::INVSCALING) {
64  return invScaling(eta, power_t, t);
65  } else if (lr_type == ML::lr_type::OPTIMAL) {
66  return optimal(alpha, eta, t);
67  } else {
68  return math_t(0);
69  }
70 }
71 
72 }; // namespace Solver
73 }; // namespace ML
74 // end namespace ML
math_t max(math_t a, math_t b)
Definition: learning_rate.h:26
math_t invScaling(math_t eta, math_t power_t, int t)
Definition: learning_rate.h:33
math_t optimal(math_t alpha, math_t optimal_init, int t)
Definition: learning_rate.h:53
math_t regDLoss(math_t a, math_t b)
Definition: learning_rate.h:39
math_t calLearningRate(ML::lr_type lr_type, math_t eta, math_t power_t, math_t alpha, math_t t)
Definition: learning_rate.h:59
math_t calOptimalInit(math_t alpha)
Definition: learning_rate.h:45
Definition: dbscan.hpp:27
lr_type
Definition: params.hpp:21
@ OPTIMAL
Definition: params.hpp:22
@ INVSCALING
Definition: params.hpp:24
@ CONSTANT
Definition: params.hpp:23