matchzoo.auto.tuner.callbacks package

Submodules

matchzoo.auto.tuner.callbacks.callback module

class matchzoo.auto.tuner.callbacks.callback.Callback

Bases: object

Tuner callback base class.

To build your own callbacks, inherit mz.auto.tuner.callbacks.Callback and overrides corresponding methods.

A run proceeds in the following way:

  • run start (callback)
  • build model
  • build end (callback)
  • fit and evaluate model
  • collect result
  • run end (callback)

This process is repeated for num_runs times in a tuner.

on_build_end(tuner, model)

Callback on build end stage.

Parameters:
  • tuner (Tuner) – Tuner.
  • model (BaseModel) – A built model ready for fitting and evluating. Changes to this model affect the fitting and evaluating process.
on_run_end(tuner, model, result)

Callback on run end stage.

Parameters:
  • tuner (Tuner) – Tuner.
  • model (BaseModel) – A built model done fitting and evaluating. Changes to the model will no longer affect the result.
  • result (dict) – Result of the run. Changes to this dictionary will be visible in the return value of the tune method.
on_run_start(tuner, sample)

Callback on run start stage.

Parameters:
  • tuner (Tuner) – Tuner.
  • sample (dict) – Sampled hyper space. Changes to this dictionary affects the model building process of the tuner.

matchzoo.auto.tuner.callbacks.lambda_callback module

class matchzoo.auto.tuner.callbacks.lambda_callback.LambdaCallback(on_run_start=None, on_build_end=None, on_run_end=None)

Bases: matchzoo.auto.tuner.callbacks.callback.Callback

LambdaCallback. Just a shorthand for creating a callback class.

See matchzoo.tuner.callbacks.Callback for more details.

Example

>>> import matchzoo as mz
>>> model = mz.models.Naive()
>>> model.guess_and_fill_missing_params(verbose=0)
>>> data = mz.datasets.toy.load_data()
>>> data = model.get_default_preprocessor().fit_transform(
...     data, verbose=0)
>>> def show_inputs(*args):
...     print(' '.join(map(str, map(type, args))))
>>> callback = mz.auto.tuner.callbacks.LambdaCallback(
...     on_run_start=show_inputs,
...     on_build_end=show_inputs,
...     on_run_end=show_inputs
... )
>>> _ = mz.auto.tune(
...     params=model.params,
...     train_data=data,
...     test_data=data,
...     num_runs=1,
...     callbacks=[callback],
...     verbose=0,
... ) # noqa: E501
<class 'matchzoo.auto.tuner.tuner.Tuner'> <class 'dict'>
<class 'matchzoo.auto.tuner.tuner.Tuner'> <class 'matchzoo.models.naive.Naive'>
<class 'matchzoo.auto.tuner.tuner.Tuner'> <class 'matchzoo.models.naive.Naive'> <class 'dict'>
on_build_end(tuner, model)

on_build_end.

on_run_end(tuner, model, result)

on_run_end.

on_run_start(tuner, sample)

on_run_start.

matchzoo.auto.tuner.callbacks.load_embedding_matrix module

class matchzoo.auto.tuner.callbacks.load_embedding_matrix.LoadEmbeddingMatrix(embedding_matrix)

Bases: matchzoo.auto.tuner.callbacks.callback.Callback

Load a pre-trained embedding after the model is built.

Used with tuner to load a pre-trained embedding matrix for each newly built model instance.

Parameters:embedding_matrix – Embedding matrix to load.

Example

>>> import matchzoo as mz
>>> model = mz.models.ArcI()
>>> prpr = model.get_default_preprocessor()
>>> data = mz.datasets.toy.load_data()
>>> data = prpr.fit_transform(data, verbose=0)
>>> embed = mz.datasets.toy.load_embedding()
>>> term_index = prpr.context['vocab_unit'].state['term_index']
>>> matrix = embed.build_matrix(term_index)
>>> callback = mz.auto.tuner.callbacks.LoadEmbeddingMatrix(matrix)
>>> model.params.update(prpr.context)
>>> model.params['task'] = mz.tasks.Ranking()
>>> model.params['embedding_output_dim'] = embed.output_dim
>>> result = mz.auto.tune(
...     params=model.params,
...     train_data=data,
...     test_data=data,
...     num_runs=1,
...     callbacks=[callback],
...     verbose=0
... )
on_build_end(tuner, model)

on_build_end.

matchzoo.auto.tuner.callbacks.save_model module

class matchzoo.auto.tuner.callbacks.save_model.SaveModel(dir_path=PosixPath('/home/docs/.matchzoo/tuned_models'))

Bases: matchzoo.auto.tuner.callbacks.callback.Callback

Save trained model.

For each trained model, a UUID will be generated as the model_id, the model will be saved under the dir_path/model_id. A model_id key will also be inserted into the result, which will visible in the return value of the tune method.

Parameters:dir_path (Union[str, Path]) – Path to save the models to. (default: matchzoo.USER_TUNED_MODELS_DIR)
on_run_end(tuner, model, result)

Save model on run end.

Module contents