chariots.keras

module that offfers support for models based on the keras api

class chariots.ml.keras.KerasOp(mode: chariots.ml._ml_mode.MLMode, verbose: Optional[int] = 1)[source]

Bases: chariots.ml._base_ml_op.BaseMLOp

Keras Ops help you create ops for all your Keras based neural networks.

To create your keras op, you will need to:

  • define the initialisation behavior of your model by overriding the _init_model method.

  • define any additional training parameters using the fit_params VersionedFieldDict.

>>> from chariots.pipelines import Pipeline
>>> from chariots.pipelines.nodes import Node
>>> from chariots.ml import MLMode
>>> from chariots.versioning import VersionType, VersionedFieldDict
>>> from keras import models, layers
...
...
>>> class KerasLinear(KerasOp):
...     fit_params = VersionedFieldDict(VersionType.MAJOR, {
...         'epochs': 3,
...         'batch_size': 32,
...     })
...
...     def _init_model(self, *input_data_sets):
...         model = models.Sequential([layers.Dense(3, activation='softmax', input_shape=(4,))])
...         model.compile(loss='categorical_crossentropy', optimizer='adam')
...         return model
...
...
>>> train = Pipeline([
...     Node(IrisFullDataSet(), output_nodes=["X", "y"]),
...     Node(Categorize(), input_nodes=['y'], output_nodes='y_cat'),
...     Node(KerasLinear(mode=MLMode.FIT, verbose=0), input_nodes=['X', 'y_cat'])
... ], 'train')
>>> pred = Pipeline([
...     Node(KerasLinear(mode=MLMode.PREDICT), input_nodes=['__pipeline_input__'],
...          output_nodes='__pipeline_output__')
... ], 'pred')

than you can call your pipeline as you would with any other:

>>> runner.run(train)
...
>>> runner.run(pred, np.array([[1, 2, 3, 4]])) 
array([[...]], dtype=float32)

or use them in an app:

>>> app = PipelinesServer([train, pred], op_store_client=op_store_client, import_name='my_app')
__init__(mode: chariots.ml._ml_mode.MLMode, verbose: Optional[int] = 1)[source]
Parameters

mode – the mode to use when instantiating the op

fit(input_data_sets: Union[List[numpy.ndarray], numpy.ndarray], output_datasets: Union[List[numpy.ndarray], numpy.ndarray])[source]

fits the inner model of the op on data (in args and kwargs) this method must not return any data (use the FIT_PREDICT mode to predict on the same data the op was trained on)

input_params = <chariots.versioning._versioned_field_dict.VersionedFieldDict object>
predict(input_datasets) → Any[source]

the method used to do predictions/inference once the model has been fitted/loaded