ytm._core.base.BaseTM#

class ytm._core.base.BaseTM(config: BaseTMConfig, device_config: DeviceConfig)#

Bases: ABC

property config: BaseTMConfig#

Model settings, see BaseTMConfig. Treat it as read-only: the kernels and derived values are built from it at construction.

property device_config: DeviceConfig#

Device settings, see DeviceConfig. Use to() to change the device.

get_clauses(force_repack: bool = True) → ClauseInfo#

Get the clauses as feature bounds, position bounds and weights.

The result is cached in clauseinfo.

Parameters:
force_repackbool, default=True

Build the clauses again from the current TA states. False returns the cached result when there is one.

Returns:
ClauseInfo

See ClauseInfo.

get_literals() → ndarray#

Get the clauses showing include and excludes only. 1 if the literal is included, 0 if excluded.

Returns:
ndarray of shape (n_clause_banks, n_clauses, n_literals), uint8
get_patch_weights() → ndarray#

Get patch weights.

Returns:
ndarray of shape (n_clause_banks, n_clauses, n_patches_y, n_patches_x)
get_ta_states() → ndarray#

Get TA states.

Returns:
ndarray of shape (n_clause_banks, n_clauses, n_literals)
get_weights() → ndarray#

Get weights.

Returns:
ndarray of shape (n_classes, n_clauses), float32
score(X: ndarray, force_repack: bool = False) → ndarray#

Calculate class sums or model outputs for the given X.

Parameters:
Xarray-like of int, shape (N, *dim) or (N, prod(dim))

Inputs, each feature within [feat_mins, feat_maxs].

force_repackbool, default=False

Force a clause repack.

Returns:
ndarray of shape (N, n_classes), float32

Output of each class.

set_threads(n: int) → None#

Set the number of threads to use with OpenMP. Only applicable for CPU device.

to(device: str, **device_kwargs) → None#

Move the model to another device, in place.

Parameters:
devicestr

"cpu[:n_threads]" or "cuda[:gpu_id]".

**device_kwargs

Other DeviceConfig options, e.g. block_size. Options not given go back to their defaults.

abstractmethod to_prob(class_sums: ndarray) → ndarray#

Convert the class sums / raw model outputs to probabilities.

transform(X: ndarray, force_repack: bool = False) → ndarray#

Clause outputs per sample: 1 where the clause fires (on any patch), else 0.

Parameters:
Xarray-like of int, shape (N, *dim) or (N, prod(dim))

Inputs, each feature within [feat_mins, feat_maxs].

force_repackbool, default=False

Force a clause repack.

Returns:
ndarray of shape (N, n_clause_banks, n_clauses), int8

Clause outputs. n_clause_banks is 1 when coalesced, else n_classes.

transform_patchwise(X: ndarray, force_repack: bool = False) → ndarray#

Patchwise clause outputs per sample.

Parameters:
Xarray-like of int, shape (N, *dim) or (N, prod(dim))

Inputs, each feature within [feat_mins, feat_maxs].

force_repackbool, default=False

Force a clause repack.

Returns:
ndarray of shape (N, n_clause_banks, n_clauses, n_patches_y, n_patches_x), int8

Clause outputs. n_clause_banks is 1 when coalesced, else n_classes.

wac(X: ndarray, target_classes: ndarray, polarity: int, force_repack: bool = False) → ndarray#

Calculate the WAC score, obtained by taking the weighted sum of activated clauses for each sample in X. This provides the Local Interpretation of the model for a given input.

Note

This functions does not normalize the WAC, it simply returns the raw values. Use norm_asymmetric() on the scores to normalize them.

Parameters:
Xarray-like of int, shape (N, *dim) or (N, prod(dim))

Inputs, each feature within [feat_mins, feat_maxs].

target_classesarray-like of int, shape (N,)

Class whose clause weights are used, per sample.

polarityint

+1 for the clauses with positive weight for the target class, -1 for negative.

force_repackbool, default=False

Force a clause repack.

Returns:
ndarray of shape (N, H, W, C), float32

WAC score per sample and input feature.

wic(class_id: int, polarity: int, pw_th: float = 0.0, force_repack: bool = False) → ndarray#

Calculate the WIC score, obtained by taking weighted sum of a set of clauses. This does not depend on X, does not care about clause activations, and thus provides the Global Interpretation of the model.

class_id and polarity define which set of clauses to use.

Note

This functions does not normalize the WIC, it simply returns the raw values. Use norm_asymmetric() on the scores to normalize them.

Parameters:
class_idint

Class whose clause weights are used.

polarityint

+1 for the clauses with positive weight for class_id, -1 for negative.

pw_thfloat, default=0.0

Convolutional models only: skip patches whose patch weight, normalized per clause to [0, 1], is at or below this value.

force_repackbool, default=False

Force a clause repack.

Returns:
ndarray of shape (H, W, C), float32

WIC score per input feature.