def validation_step(self, batch, batch_idx): if self.dataset_name == 'stl10': labeled_batch = batch[1] unlabeled_batch = batch[0] batch = unlabeled_batch (img_1, img_2), y = batch h1, z1 = self.forward(img_1) h2, z2 = self.forward(img_2) loss = self.loss_func(z1, z2, self.temp) result = {'val_loss': loss} if self.online_evaluator: if self.dataset_name == 'stl10': (img_1, img_2), y = labeled_batch h1, z1 = self.forward(img_1) z_in = z1.reshape(z1.size(0), -1) mlp_preds = self.non_linear_evaluator(z_in) mlp_loss = F.cross_entropy(mlp_preds, y) acc = metrics.accuracy(mlp_preds, y) result['mlp_acc'] = acc result['mlp_loss'] = mlp_loss return result
def validation_step(self, batch, batch_nb): # in STL10 we pass in both lab+unl for online ft if self.hparams.dataset == 'stl10': labeled_batch = batch[1] unlabeled_batch = batch[0] batch = unlabeled_batch img_1, y = batch # generate features # Latent features Z = self(img_1) # infoNCE loss nce_loss = self.info_nce(Z) result = {'val_nce': nce_loss} if self.online_evaluator: if self.hparams.dataset == 'stl10': img_1, y = labeled_batch Z = self(img_1) z_in = Z.reshape(Z.size(0), -1) mlp_preds = self.non_linear_evaluator(z_in) mlp_loss = F.cross_entropy(mlp_preds, y) acc = metrics.accuracy(mlp_preds, y) result['mlp_acc'] = acc result['mlp_loss'] = mlp_loss return result
def validation_step(self, batch, batch_idx): if isinstance(self.datamodule, STL10DataModule): labeled_batch = batch[1] unlabeled_batch = batch[0] batch = unlabeled_batch (img_1, img_2), y = batch h1, z1 = self.forward(img_1) h2, z2 = self.forward(img_2) loss = self.loss_func(z1, z2, self.hparams.loss_temperature) result = {'val_loss': loss} if self.online_evaluator: if isinstance(self.datamodule, STL10DataModule): (img_1, img_2), y = labeled_batch h1, z1 = self.forward(img_1) z_in = z1.reshape(z1.size(0), -1) mlp_preds = self.non_linear_evaluator(z_in) mlp_loss = F.cross_entropy(mlp_preds, y) acc = metrics.accuracy(mlp_preds, y) result['mlp_acc'] = acc result['mlp_loss'] = mlp_loss return result
def test_step(self, batch, batch_idx): img_1, y = batch Z = self(img_1) z_in = Z.reshape(Z.size(0), -1) mlp_preds = self.non_linear_evaluator(z_in) mlp_loss = F.cross_entropy(mlp_preds, y) acc = metrics.accuracy(mlp_preds, y) return {'acc': acc, 'loss': mlp_loss}