Skip to content
Snippets Groups Projects
guide.rst 24.56 KiB

Machines

Machines are one of the core components of |project|. They represent statistical models or other functions defined by parameters that can be learnt or set by using :doc:`TutorialsTrainer`. Two examples of machines are multi-layer perceptrons (MLPs) and Gaussian mixture models (GMMs). The operation you normally expect from a machine is to be able to feed a feature vector and extract the machine response or output for that input vector. It works, in many ways, similarly to signal processing blocks. Different types of machines will give you a different type of output. In this tutorial we examine a few of the machines available in |project| and how to make use of them. Let's start with the simplest of the machines: a :py:class:`bob.machine.LinearMachine`.

Linear machine

This machine executes the simple operation y = \mathbf{W} x, where y is the output vector, x is the input vector and W is a matrix (2D array) stored in the machine. The input vector x should be composed of double-precision floating-point elements. The output will also be in double-precision. Here is how to use a :py:class:`bob.machine.LinearMachine`:

>>> W = numpy.array([[0.5, 0.5], [1.0, 1.0]], 'float64')
>>> W
array([[ 0.5,  0.5],
       [ 1. ,  1. ]])
>>> machine = bob.machine.LinearMachine(W)
>>> machine.shape
(2, 2)
>>> x = numpy.array([0.3, 0.4], 'float64')
>>> y = machine(x)
>>> y
array([ 0.55,  0.55])

As was shown in the above example, the way to pass data through a machine is to call its () operator.

The first thing to notice about machines is that they can be stored and retrieved in HDF5 files (for more details in manipulating HDF5 files, please consult :doc:`TutorialsIO`). To save the before metioned machine to a file, just use the machine's save command. Because several machines can be stored on the same :py:class:`bob.io.HDF5File`, we let the user open the file and set it up before the machine can write to it:

>>> myh5_file = bob.io.HDF5File('linear.hdf5', 'w')
>>> #do other operations on myh5_file to set it up, optionally
>>> machine.save(myh5_file)
>>> del myh5_file #close

You can load the machine again in a similar way: