Overview:
Iterative machine enables us to write iterative algorithms that are prematurely stoppable. This means users can cancel the training process any time. The model is still usable and concurrent. This model can then be applied to test data, compared with reference weights and, if needed, can resume training from where it left off earlier. Iterative Machine framework makes using cancelled state models more robust. The idea here is to have iterative models implement only a single iteration of the main training loop instead. This will be called from a while loop in CIterativeMachine class now.
Motivation:
The previous idea was to have different callbacks like on_next
, on_pause
which will be called based on the user choice obtained from the ShogunSignalHandler
prompt. This proves a bit restrictive with respect to what the user can acutally do. Furthermore, it was not possible to define such behaivour from an interface like python. The user has to write some c++ code. Therefore, it is a better approach to allow the user to just cancel training whenever he wants and then also to resume training from where it was left off. This is more flexible to the user and developer as it removes the element of guessing what is meaningful for a user.
Implementation details:
The CIterativeMachine
is a mixin class. This means it can inherit from some other class which is passed to it through a template argument to its constructors. Iterative models will now inherit from CIterativeMachine<CMockMachine>
instead of being a direct subclass of CMockMachine
.
data members:
m_current_iteration
: The current iteration count.m_max_iteration
: Maximum number of iterations allowed.m_complete
: If the model has completed training and converged.methods:
init_model
: Virtual, must be written in subclass, this is called before training loop begins to initialize all members.continue_training
: Contains the main training loop which updatesm_current_iteration
and called theiteration
method.iteration
: Virtual, This must be written in subclass and implements a single iteration of training loop.end_training
: An optional method called after training to clean member states or giving warnings etc.
Example:
Below is a cpp example of a fake iterative model.
There are two ways to Prematurely stop an algorithm. The user can press CTRL+C
or the user can write a callback method that will trigger a signal. For more details on second method see this patch. From python the code will look like:
Applying Iterative Machine to more Algorithms:
To use the features of CIterativeMachine
with a new Algorithm we can make the following changes:
- Use existing machine members (For eg:
m_w
,bias
ofCLinearMachine
for weights and bias) instead of local member copies. If there are corresponding local members present they must be removed. This is to make sure the model updates its state every iteration. - Identify the main training loop. This is where the magic is happening.
- Everything above the loop in training process is a likely candidate for
init_model
method. - The contents of the loop represent a single iteration hence they will go into
iteration
And you are all set.
Future Work:
- Porting all iterative models to this code style is the aim here. A list of Iterative Algorithms is available here.
- Automated tests for all Iterative Machines. These will include test for correctness of a pre-trained model along with a test to make sure that the model updates its state each iteration.