ITK  6.0.0
Insight Toolkit
itkGradientDescentOptimizer.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkGradientDescentOptimizer_h
19#define itkGradientDescentOptimizer_h
20
21#include "itkIntTypes.h"
23#include "ITKOptimizersExport.h"
24#include <string>
25namespace itk
26{
32{
33public:
38 {
39 MaximumNumberOfIterations,
40 MetricError
41 };
42};
43// Define how to print enumeration
44extern ITKOptimizers_EXPORT std::ostream &
46
73{
74public:
75 ITK_DISALLOW_COPY_AND_MOVE(GradientDescentOptimizer);
76
82
84 itkNewMacro(Self);
85
87 itkOverrideGetNameOfClassMacro(GradientDescentOptimizer);
88
91#if !defined(ITK_LEGACY_REMOVE)
92 // We need to expose the enum values at the class level
93 // for backwards compatibility
94 static constexpr StopConditionGradientDescentOptimizerEnum MaximumNumberOfIterations =
95 StopConditionGradientDescentOptimizerEnum::MaximumNumberOfIterations;
96 static constexpr StopConditionGradientDescentOptimizerEnum MetricError =
97 StopConditionGradientDescentOptimizerEnum::MetricError;
98#endif
99
101 itkGetConstReferenceMacro(Maximize, bool);
102 itkSetMacro(Maximize, bool);
103 itkBooleanMacro(Maximize);
104 bool
106 {
107 return !m_Maximize;
108 }
109 void
111 {
112 this->SetMaximize(!v);
113 }
114 void
116 {
117 this->MaximizeOff();
118 }
119 void
121 {
122 this->MaximizeOn();
123 }
127 virtual void
129
131 void
133
136 void
138
141 void
143
145 itkSetMacro(LearningRate, double);
146
148 itkGetConstReferenceMacro(LearningRate, double);
149
151 itkSetMacro(NumberOfIterations, SizeValueType);
152
154 itkGetConstReferenceMacro(NumberOfIterations, SizeValueType);
155
157 itkGetConstMacro(CurrentIteration, SizeValueType);
158
160 itkGetConstReferenceMacro(Value, double);
161
164 const std::string
169 itkGetConstReferenceMacro(Gradient, DerivativeType);
170
171protected:
173 ~GradientDescentOptimizer() override = default;
174 void
175 PrintSelf(std::ostream & os, Indent indent) const override;
176
177 // made protected so subclass can access
178 DerivativeType m_Gradient{};
179
180 bool m_Maximize{ false };
181
182 double m_LearningRate{ 1.0 };
183
184private:
185 bool m_Stop{ false };
186 double m_Value{ 0.0 };
188 StopConditionGradientDescentOptimizerEnum::MaximumNumberOfIterations
189 };
190 SizeValueType m_NumberOfIterations{ 100 };
191 SizeValueType m_CurrentIteration{ 0 };
192 std::ostringstream m_StopConditionDescription{};
193};
194
195// Define how to print enumeration
196extern ITKOptimizers_EXPORT std::ostream &
198
199} // end namespace itk
200
201#endif
Array class with size defined at construction time.
Definition: itkArray.h:48
Contains all enum classes in the GradientDescentOptimizer class.
Implement a gradient descent optimizer.
void StartOptimization() override
void PrintSelf(std::ostream &os, Indent indent) const override
const std::string GetStopConditionDescription() const override
~GradientDescentOptimizer() override=default
GradientDescentOptimizerEnums::StopConditionGradientDescentOptimizer StopConditionGradientDescentOptimizerEnum
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
This class is a base for the Optimization methods that optimize a single valued function.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
unsigned long SizeValueType
Definition: itkIntTypes.h:86