SmartEngine  1.6.0
GradientDescentTrainer.h
1 // Copyright (C) Entropy Software LLC - All Rights Reserved
2 
3 #pragma once
4 
5 #include "Graph.h"
6 #include "Loss.h"
7 #include "LossTrainer.h"
8 
9 namespace SmartEngine
10 {
11 class IGradientDescentTrainer;
12 
13 #pragma pack(push, 4)
14 struct GradientDescentTrainerCInfo : public LossTrainerCInfo
18 {
22  ILoss* loss = nullptr;
23 };
24 
35 {
39  float L1Weights = 0.0f;
40 
44  float L1Biases = 0.0f;
45 
49  float L2Weights = 1e-6f;
50 
54  float L2Biases = 1e-6f;
55 };
56 
58 {
62  Adam,
63 
67  AdaMax
68 };
69 
74 {
78  GradientDescentTrainingAlgorithm algorithm = GradientDescentTrainingAlgorithm::Adam;
79 
84 
89  float clipGradients = 0.0f;
90 
94  float learnRate = 0.001f;
95 
99  float beta1 = 0.9f;
100 
104  float beta2 = 0.999f;
105 
109  float epsilon = 1e-8f;
110 };
111 #pragma pack(pop)
112 
118 class SMARTENGINE_EXPORT IGradientDescentTrainer : public ILossTrainer
119 {
120 public:
121  SMARTENGINE_DECLARE_CLASS(IGradientDescentTrainer)
122 
123 
124  virtual void SetTrainingInfo(const GradientDescentTrainingInfo& info) = 0;
128 
133  virtual void Train(int generationCount) = 0;
134 
138  virtual void Reset() = 0;
139 };
140 
145 
147 extern "C"
148 {
149  SMARTENGINE_EXPORT ObjPtr GradientDescentTrainer_CreateInstance(const GradientDescentTrainerCInfo& cinfo);
150  SMARTENGINE_EXPORT void GradientDescentTrainer_SetTrainingInfo(ObjPtr object, const GradientDescentTrainingInfo& info);
151  SMARTENGINE_EXPORT void GradientDescentTrainer_Train(ObjPtr object, int generationCount);
152  SMARTENGINE_EXPORT void GradientDescentTrainer_Reset(ObjPtr object);
153 }
155 
156 } // namespace SmartEngine
SmartEngine::RegularizationLossInfo::L2Weights
float L2Weights
L2 loss multiplier applied to weights.
Definition: GradientDescentTrainer.h:49
SmartEngine::CreateGradientDescentTrainer
SMARTENGINE_EXPORT ObjectPtr< IGradientDescentTrainer > CreateGradientDescentTrainer(const GradientDescentTrainerCInfo &cinfo)
Creates an instance of IGradientDescentTrainer
SmartEngine::GradientDescentTrainingInfo::algorithm
GradientDescentTrainingAlgorithm algorithm
The training algorithm to use
Definition: GradientDescentTrainer.h:78
SmartEngine::IGradientDescentTrainer
Trains a set of networks using gradient descent. Training with GradientDescentTrainer requires a Loss...
Definition: GradientDescentTrainer.h:119
SmartEngine::RegularizationLossInfo
Regularization tries to keep the weight values from exploding during gradient descent by adding L1 an...
Definition: GradientDescentTrainer.h:35
SmartEngine::IGradientDescentTrainer::Reset
virtual void Reset()=0
Resets the internal state of the trainer.
SmartEngine::GradientDescentTrainingAlgorithm
GradientDescentTrainingAlgorithm
Definition: GradientDescentTrainer.h:58
SmartEngine::GradientDescentTrainingInfo::learnRate
float learnRate
ADAM optimizer learn rate
Definition: GradientDescentTrainer.h:94
SmartEngine::IGradientDescentTrainer::Train
virtual void Train(int generationCount)=0
Synchronously trains for the specified number of iterations
SmartEngine::GradientDescentTrainerCInfo
Data used to construct an IGradientDescentTrainer instance
Definition: GradientDescentTrainer.h:18
SmartEngine::ObjectPtr
Smart pointer to an IObject. Automatic ref counting.
Definition: ObjectPtr.h:16
SmartEngine::RegularizationLossInfo::L1Biases
float L1Biases
L1 loss multiplier applied to biases.
Definition: GradientDescentTrainer.h:44
SmartEngine::GradientDescentTrainingInfo
GradientDescentTrainer training info
Definition: GradientDescentTrainer.h:74
SmartEngine
Definition: A2CTrainer.h:10
SmartEngine::ILossTrainer
Base class for NeuralNetwork loss trainers
Definition: LossTrainer.h:81
SmartEngine::GradientDescentTrainingInfo::clipGradients
float clipGradients
If this value is greater than 0, the gradients will be clipped to the range [-ClipGradients,...
Definition: GradientDescentTrainer.h:89
SmartEngine::GradientDescentTrainingInfo::epsilon
float epsilon
ADAM optimizer epsilon value
Definition: GradientDescentTrainer.h:109
SmartEngine::GradientDescentTrainingInfo::beta1
float beta1
ADAM optimizer beta1 value
Definition: GradientDescentTrainer.h:99
SmartEngine::GradientDescentTrainerCInfo::loss
ILoss * loss
The loss of the network output to train against.
Definition: GradientDescentTrainer.h:22
SmartEngine::GradientDescentTrainingInfo::beta2
float beta2
ADAM optimizer beta2 value
Definition: GradientDescentTrainer.h:104
SmartEngine::ILoss
The loss of a NeuralNetwork is computed using the formula (Expected Ouput - Actual Output)^2 The mean...
Definition: Loss.h:39
SmartEngine::RegularizationLossInfo::L1Weights
float L1Weights
L1 loss multiplier applied to weights.
Definition: GradientDescentTrainer.h:39
SmartEngine::RegularizationLossInfo::L2Biases
float L2Biases
L2 loss multiplier applied to biases.
Definition: GradientDescentTrainer.h:54
SmartEngine::GradientDescentTrainingInfo::regularizationLoss
RegularizationLossInfo regularizationLoss
The regularization loss parameters to apply.
Definition: GradientDescentTrainer.h:83
SmartEngine::GradientDescentTrainingAlgorithm::Adam
@ Adam
Good default choice