DDCTrainer¶
- class torcheeg.trainers.DDCTrainer(extractor: Module, classifier: Module, num_classes: int, lr: float = 0.0001, weight_decay: float = 0.0, weight_domain: float = 1.0, weight_scheduler: bool = True, lr_scheduler_gamma: float = 0.0, lr_scheduler_decay: float = 0.75, warmup_epochs: int = 0, devices: int = 1, accelerator: str = 'cpu', metrics: List[str] = ['accuracy'])[source][source]¶
The individual differences and nonstationary of EEG signals make it difficult for deep learning models trained on the training set of subjects to correctly classify test samples from unseen subjects, since the training set and test set come from different data distributions. Domain adaptation is used to address the problem of distribution drift between training and test sets and thus achieves good performance in subject-independent (cross-subject) scenarios. This class supports the implementation of Deep Domain Confusion (DDC) for deep domain adaptation.
NOTE: DDC belongs to unsupervised domain adaptation methods, which only use labeled source and unlabeled target data. This means that the target dataset does not have to return labels.
Paper: Tzeng E, Hoffman J, Zhang N, et al. Deep domain confusion: Maximizing for domain invariance[J]. arXiv preprint arXiv:1412.3474, 2014.
Related Project: https://github.com/syorami/DDC-transfer-learning/blob/master/DDC.py
from torcheeg.models import CCNN from torcheeg.trainers import DDCTrainer class Extractor(CCNN): def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.conv3(x) x = self.conv4(x) x = x.flatten(start_dim=1) return x class Classifier(CCNN): def forward(self, x): x = self.lin1(x) x = self.lin2(x) return x extractor = Extractor(in_channels=5, num_classes=3) classifier = Classifier(in_channels=5, num_classes=3) trainer = DDCTrainer(extractor, classifier, num_classes=3, devices=1, weight_domain=1.0, accelerator='gpu')
- Parameters:
extractor (nn.Module) – The feature extraction model learns the feature representation of the EEG signal by forcing the correlation matrixes of source and target data to be close.
classifier (nn.Module) – The classification model learns the classification task with the source labeled data based on the feature of the feature extraction model. The dimension of its output should be equal to the number of categories in the dataset. The output layer does not need to have a softmax activation function.
num_classes (int, optional) – The number of categories in the dataset. (default:
None
)lr (float) – The learning rate. (default:
0.0001
)weight_decay (float) – The weight decay. (default:
0.0
)weight_domain (float) – The weight of the DDC loss. (default:
1.0
)weight_scheduler (bool) – Whether to use a scheduler for the weight of the DDC loss, growing from 0 to 1 following the schedule from the DDCN paper. (default:
False
)lr_scheduler (bool) – Whether to use a scheduler for the learning rate, as defined in the DDCN paper. (default:
False
)warmup_epochs (int) – The number of epochs for the warmup phase, during which the weight of the DDC loss is 0. (default:
0
)devices (int) – The number of devices to use. (default:
1
)accelerator (str) – The accelerator to use. Available options are: ‘cpu’, ‘gpu’. (default:
"cpu"
)metrics (list of str) – The metrics to use. Available options are: ‘precision’, ‘recall’, ‘f1score’, ‘accuracy’, ‘matthews’, ‘auroc’, and ‘kappa’. (default:
["accuracy"]
)
- fit(source_loader: DataLoader, target_loader: DataLoader, val_loader: DataLoader, max_epochs: int = 300, *args, **kwargs)[source]¶
- Parameters:
source_loader (DataLoader) – Iterable DataLoader for traversing the data batch from the source domain (
torch.utils.data.dataloader.DataLoader
,torch_geometric.loader.DataLoader
, etc).target_loader (DataLoader) – Iterable DataLoader for traversing the training data batch from the target domain (
torch.utils.data.dataloader.DataLoader
,torch_geometric.loader.DataLoader
, etc). The target dataset does not have to return labels.val_loader (DataLoader) – Iterable DataLoader for traversing the validation data batch (
torch.utils.data.dataloader.DataLoader
,torch_geometric.loader.DataLoader
, etc).max_epochs (int) – The maximum number of epochs to train. (default:
300
)