MCPcopy Create free account
hub / github.com/BioinfoMachineLearning/FlowDock / log_hyperparameters

Function log_hyperparameters

flowdock/utils/logging_utils.py:14–59  ·  view source on GitHub ↗

Controls which config parts are saved by Lightning loggers. Additionally saves: - Number of model parameters :param object_dict: A dictionary containing the following objects: - `"cfg"`: A DictConfig object containing the main config. - `"model"`: The Lightning mode

(object_dict: Dict[str, Any])

Source from the content-addressed store, hash-verified

12
13@rank_zero_only
14def log_hyperparameters(object_dict: Dict[str, Any]) -> None:
15 """Controls which config parts are saved by Lightning loggers.
16
17 Additionally saves:
18 - Number of model parameters
19
20 :param object_dict: A dictionary containing the following objects:
21 - `"cfg"`: A DictConfig object containing the main config.
22 - `"model"`: The Lightning model.
23 - `"trainer"`: The Lightning trainer.
24 """
25 hparams = {}
26
27 cfg = OmegaConf.to_container(object_dict["cfg"])
28 model = object_dict["model"]
29 trainer = object_dict["trainer"]
30
31 if not trainer.logger:
32 log.warning("Logger not found! Skipping hyperparameter logging...")
33 return
34
35 hparams["model"] = cfg["model"]
36
37 # save number of model parameters
38 hparams["model/params/total"] = sum(p.numel() for p in model.parameters())
39 hparams["model/params/trainable"] = sum(
40 p.numel() for p in model.parameters() if p.requires_grad
41 )
42 hparams["model/params/non_trainable"] = sum(
43 p.numel() for p in model.parameters() if not p.requires_grad
44 )
45
46 hparams["data"] = cfg["data"]
47 hparams["trainer"] = cfg["trainer"]
48
49 hparams["callbacks"] = cfg.get("callbacks")
50 hparams["extras"] = cfg.get("extras")
51
52 hparams["task_name"] = cfg.get("task_name")
53 hparams["tags"] = cfg.get("tags")
54 hparams["ckpt_path"] = cfg.get("ckpt_path")
55 hparams["seed"] = cfg.get("seed")
56
57 # send hparams to all loggers
58 for logger in trainer.loggers:
59 logger.log_hyperparams(hparams)

Callers 3

evaluateFunction · 0.90
trainFunction · 0.90
sampleFunction · 0.90

Calls 1

getMethod · 0.45

Tested by

no test coverage detected