ytm.simple.MultiClassTM#

class ytm.simple.MultiClassTM(n_clauses: int, T: float | tuple[float, float], s: float, dim: int | tuple[int, ...], n_classes: int, q: float = 1.0, coalesced: bool = True, patch_dim: tuple[int, int] | None = None, negative_clauses: bool = True, negated_literals: bool = True, position_literals: bool = True, max_includes: int | None = None, n_states: int = 256, ta_init: str | int = 'middle', boost_tp_inc: bool = True, boost_tp_dec: bool = False, allow_polarity_change: bool = True, seed: int | None = None)#

Bases: BaseTM

TM for multi-class classification: each sample has exactly one of n_classes classes.

The target class is pushed towards T_max, the sampled other classes towards T_min. See BaseTM for the parameters.

calc_class_sums(clause_outputs: ndarray, clip: bool) → ndarray#

Weighted vote of the active clauses per class. clause_outputs has shape (N, n_banks, n_clauses).

clause_outputs(X: ndarray) → ndarray#

Clause outputs, the OR over patches. Returns shape (N, n_banks, n_clauses), bool.

feedback(class_id: int, X: ndarray, co: ndarray, dropped: ndarray, target: int, prob: float) → None#

Give feedback for one class.

Parameters:
class_idint

Class to give feedback for.

Xndarray of shape (n_clauses, n_literals), bool

Literals of the patch each clause is contrasted against.

condarray of shape (n_clauses,), bool

Clause outputs.

droppedndarray of shape (n_clauses,), bool

Clauses dropped for this epoch.

targetint

+1 to push the class sum up to T_max, -1 to push it down to T_min.

probfloat

Probability of selecting each clause for feedback.

fit(X: ndarray, Y: ndarray, shuffle: bool = True, clause_drop_p: float = 0.0) → None#

Train for one epoch.

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

Binary inputs.

Yarray-like

Targets, see the subclass.

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). Dropped clauses output 0 and get no feedback.

predict(X: ndarray)#

Predict the class of each sample: the class with the highest class sum.

Returns:
predsndarray of shape (N,)

Predicted class.

class_sumsndarray of shape (N, n_classes)

Weighted vote sum of each class.

score(X: ndarray) → ndarray#

Class sums for X, not clipped. Returns shape (N, n_classes), float32.

t1a(bank: int, clause_filter: ndarray, X: ndarray) → None#

Type Ia feedback: reinforce include for true literals, exclude for false literals.

t1b(bank: int, clause_filter: ndarray) → None#

Type Ib feedback: reinforce exclude for every literal with probability 1/s.

t2(bank: int, clause_filter: ndarray, X: ndarray) → None#

Type II feedback: penalize the exclusion of false literals, making the clause output 0 for the input.