<filename>train_utils.py<gh_stars>0
import torch


def save_ckp(epoch, model, optimizer, lr_scheduler, train_loss, val_loss, cider, path):
    """save training checkpoint"""

    torch.save({
        'epoch': epoch,
        'model_state_dict': model.state_dict(),
        'optimizer_state_dict': optimizer.state_dict(),
        'lr_scheduler_state_dict': lr_scheduler.state_dict(),
        'train_loss': train_loss,
        'val_loss': val_loss,
        'cider': cider,
    }, path)


def load_ckp(model, optimizer, lr_scheduler, path):
    """load training checkpoint"""

    checkpoint = torch.load(path)

    model.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    lr_scheduler.load_state_dict(checkpoint['lr_scheduler_state_dict'])

    epoch = checkpoint['epoch']
    train_loss = checkpoint['train_loss']
    val_loss = checkpoint['val_loss']
    cider = checkpoint['cider']

    return epoch, model, optimizer, lr_scheduler, train_loss, val_loss, cider

<filename>train_utils.py<gh_stars>0
import torch


def save_ckp(epoch, model, optimizer, lr_scheduler, train_loss, val_loss, cider, path):
    """save training checkpoint"""

    torch.save({
        'epoch': epoch,
        'model_state_dict': model.state_dict(),
        'optimizer_state_dict': optimizer.state_dict(),
        'lr_scheduler_state_dict': lr_scheduler.state_dict(),
        'train_loss': train_loss,
        'val_loss': val_loss,
        'cider': cider,
    }, path)


def load_ckp(model, optimizer, lr_scheduler, path):
    """load training checkpoint"""

    checkpoint = torch.load(path)

    model.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    lr_scheduler.load_state_dict(checkpoint['lr_scheduler_state_dict'])

    epoch = checkpoint['epoch']
    train_loss = checkpoint['train_loss']
    val_loss = checkpoint['val_loss']
    cider = checkpoint['cider']

    return epoch, model, optimizer, lr_scheduler, train_loss, val_loss, cider

save training checkpoint load training checkpoint