matchzoo.utils
¶
Submodules¶
Package Contents¶
Classes¶
Computes and stores the average and current value. |
|
Computes elapsed time. |
|
EarlyStopping stops training if no improvement after a given patience. |
Functions¶
|
|
List all concrete subclasses of base recursively. |
|
|
Retrieves a torch Module instance. |
|
Retrieves a torch Module instance. |
|
Parse input metric in any form into a |
|
Parse input metric in any form into a |
|
Downloads a file from a URL if it not already in the cache. |
|
Calculates a file sha256 or md5 hash. |
-
matchzoo.utils.
one_hot
(indices: int, num_classes: int) → np.ndarray¶ - Returns
A one-hot encoded vector.
-
matchzoo.utils.
TensorType
¶
-
matchzoo.utils.
list_recursive_concrete_subclasses
(base)¶ List all concrete subclasses of base recursively.
-
matchzoo.utils.
parse_loss
(identifier: typing.Union[str, typing.Type[nn.Module], nn.Module], task: typing.Optional[str] = None) → nn.Module¶ Retrieves a torch Module instance.
- Parameters
identifier – loss identifier, one of - String: name of a loss - Torch Module subclass - Torch Module instance (it will be returned unchanged).
task – Task type for determining specific loss.
- Returns
A
nn.Module
instance
- Examples::
>>> from torch import nn >>> from matchzoo.utils import parse_loss
- Use str as loss:
>>> loss = parse_loss('mse') >>> type(loss) <class 'torch.nn.modules.loss.MSELoss'>
- Use
torch.nn.Module
subclasses as loss: >>> type(parse_loss(nn.MSELoss)) <class 'torch.nn.modules.loss.MSELoss'>
- Use
torch.nn.Module
instances as loss: >>> type(parse_loss(nn.MSELoss())) <class 'torch.nn.modules.loss.MSELoss'>
-
matchzoo.utils.
parse_activation
(identifier: typing.Union[str, typing.Type[nn.Module], nn.Module]) → nn.Module¶ Retrieves a torch Module instance.
- Parameters
identifier – activation identifier, one of - String: name of a activation - Torch Modele subclass - Torch Module instance (it will be returned unchanged).
- Returns
A
nn.Module
instance
- Examples::
>>> from torch import nn >>> from matchzoo.utils import parse_activation
- Use str as activation:
>>> activation = parse_activation('relu') >>> type(activation) <class 'torch.nn.modules.activation.ReLU'>
- Use
torch.nn.Module
subclasses as activation: >>> type(parse_activation(nn.ReLU)) <class 'torch.nn.modules.activation.ReLU'>
- Use
torch.nn.Module
instances as activation: >>> type(parse_activation(nn.ReLU())) <class 'torch.nn.modules.activation.ReLU'>
-
matchzoo.utils.
parse_metric
(metric: typing.Union[str, typing.Type[BaseMetric], BaseMetric], task: str) → BaseMetric¶ Parse input metric in any form into a
BaseMetric
instance.- Parameters
metric – Input metric in any form.
task – Task type for determining specific metric.
- Returns
A
BaseMetric
instance
- Examples::
>>> from matchzoo import metrics >>> from matchzoo.utils import parse_metric
- Use str as MatchZoo metrics:
>>> mz_metric = parse_metric('map', 'ranking') >>> type(mz_metric) <class 'matchzoo.metrics.mean_average_precision.MeanAveragePrecision'>
- Use
matchzoo.engine.BaseMetric
subclasses as MatchZoo metrics: >>> type(parse_metric(metrics.AveragePrecision, 'ranking')) <class 'matchzoo.metrics.average_precision.AveragePrecision'>
- Use
matchzoo.engine.BaseMetric
instances as MatchZoo metrics: >>> type(parse_metric(metrics.AveragePrecision(), 'ranking')) <class 'matchzoo.metrics.average_precision.AveragePrecision'>
-
matchzoo.utils.
parse_optimizer
(identifier: typing.Union[str, typing.Type[optim.Optimizer]]) → optim.Optimizer¶ Parse input metric in any form into a
Optimizer
class.- Parameters
optimizer – Input optimizer in any form.
- Returns
A
Optimizer
class
- Examples::
>>> from torch import optim >>> from matchzoo.utils import parse_optimizer
- Use str as optimizer:
>>> parse_optimizer('adam') <class 'torch.optim.adam.Adam'>
- Use
torch.optim.Optimizer
subclasses as optimizer: >>> parse_optimizer(optim.Adam) <class 'torch.optim.adam.Adam'>
-
class
matchzoo.utils.
AverageMeter
¶ Bases:
object
Computes and stores the average and current value.
Examples
>>> am = AverageMeter() >>> am.update(1) >>> am.avg 1.0 >>> am.update(val=2.5, n=2) >>> am.avg 2.0
-
reset
(self)¶ Reset AverageMeter.
-
update
(self, val, n=1)¶ Update value.
-
property
avg
(self)¶ Get avg.
-
-
class
matchzoo.utils.
Timer
¶ Bases:
object
Computes elapsed time.
-
reset
(self)¶ Reset timer.
-
resume
(self)¶ Resume.
-
stop
(self)¶ Stop.
-
property
time
(self)¶ Return time.
-
-
class
matchzoo.utils.
EarlyStopping
(patience: typing.Optional[int] = None, should_decrease: bool = None, key: typing.Any = None)¶ EarlyStopping stops training if no improvement after a given patience.
- Parameters
patience – Number fo events to wait if no improvement and then stop the training.
should_decrease – The way to judge the best so far.
key – Key of metric to be compared.
-
state_dict
(self) → typing.Dict[str, typing.Any]¶ A Trainer can use this to serialize the state.
-
load_state_dict
(self, state_dict: typing.Dict[str, typing.Any]) → None¶ Hydrate a early stopping from a serialized state.
-
update
(self, result: list)¶ Call function.
-
property
best_so_far
(self) → bool¶ Returns best so far.
-
property
is_best_so_far
(self) → bool¶ Returns true if it is the best so far.
-
property
should_stop_early
(self) → bool¶ Returns true if improvement has stopped for long enough.
-
matchzoo.utils.
get_file
(fname: str = None, origin: str = None, untar: bool = False, extract: bool = False, md5_hash: typing.Any = None, file_hash: typing.Any = None, hash_algorithm: str = 'auto', archive_format: str = 'auto', cache_subdir: typing.Union[Path, str] = 'data', cache_dir: typing.Union[Path, str] = matchzoo.USER_DATA_DIR, verbose: int = 1) → str¶ Downloads a file from a URL if it not already in the cache.
By default the file at the url origin is downloaded to the cache_dir ~/.matchzoo/datasets, placed in the cache_subdir data, and given the filename fname. The final location of a file example.txt would therefore be ~/.matchzoo/datasets/data/example.txt.
Files in tar, tar.gz, tar.bz, and zip formats can also be extracted. Passing a hash will verify the file after download. The command line programs shasum and sha256sum can compute the hash.
- Parameters
fname – Name of the file. If an absolute path /path/to/file.txt is specified the file will be saved at that location.
origin – Original URL of the file.
untar – Deprecated in favor of ‘extract’. Boolean, whether the file should be decompressed.
md5_hash – Deprecated in favor of ‘file_hash’. md5 hash of the file for verification.
file_hash – The expected hash string of the file after download. The sha256 and md5 hash algorithms are both supported.
cache_subdir – Subdirectory under the cache dir where the file is saved. If an absolute path /path/to/folder is specified the file will be saved at that location.
hash_algorithm – Select the hash algorithm to verify the file. options are ‘md5’, ‘sha256’, and ‘auto’. The default ‘auto’ detects the hash algorithm in use.
archive_format – Archive format to try for extracting the file. Options are ‘auto’, ‘tar’, ‘zip’, and None. ‘tar’ includes tar, tar.gz, and tar.bz files. The default ‘auto’ is [‘tar’, ‘zip’]. None or an empty list will return no matches found.
cache_dir – Location to store cached files, when None it defaults to the [matchzoo.USER_DATA_DIR](~/.matchzoo/datasets).
verbose – Verbosity mode, 0 (silent), 1 (verbose), 2 (semi-verbose)
- Papram extract
True tries extracting the file as an Archive, like tar or zip.
- Returns
Path to the downloaded file.
-
matchzoo.utils.
_hash_file
(fpath, algorithm='sha256', chunk_size=65535)¶ Calculates a file sha256 or md5 hash.
- Parameters
fpath – path to the file being validated
algorithm – hash algorithm, one of ‘auto’, ‘sha256’, or ‘md5’. The default ‘auto’ detects the hash algorithm in use.
chunk_size – Bytes to read at a time, important for large files.
- Returns
The file hash.