JANTrainer¶
- class torcheeg.trainers.JANTrainer(extractor: Module, classifier: Module, num_classes: int, lr: float = 0.0001, weight_decay: float = 0.0, mul_kernel=(2.0, 2.0), num_kernel=(5, 1), 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]¶
This class supports the implementation of Joint Adaptation Networks (JAN) for deep domain adaptation.
NOTE: JAN belongs to unsupervised domain adaptation methods, which only use labeled source data and unlabeled target data. This means that the target dataset does not have to contain labels.
Paper: Long M, Zhu H, Wang J, et al. Deep transfer learning with joint adaptation networks[C]//International conference on machine learning. PMLR, 2017: 2208-2217.
Related Project: https://github.com/criteo-research/pytorch-ada
from torcheeg.models import CCNN from torcheeg.trainers import JANTrainer 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 = JANTrainer(extractor, classifier, num_classes=3, devices=1, weight_visit=0.6, 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
)mul_kernel (tuple of float) – The kernel multiplier for the Gaussian kernel. (default:
(2.0, 2.0)
)num_kernel (tuple of int) – The number of kernels for the Gaussian kernel. (default:
(5, 1)
)weight_domain (float) – The weight of the associative loss (default:
1.0
)lr_scheduler (bool) – Whether to use a scheduler for the weight of the associative loss, growing from 0 to 1 following the schedule from the DANN paper. (default:
False
)lr_scheduler – Whether to use a scheduler for the learning rate, as defined in the DANN paper. (default:
False
)warmup_epochs (int) – The number of epochs for the warmup phase, during which the weight of the associative 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
)