braintools.cogtask.one_hot

Contents

braintools.cogtask.one_hot#

braintools.cogtask.one_hot(key, num_classes=None, active_value=1.0)[source]#

Create a one-hot encoding value specification.

Parameters:
  • key (str) – Context key containing the class index.

  • num_classes (int | None) – Number of classes. If None, inferred from feature.num.

  • active_value (float) – Value for the active class (default 1.0).

Returns:

Encoder function (ctx, feature) -> np.ndarray

Return type:

Callable[[Context, Feature], Array]

Examples

>>> from braintools.cogtask import Context, Feature, one_hot
>>> ctx = Context()
>>> ctx['sample_idx'] = 2
>>> encode = one_hot('sample_idx')  # e.g. inputs={'stimulus': encode}
>>> encode(ctx, Feature(4, 'stimulus'))
Array([0., 0., 1., 0.], dtype=float32)
>>>
>>> one_hot('sample_idx', num_classes=3, active_value=0.5)(ctx, Feature(3, 'stimulus'))
Array([0. , 0. , 0.5], dtype=float32)