You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"\n# Training on recordings with different channels (heterogeneous montages)\n\nEEG datasets often do not share the same channels: recordings come from\ndifferent caps, montages, or vendors. Stacking such recordings into a single\ntraining batch is normally impossible -- the signal tensors have different\nnumbers of channels and cannot be concatenated.\n\nThis example shows braindecode's tools for training a **position-aware** model\nacross recordings with different channel sets:\n\n* :meth:`~braindecode.datasets.BaseConcatDataset.set_return_ch_pos` makes every\n window carry its electrode positions ``(n_ch, 3)`` (x, y, z), so channel\n identity is encoded by *where* each electrode sits rather than by its row\n index.\n* :func:`~braindecode.datasets.pad_channels_collate` pads each batch to the\n largest channel count present and returns a boolean ``ch_mask`` marking the\n real (non-padded) channels.\n\nA model that consumes positions (and ignores padded channels via the mask) can\nthen train on the mixed collection. Here we use a tiny illustrative model;\nswap in any position-aware architecture (e.g. ``REVE``).\n :depth: 2\n"
"## Build two recordings with different channel sets\n\nWe synthesise two short recordings: one with 8 channels and one with 10, both\nusing ``standard_1020`` electrode positions. In practice these would be real\ndatasets loaded with different montages.\n\n"
"## Window the recordings and enable channel positions\n\nAfter cutting fixed-length windows we assign the per-recording ``label`` as\nthe target and turn on position returning for the whole collection.\n\n"
"## Inspect a heterogeneous batch\n\n``pad_channels_collate`` pads each batch to the largest channel count (10\nhere) and returns a boolean ``ch_mask``. Because we mix 8- and 10-channel\nrecordings, batches contain both -- the mask tells real channels from padding.\n\n"
"## A minimal position-aware model\n\nThis tiny model is permutation-invariant over channels: it embeds each\nchannel from a small signal summary plus its (x, y, z) position, then\n**masked-mean-pools** over channels so padded channels are ignored. Any model\nthat accepts ``forward(x, pos=None, ch_mask=None)`` works the same way -- this\nis the signature braindecode routes positions and the mask into.\n\n"
80
+
]
81
+
},
82
+
{
83
+
"cell_type": "code",
84
+
"execution_count": null,
85
+
"metadata": {
86
+
"collapsed": false
87
+
},
88
+
"outputs": [],
89
+
"source": [
90
+
"class TinyPositionalNet(nn.Module):\n def __init__(self, n_outputs=2, dim=32):\n super().__init__()\n self.embed = nn.Sequential(nn.Linear(6, dim), nn.ReLU(), nn.Linear(dim, dim))\n self.head = nn.Linear(dim, n_outputs)\n\n def forward(self, x, pos=None, ch_mask=None):\n # Per-channel signal summary: mean, std, max over time -> (B, C, 3).\n feat = torch.stack([x.mean(-1), x.std(-1), x.amax(-1)], dim=-1)\n if pos is None:\n pos = torch.zeros_like(feat)\n h = self.embed(torch.cat([feat, pos], dim=-1)) # (B, C, dim)\n if ch_mask is not None:\n m = ch_mask.unsqueeze(-1).float()\n h = (h * m).sum(1) / m.sum(1).clamp(min=1) # masked mean over channels\n else:\n h = h.mean(1)\n return self.head(h)"
91
+
]
92
+
},
93
+
{
94
+
"cell_type": "markdown",
95
+
"metadata": {},
96
+
"source": [
97
+
"## Train with EEGClassifier\n\nWe pass ``pad_channels_collate`` as the iterator's ``collate_fn``. braindecode\nroutes the signal, positions and mask into the model's ``forward`` for you.\n\n"
"## Next steps\n\nThe data layer here makes a heterogeneous collection **batchable** and feeds\npositions plus a channel mask to the model. How a model *normalizes* and\nconsumes variable channels (e.g. applying ``ch_mask`` inside attention, or\nmapping electrode positions to a canonical space) is model-specific and the\nnatural next step when adapting a real architecture such as ``REVE``.\n\n"
0 commit comments