Graphically monitoring learning algorithms

This recipe explains how yaplf can be used in order to generate a graphical output summarizing how an iterative learning algorithm evolves through time. A basic knowledge of the python programming language is required, as well as the comprehension of the basic concepts related to multilayer perceptrons and corresponding learning algorithms.

A light gray cell denotes one or more python statements, while a subsequent darw gray cell contains the expected output of the above statements. Statements can either be executed in a python or in a sage shell. For sake of visualization this document assumes that statements are executed in a sage notebook, so that graphics are shown right after the cell generating them. The execution in a pure python environment works in the same way, the only difference being that graphic functions return a matplotlib object that can be dealt with as usual.

Observing iterative learning algorithms

A significant number of learning algorithms performs an incremental inference, for it starts considering a model randomly chosen which is modified as time evolves until some convergence criterion is met. Sometimes this criterion should be checked through an external intervention; in other cases one simply wants to watch how the learning process is evolving through time. In such cases yaplf allows these special learning algorithms, tagged as subclasses of IterativeLearningAlgorithm, to be observed by an external class which eventually produces a graphic form of the observation process.

The typical form of observation is that focusing on some error metric to be minimized under a given threshold in order to declare a successful learning. This is accomplished through the ErrorTrajectory class. For instance, in the following cell the error backpropagation algorithm is used to learn the binary XOR function for a thousand iterations, and the corresponding square error averaged through all examples is plotted versus the iteration number:

from import LabeledExample
from yaplf.algorithms.neural import BackpropagationAlgorithm
from yaplf.utility import SigmoidActivationFunction
from yaplf.utility import ErrorTrajectory, FixedIterationsStoppingCriterion
xor_sample = [LabeledExample((0.1, 0.1), (0.1,)),
  LabeledExample((0.1, 0.9), (0.9,)), LabeledExample((0.9, 0.1), (0.9,)),
  LabeledExample((0.9, 0.9), (0.1,))]
alg = BackpropagationAlgorithm(xor_sample, (2, 2, 1), threshold = True,
  learning_rate = .3, momentum_term = .8, min_error = 0.1,
  activations = SigmoidActivationFunction(10))

errObs = ErrorTrajectory(alg) = FixedIterationsStoppingCriterion(1000))
errObs.get_trajectory(color='red', joined = True)

The metric used in order to measure error can be changed through the error_measure named argument, to be assigned to an instance of any sublcass of ErrorModel. For instance, the next cell performs again the previous learning task, now focusing on the maximum square error over all patterns rather than on the error average:

from yaplf.utility import MaxError
errObs = ErrorTrajectory(alg, error_measure=MaxError()) = FixedIterationsStoppingCriterion(1000))
errObs.get_trajectory(color='red', joined = True)

There are also other ways of observing a learning algorithm run: for instance, the class PerceptronWeightTrajectory outputs the trajectory described by the perceptron weights (seen as a point in 2D or 3D space) during the learning phase:

from yaplf.algorithms.neural import GradientPerceptronAlgorithm
from yaplf.utility import PerceptronWeightTrajectory
and_sample = [LabeledExample((0.1, 0.1), (0.1,)),
  LabeledExample((0.1, 0.9), (0.1,)), LabeledExample((0.9, 0.1), (0.1,)),
  LabeledExample((0.9, 0.9), (0.9,))]
alg = GradientPerceptronAlgorithm(and_sample, threshold = True,
  weight_bound = 0.1, beta = 0.8)
weightObs = PerceptronWeightTrajectory(alg) = FixedIterationsStoppingCriterion(5000),
  batch = False, learning_rate = .1)
weightObs.get_trajectory(joined = True)