lightning/pl_examples/basic_examples/cpu_template.py

55 lines
1.3 KiB
Python
Raw Normal View History

"""
Runs a model on the CPU on a single node.
"""
import os
from argparse import ArgumentParser
import numpy as np
import torch
import pytorch_lightning as pl
from pl_examples.models.lightning_template import LightningTemplateModel
SEED = 2334
torch.manual_seed(SEED)
np.random.seed(SEED)
def main(hparams):
"""
Main training routine specific for this project
:param hparams:
"""
# ------------------------
# 1 INIT LIGHTNING MODEL
# ------------------------
model = LightningTemplateModel(hparams)
# ------------------------
# 2 INIT TRAINER
# ------------------------
trainer = pl.Trainer(max_epochs=hparams.epochs)
# ------------------------
2019-10-05 18:13:32 +00:00
# 3 START TRAINING
# ------------------------
trainer.fit(model)
if __name__ == '__main__':
2019-10-05 18:13:32 +00:00
# ------------------------
# TRAINING ARGUMENTS
# ------------------------
# these are project-wide arguments
root_dir = os.path.dirname(os.path.realpath(__file__))
2019-10-05 18:13:32 +00:00
parent_parser = ArgumentParser(add_help=False)
2019-10-05 18:13:32 +00:00
# each LightningModule defines arguments relevant to it
parser = LightningTemplateModel.add_model_specific_args(parent_parser, root_dir)
hyperparams = parser.parse_args()
# ---------------------
# RUN TRAINING
# ---------------------
main(hyperparams)