CorTeX Core ABI 7
ToolTech Software · C++ library reference
Loading...
Searching...
No Matches
ctxCortexTrainer.h
Go to the documentation of this file.
1//*****************************************************************************
2// File : ctxCortexTrainier.h
3// Description : The function that trains and evaluates the brain
4// Author : Anders Modén
5// Product : CorTeX 2.0.1
6//
7// Copyright © 2015- Anders Modén, Sweden
8//
9// NOTE: CorTeX is a software development kit (SDK)
10// for neural networks and genetic programming.
11// Licensed by Anders Modén.
12//
13//
14// Revision History...
15//
16// Who Date Description
17//
18// AMO 261115 Set DropConnect probability between prepared batches (2.0.2)
19// AMO 261023 Retain completed Genetic state in the whole-session facade (2.0.2)
20// AMO 261021 Expose explicit training-prefix epoch orders (2.0.2)
21// AMO 261016 Distinguish preparation rejection from runtime mutation (2.0.2)
22// AMO 261016 Reject binding a trainer session to another cortex (2.0.2)
23// AMO 261016 Use canonical training operations without compatibility aliases (2.0.2)
24// AMO 261015 Expose explicit weighted training epochs (2.0.2)
25// AMO 150708 Created file (1.0.0)
26//
27//******************************************************************************
28#ifndef __CTX_CORTEX_TRAINER_H__
29#define __CTX_CORTEX_TRAINER_H__
30
31#include "ctxCortex.h"
32#include "ctxSolver.h"
33
34#define PROCS 100 // Number of max workers
35
36class ctxBackpropStatistics;
37class ctxIndex;
38
57
58class ctxOptimizer : public gzReference
59{
60public:
61
62 GZ_DECLARE_TYPE_INTERFACE_EXPORT(CTX_EXPORT);
63
65 gzFloat rate = 1.0f, gzFloat lambda = CTX_GRADIENT_LAMBDA_DEFAULT,
66 gzFloat my = CTX_GRADIENT_MY_DEFAULT, gzFloat phi = CTX_GRADIENT_PHI_DEFAULT);
67
68 CTX_EXPORT virtual gzBool validate(gzString& error) const;
70 CTX_EXPORT gzFloat getRate() const;
71 CTX_EXPORT gzFloat getLambda() const;
72 CTX_EXPORT gzFloat getMy() const;
73 CTX_EXPORT gzFloat getPhi() const;
74
75protected:
76
78 gzFloat m_rate;
79 gzFloat m_lambda;
80 gzFloat m_my;
81 gzFloat m_phi;
82};
83
84GZ_DECLARE_REFPTR(ctxOptimizer);
85
86class ctxAdam : public ctxOptimizer
87{
88public:
89
90 GZ_DECLARE_TYPE_INTERFACE_EXPORT(CTX_EXPORT);
91
92 CTX_EXPORT ctxAdam(gzFloat rate = 1.0f,
93 gzFloat beta1 = CTX_GRADIENT_MY_DEFAULT,
94 gzFloat beta2 = CTX_GRADIENT_PHI_DEFAULT,
95 gzFloat lambda = CTX_GRADIENT_LAMBDA_DEFAULT);
96};
97
98GZ_DECLARE_REFPTR(ctxAdam);
99
105
127
133
144
145class ctxTrainer : public gzReference
146{
147public:
148
149 GZ_DECLARE_TYPE_INTERFACE_EXPORT(CTX_EXPORT);
150
152
156 CTX_EXPORT virtual gzBool install(ctxCortex* cortex);
157 CTX_EXPORT virtual gzBool prepare() = 0;
159 CTX_EXPORT virtual gzBool trainStep() = 0;
160 CTX_EXPORT virtual gzBool isPrepared() const = 0;
161 CTX_EXPORT virtual gzBool isComplete() const = 0;
162 CTX_EXPORT virtual gzBool saveCheckpoint(const gzString& url) const = 0;
163 CTX_EXPORT virtual gzBool loadCheckpoint(const gzString& url) = 0;
164 CTX_EXPORT virtual gzBool train();
166 CTX_EXPORT virtual gzUInt32 getCapabilities() const = 0;
168 CTX_EXPORT gzString getError() const;
169
170protected:
171
172 ctxCortexPtr m_cortex;
173 gzString m_error;
174};
175
176GZ_DECLARE_REFPTR(ctxTrainer);
177
179{
180public:
181
182 GZ_DECLARE_TYPE_INTERFACE_EXPORT(CTX_EXPORT);
183
186
196 CTX_EXPORT gzBool setDropConnect(gzFloat probability);
197 CTX_EXPORT gzFloat getDropConnect() const;
201 CTX_EXPORT gzBool isPrepared() const;
202 CTX_EXPORT gzBool isComplete() const;
205 CTX_EXPORT gzUInt32 getCapabilities() const;
206 CTX_EXPORT gzBool beginEpoch(gzUInt32 epoch);
216 CTX_EXPORT gzBool beginEpoch(gzUInt32 epoch, const gzArray<gzFloat>& sampleWeights,
217 gzUInt32 seed = 0);
225 CTX_EXPORT gzBool beginIndexedEpoch(gzUInt32 epoch, const ctxIndex& sampleOrder);
230 CTX_EXPORT gzBool saveCheckpoint(const gzString& url) const;
231 CTX_EXPORT gzBool loadCheckpoint(const gzString& url);
232
233 CTX_EXPORT gzUInt32 getSamples() const;
234 CTX_EXPORT gzUInt32 getValidations() const;
235 CTX_EXPORT gzUInt32 getBatchSize() const;
236 CTX_EXPORT gzUInt32 getSubsetLength() const;
237 CTX_EXPORT gzUInt32 getBatchOffset() const;
238 CTX_EXPORT gzUInt32 getSampleIndex(gzUInt32 page) const;
239 CTX_EXPORT gzUInt32 getIteration() const;
240
241private:
242
243 friend class ctxModel;
244 gzBool prepareInternal(gzBool& runtimeMayChange);
245
246 ctxBackpropTrainer(const ctxBackpropTrainer&) = delete;
247 ctxBackpropTrainer& operator=(const ctxBackpropTrainer&) = delete;
248
250 ctxOptimizerPtr m_optimizer;
251 gzArray<gzUInt32> m_randomIndices;
252 gzArray<gzUInt32> m_batchSamples;
253 gzUInt32 m_samples;
254 gzUInt32 m_validations;
255 gzUInt32 m_batchSize;
256 gzUInt32 m_subsetLength;
257 gzUInt32 m_nextBatchOffset;
258 gzUInt32 m_batchOffset;
259 gzUInt32 m_iteration;
260 gzUInt32 m_epoch;
261 gzBool m_prepared;
262 gzBool m_epochReady;
263 gzBool m_batchReady;
264 ctxBackpropStatistics* m_statistics;
265};
266
267GZ_DECLARE_REFPTR(ctxBackpropTrainer);
268
270{
271public:
272
283
284 GZ_DECLARE_TYPE_INTERFACE_EXPORT(CTX_EXPORT);
285
287
291 CTX_EXPORT gzBool isPrepared() const;
294 CTX_EXPORT gzUInt32 getCapabilities() const;
301 CTX_EXPORT gzBool saveCheckpoint(const gzString& url) const;
302 CTX_EXPORT gzBool loadCheckpoint(const gzString& url);
303
305 CTX_EXPORT gzBool isComplete() const;
306 CTX_EXPORT gzUInt32 getGeneration() const;
307 CTX_EXPORT gzUInt32 getCandidate() const;
308 CTX_EXPORT gzUInt32 getFitnessBatch() const;
311 CTX_EXPORT gzFloat getBestError() const;
312
313private:
314
315 friend class ctxCortexTrainer;
316
317 gzBool fail(const gzString& error);
318 gzBool initializePopulation();
319 gzBool evaluatePreparedCandidate(gzFloat& errorSum);
320 gzBool rebasePopulation();
321 gzBool updatePopulation(ctxCortexParameters* parameters, gzUInt32 method);
322 gzUInt32 nextRandom();
323 gzFloat nextRandomFloat();
324 gzFloat nextRandomNormal(gzFloat mean, gzFloat standardDeviation);
325 gzVoid randomParameters(gzBool add);
326 gzVoid mutateParameters(gzBool add);
327 gzVoid mutateBreeding();
328 gzVoid crossover(ctxCortexParameters* mate, gzUInt32 method);
329 gzVoid installBestParameters();
330 gzVoid randomizeSamples();
331 gzVoid initializeRandomPhase();
332
334 gzRefList<ctxCortexParameters> m_population;
335 gzRefList<ctxCortexParameters> m_elites;
336 gzArray<gzUInt32> m_randomIndices;
337 ctxCortexParametersPtr m_originalParameters;
338 ctxDebugFlags m_debugFlags;
339 State m_state;
340 gzUInt32 m_samples;
341 gzUInt32 m_batchSize;
342 gzUInt32 m_totalBatchSize;
343 gzUInt32 m_periods;
344 gzUInt32 m_subsetLength;
345 gzUInt32 m_period;
346 gzUInt32 m_epoch;
347 gzUInt32 m_generation;
348 gzUInt32 m_candidate;
349 gzUInt32 m_fitnessBatch;
350 gzUInt32 m_currentRandomCount;
351 gzUInt32 m_eliteCount;
352 gzUInt32 m_method;
353 gzUInt32 m_randomState;
354 gzFloat m_standardDeviation;
355 gzFloat m_candidateError;
356 gzFloat m_bestError;
357 gzFloat m_targetError;
358 gzBool m_candidateAvailable;
359 gzBool m_batchReady;
360};
361
362GZ_DECLARE_REFPTR(ctxGeneticTrainer);
363
364class ctxCortexTrainer : public gzObject,
366 public gzObjectSender,
367 public gzObjectReceiver
368{
369public:
370
371 GZ_DECLARE_TYPE_INTERFACE_EXPORT(CTX_EXPORT);
372
373 CTX_EXPORT ctxCortexTrainer(const gzString &save_url=GZ_EMPTY_STRING,const gzString & channel_url="udp::45666",const gzString& channel_name="ctx");
374
375 GZ_PROPERTY_EXPORT(ctxCortexPtr, Cortex, CTX_EXPORT);
376
383
385
387
388
389protected:
390
391 //CTX_EXPORT virtual gzVoid onEvaluate(gzFloat *parameters, gzInt32 sample_count, gzFloat *fvec, void *data, int *info);
392
393 CTX_EXPORT gzBool updateCandidates(ctxCortexParameters *parameters,gzUInt32 iteration,gzUInt32 period,gzUInt32 epoch,gzUInt32 method, gzFloat expectedMinDiff, gzBool external, gzBool *updatedPopulation=NULL);
394
395 CTX_EXPORT gzFloat calcAccumError(ctxContext *context, gzUInt32 *index, gzUInt32 accumulations, gzUInt32 batch_size, gzUInt32 subset_len);
396
397 CTX_EXPORT gzVoid virtual onObject(gzObject *object);
398
399 gzRefList<ctxCortexParameters> m_populationList;
400 gzRefList<ctxCortexParameters> m_populationEliteList;
402
403 gzFloat m_bestError;
405 gzUInt32 m_eliteCount;
406
409 gzString m_save_url;
410
411 gzString m_error;
412};
413
414GZ_DECLARE_REFPTR(ctxCortexTrainer);
415
416#endif //__CTX_CORTEX_TRAINER_H__
ctxAdam(gzFloat rate=1.0f, gzFloat beta1=CTX_GRADIENT_MY_DEFAULT, gzFloat beta2=CTX_GRADIENT_PHI_DEFAULT, gzFloat lambda=CTX_GRADIENT_LAMBDA_DEFAULT)
gzUInt32 getBatchSize() const
gzUInt32 getIteration() const
gzBool saveCheckpoint(const gzString &url) const
gzBool isComplete() const
ctxBackpropTrainer(const ctxBackpropTrainingOptions &options=ctxBackpropTrainingOptions())
gzUInt32 getSampleIndex(gzUInt32 page) const
gzUInt32 getValidations() const
gzBool prepareNextBatch()
Prepare the next selected batch, without a parameter update.
gzBool setDropConnect(gzFloat probability)
Set the training DropConnect probability for subsequent batches.
ctxTrainerProgramProfile getProgramProfile() const
gzBool isPrepared() const
gzBool loadCheckpoint(const gzString &url)
gzBool trainStep()
Prepare and train the next bounded unit; Backprop consumes one batch.
gzFloat getDropConnect() const
virtual ~ctxBackpropTrainer()
gzBool beginIndexedEpoch(gzUInt32 epoch, const ctxIndex &sampleOrder)
Install a prebuilt training-prefix order at an epoch boundary.
gzUInt32 getSamples() const
gzBool setOptimizer(ctxOptimizer *optimizer, ctxOptimizerStatePolicy statePolicy=CTX_OPTIMIZER_STATE_RESET)
gzBool trainPreparedBatch()
Train one prepared batch and commit its batch/epoch progress.
gzBool beginEpoch(gzUInt32 epoch)
gzUInt32 getSubsetLength() const
gzBool beginEpoch(gzUInt32 epoch, const gzArray< gzFloat > &sampleWeights, gzUInt32 seed=0)
Explicit weighted epoch over the prepared training prefix, excluding validations.
ctxOptimizer * getOptimizer() const
gzUInt32 getBatchOffset() const
gzUInt32 getCapabilities() const
ctxCostErrorMethod errorMethod
gzBool updateCandidates(ctxCortexParameters *parameters, gzUInt32 iteration, gzUInt32 period, gzUInt32 epoch, gzUInt32 method, gzFloat expectedMinDiff, gzBool external, gzBool *updatedPopulation=NULL)
ctxCortexTrainer(const gzString &save_url=GZ_EMPTY_STRING, const gzString &channel_url="udp::45666", const gzString &channel_name="ctx")
gzBool train(const ctxGeneticTrainingOptions &options)
Train a complete fixed-topology Genetic session and retain its final population, elite archive and fi...
gzFloat calcAccumError(ctxContext *context, gzUInt32 *index, gzUInt32 accumulations, gzUInt32 batch_size, gzUInt32 subset_len)
virtual gzVoid onObject(gzObject *object)
gzVoid resetTrainerError()
gzString getTrainerError()
gzBool train(const ctxBackpropTrainingOptions &options)
Train a complete Backprop session with explicit typed configuration.
gzRefList< ctxCortexParameters > m_populationList
gzRefList< ctxCortexParameters > m_populationEliteList
gzBool completeCandidate()
gzBool isPrepared() const
gzFloat getBestError() const
gzBool nextFitnessBatch()
gzBool stepFitnessBatch()
ctxTrainerProgramProfile getProgramProfile() const
gzUInt32 getCandidate() const
gzUInt32 getCapabilities() const
ctxGeneticTrainer(const ctxGeneticTrainingOptions &options=ctxGeneticTrainingOptions())
gzBool nextCandidate()
friend class ctxCortexTrainer
gzBool isComplete() const
gzBool loadCheckpoint(const gzString &url)
gzUInt32 getFitnessBatch() const
gzUInt32 getPopulationSize() const
gzBool saveCheckpoint(const gzString &url) const
gzBool beginGeneration()
gzUInt32 getGeneration() const
gzBool trainStep()
Advance one bounded state-machine unit of genetic training.
State getState() const
gzBool completeGeneration()
gzUInt32 getFitnessBatchCount() const
ctxCostErrorMethod errorMethod
ctxLevenbergMarquardtSolver(ctxDefSolverFunction function=NULL)
virtual gzBool validate(gzString &error) const
gzFloat getPhi() const
ctxGradientType m_gradientType
gzFloat getLambda() const
ctxOptimizer(ctxGradientType gradientType=CTX_GRADIENT_TYPE_DEFAULT, gzFloat rate=1.0f, gzFloat lambda=CTX_GRADIENT_LAMBDA_DEFAULT, gzFloat my=CTX_GRADIENT_MY_DEFAULT, gzFloat phi=CTX_GRADIENT_PHI_DEFAULT)
gzFloat getMy() const
gzFloat getRate() const
ctxGradientType getGradientType() const
virtual gzBool train()
virtual gzBool loadCheckpoint(const gzString &url)=0
gzString getError() const
virtual gzBool prepare()=0
virtual ctxTrainerProgramProfile getProgramProfile() const =0
ctxCortexPtr m_cortex
virtual gzBool saveCheckpoint(const gzString &url) const =0
virtual gzUInt32 getCapabilities() const =0
virtual gzBool isComplete() const =0
virtual gzBool isPrepared() const =0
virtual gzBool trainStep()=0
Advance one bounded unit of the installed learning strategy.
virtual gzBool install(ctxCortex *cortex)
Bind this session to one cortex.
ctxCortex * getCortex() const
#define CTX_EXPORT
Definition ctxBase.h:68
CONST_FLOAT CTX_GRADIENT_PHI_DEFAULT
CONST_FLOAT CTX_GRADIENT_MY_DEFAULT
CONST_FLOAT CTX_GRADIENT_LAMBDA_DEFAULT
ctxGradientType
@ CTX_GRADIENT_TYPE_DEFAULT
ctxTrainerProgramProfile
@ CTX_TRAINER_PROGRAM_BACKPROP
@ CTX_TRAINER_PROGRAM_EVALUATE
ctxTrainerCapability
@ CTX_TRAINER_CAPABILITY_CHECKPOINTS
@ CTX_TRAINER_CAPABILITY_GRADIENTS
@ CTX_TRAINER_CAPABILITY_DISTRIBUTED
@ CTX_TRAINER_CAPABILITY_POPULATION
@ CTX_TRAINER_CAPABILITY_REWARDS
@ CTX_TRAINER_CAPABILITY_SEQUENCES
@ CTX_TRAINER_CAPABILITY_BATCHES
ctxOptimizerStatePolicy
@ CTX_OPTIMIZER_STATE_CONTINUE
@ CTX_OPTIMIZER_STATE_RESET
LevenbergMarquardt base class for Newton Rapson on steroids.
ctxCostErrorMethod
Definition ctxTypes.h:81
ctxDebugFlags
Definition ctxTypes.h:47