matchzoo.engine.base_model

Base Model.

Module Contents

Classes

BaseModel

Abstract base class of all MatchZoo models.

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()  
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
classmethod get_default_params(cls, with_embedding=False, with_multi_layer_perceptron=False) → ParamTable

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, truncated_mode: str = 'pre', truncated_length_left: typing.Optional[int] = None, truncated_length_right: typing.Optional[int] = None, filter_mode: str = 'df', filter_low_freq: float = 1, filter_high_freq: float = float('inf'), remove_stop_words: bool = False, ngram_size: typing.Optional[int] = None) → BasePreprocessor

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, fixed_length_left: int = None, fixed_length_right: int = None, pad_word_value: typing.Union[int, str] = 0, pad_word_mode: str = 'pre', with_ngram: bool = False, fixed_ngram_length: int = None, pad_ngram_value: typing.Union[int, str] = 0, pad_ngram_mode: str = 'pre') → BaseCallback

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.

property params(self) → ParamTable
Returns

model parameters.

abstract build(self)

Build model, each subclass need to implement this method.

abstract 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) → nn.Module
Returns

an embedding module.

_make_default_embedding_layer(self, **kwargs) → nn.Module
Returns

an embedding module.

_make_output_layer(self, in_features: int = 0) → nn.Module
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()) → nn.Module
Returns

a perceptron layer.

_make_multi_layer_perceptron_layer(self, in_features) → nn.Module
Returns

a multiple layer perceptron.