matchzoo.auto
¶
Subpackages¶
Package Contents¶
Classes¶
Unified setup processes of all MatchZoo models. |
|
Model hyper-parameters tuner. |
-
class
matchzoo.auto.
Preparer
(task: BaseTask, config: typing.Optional[dict] = None)¶ Bases:
object
Unified setup processes of all MatchZoo models.
config is used to control specific behaviors. The default config will be updated accordingly if a config dictionary is passed. e.g. to override the default bin_size, pass config={‘bin_size’: 15}.
See tutorials/automation.ipynb for a detailed walkthrough on usage.
Default config:
- {
# pair generator builder kwargs ‘num_dup’: 1,
# histogram unit of DRMM ‘bin_size’: 30, ‘hist_mode’: ‘LCH’,
# dynamic Pooling of MatchPyramid ‘compress_ratio_left’: 1.0, ‘compress_ratio_right’: 1.0,
# if no matchzoo.Embedding is passed to tune ‘embedding_output_dim’: 50
}
- Parameters
task – Task.
config – Configuration of specific behaviors.
Example
>>> import matchzoo as mz >>> task = mz.tasks.Ranking(losses=mz.losses.RankCrossEntropyLoss()) >>> preparer = mz.auto.Preparer(task) >>> model_class = mz.models.DenseBaseline >>> train_raw = mz.datasets.toy.load_data('train', 'ranking') >>> model, prpr, dsb, dlb = preparer.prepare(model_class, ... train_raw) >>> model.params.completed(exclude=['out_activation_func']) True
-
prepare
(self, model_class: typing.Type[BaseModel], data_pack: mz.DataPack, callback: typing.Optional[BaseCallback] = None, preprocessor: typing.Optional[BasePreprocessor] = None, embedding: typing.Optional[‘mz.Embedding’] = None) → typing.Tuple[BaseModel, BasePreprocessor, DatasetBuilder, DataLoaderBuilder]¶ Prepare.
- Parameters
model_class – Model class.
data_pack – DataPack used to fit the preprocessor.
callback – Callback used to padding a batch. (default: the default callback of model_class)
preprocessor – Preprocessor used to fit the data_pack. (default: the default preprocessor of model_class)
- Returns
A tuple of (model, preprocessor, dataset_builder, dataloader_builder).
-
_build_model
(self, model_class, preprocessor, embedding) → typing.Tuple[BaseModel, np.ndarray]¶
-
_build_matrix
(self, preprocessor, embedding)¶
-
_build_dataset_builder
(self, model, embedding_matrix, preprocessor)¶
-
_build_dataloader_builder
(self, model, callback)¶
-
_infer_num_neg
(self)¶
-
classmethod
get_default_config
(cls) → dict¶ Default config getter.
-
class
matchzoo.auto.
Tuner
(params: mz.ParamTable, optimizer: str = 'adam', trainloader: mz.dataloader.DataLoader = None, validloader: mz.dataloader.DataLoader = None, embedding: np.ndarray = None, fit_kwargs: dict = None, metric: typing.Union[str, BaseMetric] = None, mode: str = 'maximize', num_runs: int = 10, verbose=1)¶ Bases:
object
Model hyper-parameters tuner.
model.params.hyper_space reprensents the model’s hyper-parameters search space, which is the cross-product of individual hyper parameter’s hyper space. When a Tuner builds a model, for each hyper parameter in model.params, if the hyper-parameter has a hyper-space, then a sample will be taken in the space. However, if the hyper-parameter does not have a hyper-space, then the default value of the hyper-parameter will be used.
See tutorials/model_tuning.ipynb for a detailed walkthrough on usage.
- Parameters
params – A completed parameter table to tune. Usually model.params of the desired model to tune. params.completed() should be True.
optimizer – Str or Optimizer class. Optimizer for optimizing model.
trainloader – Training data to use. Should be a DataLoader.
validloader – Testing data to use. Should be a DataLoader.
embedding – Embedding used by model.
fit_kwargs – Extra keyword arguments to pass to fit. (default: dict(epochs=10, verbose=0))
metric – Metric to tune upon. Must be one of the metrics in model.params[‘task’].metrics. (default: the first metric in params.[‘task’].metrics.
mode – Either maximize the metric or minimize the metric. (default: ‘maximize’)
num_runs – Number of runs. Each run takes a sample in params.hyper_space and build a model based on the sample. (default: 10)
verbose – Verbosity. (default: 1)
-
tune
(self)¶ Start tuning.
Notice that tune does not affect the tuner’s inner state, so each new call to tune starts fresh. In other words, hyperspaces are suggestive only within the same tune call.
-
_fmin
(self, trials)¶
-
_run
(self, sample)¶
-
_create_full_params
(self, sample)¶
-
_fix_loss_sign
(self, loss)¶
-
classmethod
_log_result
(cls, result)¶
-
property
params
(self)¶ params getter.
-
property
trainloader
(self)¶ trainloader getter.
-
property
validloader
(self)¶ validloader getter.
-
property
fit_kwargs
(self)¶ fit_kwargs getter.
-
property
metric
(self)¶ metric getter.
-
property
mode
(self)¶ mode getter.
-
property
num_runs
(self)¶ num_runs getter.
-
property
verbose
(self)¶ verbose getter.
-
classmethod
_validate_params
(cls, params)¶
-
classmethod
_validate_optimizer
(cls, optimizer)¶
-
classmethod
_validate_dataloader
(cls, data)¶
-
classmethod
_validate_kwargs
(cls, kwargs)¶
-
classmethod
_validate_mode
(cls, mode)¶
-
classmethod
_validate_metric
(cls, params, metric)¶
-
classmethod
_validate_num_runs
(cls, num_runs)¶