def train_classifier(self, max_epochs, train_dataloader, validation_dataloader, comparison): epochs = [] train_losses = [] validation_accs = [] early_stopping = EarlyStopping('{}/{}_classifier.pt'.format( self.model_name, self.dataset_name)) for epoch in range(max_epochs): if early_stopping.early_stop: break for batch_idx, (data, labels) in enumerate(train_dataloader): self.Classifier.train() data = data.to(self.device) labels = labels.to(self.device) self.Classifier_optim.zero_grad() preds = self.Classifier(data) loss = self.Classifier_criterion(preds, labels) loss.backward() self.Classifier_optim.step() if comparison: epochs.append(epoch) train_losses.append(loss.item()) validation_accs.append( accuracy(self.Classifier, validation_dataloader, self.device)) val = accuracy(self.Classifier, validation_dataloader, self.device) print('Supervised Epoch: {} Validation acc: {}'.format(epoch, val)) early_stopping(1 - val, self.Classifier) if early_stopping.early_stop: early_stopping.load_checkpoint(self.Classifier) return epochs, train_losses, validation_accs
def train_classifier(self, max_epochs, train_dataloader, validation_dataloader): epochs = [] train_losses = [] validation_accs = [] early_stopping = EarlyStopping('{}/{}_inner.pt'.format( self.state_path, self.model_name)) for epoch in range(max_epochs): if early_stopping.early_stop: break train_loss = 0 for batch_idx, (data, labels) in enumerate(train_dataloader): self.SDAEClassifier.train() data = data.to(self.device) labels = labels.to(self.device) self.optimizer.zero_grad() predictions = self.SDAEClassifier(data) loss = self.criterion(predictions, labels) loss.backward() self.optimizer.step() train_loss += loss.item() if validation_dataloader is not None: acc = accuracy(self.SDAEClassifier, validation_dataloader, self.device) validation_accs.append(acc) early_stopping(1 - acc, self.SDAEClassifier) epochs.append(epoch) train_losses.append(train_loss / len(train_dataloader)) early_stopping.load_checkpoint(self.SDAEClassifier) return epochs, train_losses, validation_accs
def full_train(self, unsupervised_dataset, train_dataset, validation_dataset): self.reset_model() # TODO: don't use arbitrary values for batch size unsupervised_dataloader = DataLoader(dataset=unsupervised_dataset, batch_size=180, shuffle=True) supervised_dataloader = DataLoader(dataset=train_dataset, batch_size=20, shuffle=True) validation_dataloader = DataLoader( dataset=validation_dataset, batch_size=validation_dataset.__len__()) for epoch in range(50): self.train_one_epoch(epoch, supervised_dataloader, unsupervised_dataloader) print('Epoch: {} Validation Acc: {}'.format( epoch, trainingutils.accuracy(self.D, validation_dataloader, self.device)))
def test_model(self, test_dataloader): return accuracy(self.SDAEClassifier, test_dataloader, self.device)
def full_test(self, test_dataset): test_dataloader = DataLoader(dataset=test_dataset, batch_size=test_dataset.__len__()) return trainingutils.accuracy(self.D, test_dataloader, self.device)