ytm.tm.RegressionTM#
- class ytm.tm.RegressionTM(n_clauses: int, T: float, s: float, dim: int | tuple[int, ...], y_range: tuple[float, float], **opt: Unpack[T_Config])#
Bases:
BaseTMTM for regression: one real-valued target per sample.
Targets in
y_rangeare scaled to class sums in[0, T], and predictions are scaled back. Defaults differ fromRegressionConfig:negative_clauses=False.- Parameters:
- n_clausesint
Number of clauses.
- Tfloat
Vote threshold. Class sums are clipped to
[0, T].- sfloat
Specificity, at least 1.0.
- dimint or tuple of int
Input shape:
D,(H, W)or(H, W, C).- y_rangetuple of (float, float)
Range
(lo, hi)of the targets.- **opt
Other options of
RegressionConfig, anddeviceandblock_sizeofDeviceConfig.
- property config: RegressionConfig#
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)#
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, each within
y_range.- 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).
- Xarray-like of int, shape
- 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, clip_class_sums: bool = True, force_repack: bool = False)#
Predict the target of each sample: the class sum scaled from
[0, T]back toy_range.- Parameters:
- Xarray-like of int, shape
(N, *dim)or(N, prod(dim)) Inputs, each feature within
[feat_mins, feat_maxs].- clip_class_sumsbool, default=True
Clip class sums to
[0, T]before scaling, so predictions stay withiny_range.- 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)
Weighted vote sum.
- score(X: ndarray, force_repack: bool = False, clip_class_sums: bool = False) ndarray#
Calculate class sums 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
Repack every clause first, not only the changed ones.
- clip_class_sumsbool, default=False
Clip the class sums to
[T_min, T_max].
- Xarray-like of int, shape
- Returns:
- ndarray of shape (N, n_classes), float32
Class sums.
- 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#
Convert the class sums to probabilities.
(clip(class_sums, T_min, T_max) - T_min) / (T_max - T_min)
- 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.