Getting started

After installing AIMM, its CLI and libraries become available. Since the main purpose of the server is providing an API to dynamically defined plugins, the first step is defining the plugins the server will provide an interface for. The following code shows an example of a module containing multiple plugin definitions:

from aimm import plugins
from sklearn import svm
from sklearn import datasets
import pickle


@plugins.data_access("iris_inputs")
def iris_inputs():
    return datasets.load_iris(return_X_y=True)[0]


@plugins.data_access("iris_outputs")
def iris_outputs():
    return datasets.load_iris(return_X_y=True)[1]


@plugins.model
class SVC(plugins.Model):
    def __init__(self, gamma=0.001, C=100.0):
        self._svc = svm.SVC(gamma=gamma, C=C)

    def fit(self, X, y):
        self._svc = self._svc.fit(X, y)
        return self

    def predict(self, X):
        return self._svc.predict(X)

    def serialize(self):
        return pickle.dumps(self)

    @classmethod
    def deserialize(cls, instance_bytes):
        return pickle.loads(instance_bytes)

The plugins defined are:

  • data access for Iris dataset inputs and outputs

  • model implementation that is actually a wrapper around sklearn’s SVC

For more information on the plugin interface, consult the documentation entry.

The next step is server configuration. This involves writing a YAML file containing the necessary settings. An example of such a configuration, following up to the previous example, may be:

---
name: AIMM
log:
    disable_existing_loggers: false
    formatters:
        default: {}
    handlers:
        console:
            class : logging.StreamHandler
            level   : INFO
            stream  : ext://sys.stdout
    root:
        handlers:
        - console
        level: INFO
    version: 1
engine:
    sigterm_timeout: 5
    max_children: 5
    check_children_period: 3
backend:
    module: aimm.server.backend.sqlite
    path: ./data/aimm.db
control:
  - module: aimm.server.control.repl
    server:
        host: 0.0.0.0
        port: 9999
    users:
        - username: user
          password: d74ff0ee8da3b9806b18c877dbf29bbde50b5bd8e4dad7a3a725000feb82e8f1
plugins:
    names:
        - 'plugins.sklearn_wrapper'
...

For more details on configuring and running the server, consult the documentation entry.

Running the server now starts a service that listens on the address 127.0.0.1:9999 and allows clients to connect, create, fit and use SVC models. Since the only configured control is REPL control, it should be possible to connect using the REPL client. Following snippet shows how the library may be used:

from aimm.client import repl

async def run():
    aimm = repl.AIMM()
    await aimm.connect('ws://127.0.0.1:9999/ws')
    m = await aimm.create_instance('plugins.sklearn.SVC')

    await m.fit(repl.DataAccessArg('iris_inputs'), repl.DataAccessArg('iris_outputs'))
    await m.predict(repl.DataAccessArg('iris_inputs'))

The code would connect to the server, create an SVC instance, remotely fit it with Iris data and use it to perform classification.

Example of Hat integration is available in the examples directory.