matchzoo.engine.base_model

Base Model.

Module Contents

class matchzoo.engine.base_model.BaseModel(params:typing.Optional[ParamTable]=None)

Bases: torch.nn.Module, abc.ABC

Abstract base class of all MatchZoo models.

MatchZoo models are wrapped over pytorch models. params is a set of model hyper-parameters that deterministically builds a model. In other words, params[‘model_class’](params=params) of the same params always create models with the same structure.

Parameters:params – Model hyper-parameters. (default: return value from get_default_params())

Example

>>> BaseModel()  # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: Can't instantiate abstract class BaseModel ...
>>> class MyModel(BaseModel):
...     def build(self):
...         pass
...     def forward(self):
...         pass
>>> isinstance(MyModel(), BaseModel)
True
params

model parameters.

Type:return
classmethod get_default_params(cls, with_embedding=False, with_multi_layer_perceptron=False)

Model default parameters.

The common usage is to instantiate matchzoo.engine.ModelParams
first, then set the model specific parametrs.

Examples

>>> class MyModel(BaseModel):
...     def build(self):
...         print(self._params['num_eggs'], 'eggs')
...         print('and', self._params['ham_type'])
...     def forward(self, greeting):
...         print(greeting)
...
...     @classmethod
...     def get_default_params(cls):
...         params = ParamTable()
...         params.add(Param('num_eggs', 512))
...         params.add(Param('ham_type', 'Parma Ham'))
...         return params
>>> my_model = MyModel()
>>> my_model.build()
512 eggs
and Parma Ham
>>> my_model('Hello MatchZoo!')
Hello MatchZoo!

Notice that all parameters must be serialisable for the entire model to be serialisable. Therefore, it’s strongly recommended to use python native data types to store parameters.

Returns:model parameters
guess_and_fill_missing_params(self, verbose=1)

Guess and fill missing parameters in params.

Use this method to automatically fill-in other hyper parameters. This involves some guessing so the parameter it fills could be wrong. For example, the default task is Ranking, and if we do not set it to Classification manaully for data packs prepared for classification, then the shape of the model output and the data will mismatch.

Parameters:verbose – Verbosity.
_set_param_default(self, name:str, default_val:str, verbose:int=0)
classmethod get_default_preprocessor(cls)

Model default preprocessor.

The preprocessor’s transform should produce a correctly shaped data pack that can be used for training.

Returns:Default preprocessor.
classmethod get_default_padding_callback(cls)

Model default padding callback.

The padding callback’s on_batch_unpacked would pad a batch of data to a fixed length.

Returns:Default padding callback.
build(self)

Build model, each subclass need to implement this method.

forward(self, *input)

Defines the computation performed at every call.

Should be overridden by all subclasses.

_make_embedding_layer(self, num_embeddings:int=0, embedding_dim:int=0, freeze:bool=True, embedding:typing.Optional[np.ndarray]=None, **kwargs)
Returns:an embedding module.
_make_default_embedding_layer(self, **kwargs)
Returns:an embedding module.
_make_output_layer(self, in_features:int=0, activation:typing.Union[str, nn.Module]=None)
Returns:a correctly shaped torch module for model output.
_make_perceptron_layer(self, in_features:int=0, out_features:int=0, activation:nn.Module=nn.ReLU)
Returns:a perceptron layer.
_make_multi_layer_perceptron_layer(self, in_features)
Returns:a multiple layer perceptron.