mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-10 12:21:57 +08:00
hparams as dict [blocked by 1041] (#1029)
* hparams as dict * hparams as dict * fixing * fixing * fixing * fixing * typing * typing * chnagelog * update set hparams * use setter * simplify * chnagelog * imports * pylint * typing * Update training_io.py * Update training_io.py * Update lightning.py * Update test_trainer.py * Update __init__.py * Update base.py * Update utils.py * Update test_trainer.py * Update training_io.py * Update test_trainer.py * Update test_trainer.py * Update test_trainer.py * Update test_trainer.py * Update callback_config.py * Update callback_config.py * Update test_trainer.py Co-authored-by: William Falcon <waf2107@columbia.edu>
This commit is contained in:
co-authored by
William Falcon
parent
6a39573267
commit
e586ed4767
@@ -3,30 +3,28 @@ import math
|
||||
import os
|
||||
import pytest
|
||||
import torch
|
||||
import argparse
|
||||
from argparse import ArgumentParser, Namespace
|
||||
|
||||
import tests.models.utils as tutils
|
||||
from unittest import mock
|
||||
from pytorch_lightning import Trainer
|
||||
from pytorch_lightning import Trainer, LightningModule
|
||||
from pytorch_lightning.callbacks import (
|
||||
EarlyStopping,
|
||||
ModelCheckpoint,
|
||||
)
|
||||
from tests.models import (
|
||||
TestModelBase,
|
||||
DictHparamsModel,
|
||||
LightningTestModel,
|
||||
LightEmptyTestStep,
|
||||
LightValidationStepMixin,
|
||||
LightValidationMultipleDataloadersMixin,
|
||||
LightTrainDataloader,
|
||||
LightTestDataloader,
|
||||
LightValidationMixin,
|
||||
LightTestMixin
|
||||
)
|
||||
from pytorch_lightning.core.lightning import load_hparams_from_tags_csv
|
||||
from pytorch_lightning.trainer.logging import TrainerLoggingMixin
|
||||
from pytorch_lightning.utilities.debugging import MisconfigurationException
|
||||
from pytorch_lightning import Callback
|
||||
|
||||
|
||||
def test_no_val_module(tmpdir):
|
||||
@@ -128,7 +126,7 @@ def test_gradient_accumulation_scheduling(tmpdir):
|
||||
assert Trainer(accumulate_grad_batches={1: 2.5, 3: 5})
|
||||
|
||||
# test optimizer call freq matches scheduler
|
||||
def optimizer_step(self, epoch, batch_idx, optimizer, optimizer_idx, second_order_closure=None):
|
||||
def _optimizer_step(self, epoch, batch_idx, optimizer, optimizer_idx, second_order_closure=None):
|
||||
# only test the first 12 batches in epoch
|
||||
if batch_idx < 12:
|
||||
if epoch == 0:
|
||||
@@ -179,7 +177,7 @@ def test_gradient_accumulation_scheduling(tmpdir):
|
||||
default_save_path=tmpdir)
|
||||
|
||||
# for the test
|
||||
trainer.optimizer_step = optimizer_step
|
||||
trainer.optimizer_step = _optimizer_step
|
||||
model.prev_called_batch_idx = 0
|
||||
|
||||
trainer.fit(model)
|
||||
@@ -188,7 +186,6 @@ def test_gradient_accumulation_scheduling(tmpdir):
|
||||
def test_loading_meta_tags(tmpdir):
|
||||
tutils.reset_seed()
|
||||
|
||||
from argparse import Namespace
|
||||
hparams = tutils.get_hparams()
|
||||
|
||||
# save tags
|
||||
@@ -604,8 +601,9 @@ def test_testpass_overrides(tmpdir):
|
||||
model = LightningTestModel(hparams)
|
||||
Trainer().test(model)
|
||||
|
||||
|
||||
@mock.patch('argparse.ArgumentParser.parse_args',
|
||||
return_value=argparse.Namespace(**Trainer.default_attributes()))
|
||||
return_value=Namespace(**Trainer.default_attributes()))
|
||||
def test_default_args(tmpdir):
|
||||
"""Tests default argument parser for Trainer"""
|
||||
tutils.reset_seed()
|
||||
@@ -613,7 +611,7 @@ def test_default_args(tmpdir):
|
||||
# logger file to get meta
|
||||
logger = tutils.get_test_tube_logger(tmpdir, False)
|
||||
|
||||
parser = argparse.ArgumentParser(add_help=False)
|
||||
parser = ArgumentParser(add_help=False)
|
||||
args = parser.parse_args()
|
||||
args.logger = logger
|
||||
|
||||
@@ -622,3 +620,25 @@ def test_default_args(tmpdir):
|
||||
|
||||
assert isinstance(trainer, Trainer)
|
||||
assert trainer.max_epochs == 5
|
||||
|
||||
|
||||
def test_hparams_save_load(tmpdir):
|
||||
model = DictHparamsModel({'in_features': 28 * 28, 'out_features': 10})
|
||||
|
||||
# logger file to get meta
|
||||
trainer_options = dict(
|
||||
default_save_path=tmpdir,
|
||||
max_epochs=2,
|
||||
)
|
||||
|
||||
# fit model
|
||||
trainer = Trainer(**trainer_options)
|
||||
result = trainer.fit(model)
|
||||
|
||||
assert result == 1
|
||||
|
||||
# try to load the model now
|
||||
pretrained_model = tutils.load_model_from_checkpoint(
|
||||
trainer.checkpoint_callback.dirpath,
|
||||
module_class=DictHparamsModel
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user