How to save a model in PyTorch?
Say we have a model that we can validate like this and that Model inherits torch.nn.Module
def validate(logger, config, valid_loader, model, criterion, epoch, main_proc):
    meters = AverageMeterGroup()
    model.eval()

    with torch.no_grad():
        for step, (x, y) in enumerate(valid_loader):
            x, y = x.cuda(non_blocking=True), y.cuda(non_blocking=True)
            logits, _ = model(x)
            loss = criterion(logits, y)
            prec1, prec5 = utils.accuracy(logits, y, topk=(1, 5))
            metrics = {&quot;prec1&quot;: prec1, &quot;prec5&quot;: prec5, &quot;loss&quot;: loss}
            metrics = utils.reduce_metrics(metrics, config.distributed)
            meters.update(metrics)

            if main_proc and (step % config.log_frequency == 0 or step + 1 == len(valid_loader)):
                logger.info(&quot;Epoch [%d/%d] Step [%d/%d]  %s&quot;, epoch + 1, config.epochs, step + 1, len(valid_loader), meters)

    if main_proc:
        logger.info(&quot;Train: [%d/%d] Final Prec@1 %.4f Prec@5 %.4f&quot;, epoch + 1, config.epochs, meters.prec1.avg, meters.prec5.avg)
    return meters.prec1.avg, meters.prec5.avg

How to change validation procedure in order to save model after validation into file system so that it would be runnable on other data?

with torch.no_grad():
    for step, (x, y) in enumerate(valid_loader):
        x, y = x.cuda(non_blocking=True), y.cuda(non_blocking=True)
        logits, _ = model(x)
        loss = criterion(logits, y)
        prec1, prec5 = utils.accuracy(logits, y, topk=(1, 5))
        metrics = {&quot;prec1&quot;: prec1, &quot;prec5&quot;: prec5, &quot;loss&quot;: loss}
        metrics = utils.reduce_metrics(metrics, config.distributed)
        meters.update(metrics)

        if main_proc and (step % config.log_frequency == 0 or step + 1 == len(valid_loader)):
            logger.info(&quot;Epoch [%d/%d] Step [%d/%d]  %s&quot;, epoch + 1, config.epochs, step + 1, len(valid_loader), meters)

torch.save(model,'model'+str(epoch)+'.pt')
if main_proc:
    logger.info(&quot;Train: [%d/%d] Final Prec@1 %.4f Prec@5 %.4f&quot;, epoch + 1, config.epochs, meters.prec1.avg, meters.prec5.avg)
return meters.prec1.avg, meters.prec5.avg

