eegproc.deep_learning.domain_generalization.meta_learning module

class eegproc.deep_learning.domain_generalization.meta_learning.MLDGEpisodeSequence(*, eeg, labels, subject_ids, trial_ids, sample_weight, meta_train_subjects, meta_test_subjects, trials_per_subject, steps_per_epoch, seed)[source]

Bases: PyDataset

Build class-balanced, complete-trial MLDG episodes for domain generalization.

One sequence item is one MLDG update. Meta-train and meta-test subjects are disjoint, subject roles are balanced across an epoch, and each role receives as close to a 50/50 class split as its number of selected trials permits. Selected trials are kept intact so window-level inputs never split a trial.

For an even trials_per_subject, every subject contributes exactly half class-0 and half class-1 trials. For an odd value, every subject contributes a one-trial class difference, and the extra class alternates across subjects so the complete meta-train and meta-test groups remain balanced whenever their total number of trials is even.

Parameters:
  • meta_train_subjects (int | None)

  • meta_test_subjects (int)

  • trials_per_subject (int)

  • steps_per_epoch (int | None)

  • seed (int | None)

on_epoch_end()[source]

Method called at the end of every epoch.

Return type:

None

class eegproc.deep_learning.domain_generalization.meta_learning.MetaLearningSubjectSequence(X, y, subject_ids, *, model, meta_train_subjects=6, meta_test_subjects=2, samples_per_subject=4, class_weight=None, seed=42)[source]

Bases: PyDataset

Yield subject-stratified, class-balanced first-order MLDG episodes.

Every item contains two disjoint groups sampled from the current fold’s gradient-training subjects:

meta_train (A)

Supplies the inner VC/classification, branch-reconstruction, and optional adversarial gradient.

meta_test (B)

Supplies the complete VC emotion gradient, without reconstruction, after the model has been moved to temporary fast weights from A.

Subject IDs are remapped once over the complete fold-local training pool so A and B use one consistent subject-class vocabulary. The true LOSO test subject and any validation subjects are absent because this sequence is constructed only from X_fit_train.

Parameters:
  • X (ndarray)

  • y (ndarray)

  • subject_ids (ndarray)

  • model (Model)

  • meta_train_subjects (int, default: 6)

  • meta_test_subjects (int, default: 2)

  • samples_per_subject (int, default: 4)

  • class_weight (dict[int, float] | None, default: None)

  • seed (int | None, default: 42)

on_epoch_end()[source]

Method called at the end of every epoch.

Return type:

None

eegproc.deep_learning.domain_generalization.meta_learning.fit_mldg(model, *, x, y, epochs, verbose, callbacks, validation_split, validation_data, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, **kwargs)[source]

Adapt ordinary source arrays into complete MLDG episodes.

eegproc.deep_learning.domain_generalization.meta_learning.run_mldg_train_step(model, x, y_flat, sample_weight)[source]

Execute the first-order MLDG update outside the model definition.

Return type:

None