ytm.guided.RegressionTM#
- class ytm.guided.RegressionTM(n_clauses: int, s: float, dim: int | tuple[int, ...], **opt: Unpack[T_Config])#
Bases:
BaseTMGuided TM for regression: one real-valued target per sample.
Defaults differ from
TMConfig:act_lossisMSE.- Parameters:
- n_clausesint
Number of clauses.
- sfloat
Specificity, at least 1.0.
- dimint or tuple of int
Input shape:
D,(H, W)or(H, W, C).- **opt
Other options of
TMConfig, anddeviceandblock_sizeofDeviceConfig.
- property config: TMConfig#
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. Useto()to change the device.
- fit(X: ndarray, Y: ndarray, shuffle: bool = True, clause_drop_p: float = 0.0, lr: float | None = None, lambda_: float | None = None) float#
Train for one epoch.
- Parameters:
- Xarray-like of int, shape
(N, *dim)or(N, prod(dim)) Inputs, each feature within
[feat_mins, feat_maxs].- Yarray-like of float, shape (N,)
Targets.
- shufflebool, default=True
Visit the samples in a random order.
- clause_drop_pfloat, default=0.0
Probability of dropping each clause for this epoch (Drop Clause).
- lrfloat, default=None
Learning rate for this epoch. None uses the config’s
lr.- lambda_float, default=None
Feedback strength for this epoch. None uses the config’s
lambda_.
- Xarray-like of int, shape
- Returns:
- float
Mean loss per sample over the epoch.
- 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)
- predict(X: ndarray, force_repack: bool = False)#
Predict the target of each sample: the activated output.
- Parameters:
- Xarray-like of int, shape
(N, *dim)or(N, prod(dim)) Inputs, each feature within
[feat_mins, feat_maxs].- force_repackbool, default=False
Repack every clause first, not only the changed ones.
- Xarray-like of int, shape
- Returns:
- predsndarray of shape (N,)
Predicted targets.
- class_sumsndarray of shape (N, 1)
Activated output, the same values as
preds.
- 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.
- Xarray-like of int, shape
- 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
DeviceConfigoptions, e.g.block_size. Options not given go back to their defaults.
- to_prob(class_sums: ndarray) ndarray#
No-op, simply returns the activated class sums (
class_sums) as is.
- 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.
- Xarray-like of int, shape
- Returns:
- ndarray of shape (N, n_clause_banks, n_clauses), int8
Clause outputs.
n_clause_banksis 1 when coalesced, elsen_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.
- Xarray-like of int, shape
- Returns:
- ndarray of shape (N, n_clause_banks, n_clauses, n_patches_y, n_patches_x), int8
Clause outputs.
n_clause_banksis 1 when coalesced, elsen_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
+1for the clauses with positive weight for the target class,-1for negative.- force_repackbool, default=False
Force a clause repack.
- Xarray-like of int, shape
- 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_idandpolaritydefine 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
+1for the clauses with positive weight forclass_id,-1for 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.