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