2020-04-16 02:16:40 +00:00
|
|
|
from abc import ABC
|
|
|
|
from collections import OrderedDict
|
2020-08-16 01:45:41 +00:00
|
|
|
from pytorch_lightning.core.step_result import EvalResult
|
2020-04-22 00:33:10 +00:00
|
|
|
|
2020-04-16 02:16:40 +00:00
|
|
|
import torch
|
|
|
|
|
|
|
|
|
|
|
|
class ValidationStepVariations(ABC):
|
|
|
|
"""
|
|
|
|
Houses all variations of validation steps
|
|
|
|
"""
|
|
|
|
def validation_step(self, batch, batch_idx, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Lightning calls this inside the validation loop
|
|
|
|
:param batch:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
y_hat = self(x)
|
|
|
|
|
|
|
|
loss_val = self.loss(y, y_hat)
|
|
|
|
|
|
|
|
# acc
|
|
|
|
labels_hat = torch.argmax(y_hat, dim=1)
|
|
|
|
val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
|
2020-05-31 12:29:51 +00:00
|
|
|
val_acc = torch.tensor(val_acc).type_as(x)
|
2020-04-16 02:16:40 +00:00
|
|
|
|
2020-05-31 12:29:51 +00:00
|
|
|
output = OrderedDict({
|
|
|
|
'val_loss': loss_val,
|
|
|
|
'val_acc': val_acc,
|
|
|
|
'test_dic': {'val_loss_a': loss_val}
|
|
|
|
})
|
|
|
|
return output
|
2020-04-16 02:16:40 +00:00
|
|
|
|
2020-08-16 01:45:41 +00:00
|
|
|
def validation_step_result_obj(self, batch, batch_idx, *args, **kwargs):
|
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
y_hat = self(x)
|
|
|
|
|
|
|
|
loss_val = self.loss(y, y_hat)
|
|
|
|
|
|
|
|
# acc
|
|
|
|
labels_hat = torch.argmax(y_hat, dim=1)
|
|
|
|
val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
|
|
|
|
val_acc = torch.tensor(val_acc).type_as(x)
|
|
|
|
|
|
|
|
result = EvalResult(checkpoint_on=loss_val, early_stop_on=loss_val)
|
|
|
|
result.log_dict({
|
|
|
|
'val_loss': loss_val,
|
|
|
|
'val_acc': val_acc,
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
|
2020-08-17 14:29:39 +00:00
|
|
|
def validation_step_result_obj_dp(self, batch, batch_idx, *args, **kwargs):
|
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
y_hat = self(x.to(self.device))
|
|
|
|
|
|
|
|
y = y.to(y_hat.device)
|
|
|
|
loss_val = self.loss(y, y_hat)
|
|
|
|
|
|
|
|
# acc
|
|
|
|
labels_hat = torch.argmax(y_hat, dim=1)
|
|
|
|
val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
|
|
|
|
val_acc = torch.tensor(val_acc).type_as(x)
|
|
|
|
|
|
|
|
result = EvalResult(checkpoint_on=loss_val, early_stop_on=loss_val)
|
|
|
|
result.log_dict({
|
|
|
|
'val_loss': loss_val,
|
|
|
|
'val_acc': val_acc,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.validation_step_called = True
|
|
|
|
return result
|
|
|
|
|
2020-05-02 12:38:22 +00:00
|
|
|
def validation_step__multiple_dataloaders(self, batch, batch_idx, dataloader_idx, **kwargs):
|
2020-04-16 02:16:40 +00:00
|
|
|
"""
|
|
|
|
Lightning calls this inside the validation loop
|
|
|
|
:param batch:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
x, y = batch
|
|
|
|
x = x.view(x.size(0), -1)
|
|
|
|
y_hat = self(x)
|
|
|
|
|
|
|
|
loss_val = self.loss(y, y_hat)
|
|
|
|
|
|
|
|
# acc
|
|
|
|
labels_hat = torch.argmax(y_hat, dim=1)
|
|
|
|
val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
|
2020-05-31 12:29:51 +00:00
|
|
|
val_acc = torch.tensor(val_acc).type_as(x)
|
2020-04-16 02:16:40 +00:00
|
|
|
|
2020-05-31 12:29:51 +00:00
|
|
|
output = OrderedDict({
|
|
|
|
f'val_loss_{dataloader_idx}': loss_val,
|
|
|
|
f'val_acc_{dataloader_idx}': val_acc,
|
|
|
|
})
|
|
|
|
return output
|