-
Notifications
You must be signed in to change notification settings - Fork 51
Add PyTorch Lightning experiment adapter (TorchExperiment) #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add PyTorch Lightning experiment adapter (TorchExperiment) #201
Conversation
src/hyperactive/experiment/integrations/torch_lightning_experiment.py
Outdated
Show resolved
Hide resolved
""" | ||
|
||
model = self.lightning_module(**params) | ||
self.trainer.fit(model, self.datamodule) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the trainer designed to be fitted multiple/many times like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, I'll modify this to create a new trainer instance for every run!
return params | ||
|
||
@classmethod | ||
def _get_score_params(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self -> cls
else: | ||
val_results = self.trainer.callback_metrics.get(self.objective_metric, float('inf')) | ||
metadata = {} | ||
import numpy as np |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put this to the top of the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll address all the issues!
self.trainer.fit(model, self.datamodule) | ||
# get validation results | ||
if self.objective_metric not in self.trainer.callback_metrics: | ||
raise ValueError(f"objective metric {self.objective_metric} not found in trainer callback metrics") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is redundant. The get method already handles of objective_metric is not in objective_metrics.
if self.objective_metric not in self.trainer.callback_metrics: | ||
raise ValueError(f"objective metric {self.objective_metric} not found in trainer callback metrics") | ||
else: | ||
val_results = self.trainer.callback_metrics.get(self.objective_metric, float('inf')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
val_results -> val_result
It is one result, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deep-Learning frameworks often have very verbose logging behaviour per default. Since we run multiple trainings (in each iteration), this can get overwhelming in the command-line. Maybe we should address this.
Also answer the questions in the code and make necessary changes.
>>> | ||
>>> val_result, metadata = experiment._evaluate(params) | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the objective_metric is a loss the property:higher_or_lower_is_better in the _tags must be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was actually a bit confused about the usage of tags and the author credit. Can you help me with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re author credit, that is ["amitsubhashchejara"]
, i.e., your GitHub account name
|
||
Examples | ||
-------- | ||
>>> from hyperactive.experiment.integrations import TorchExperiment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assumes, that "TorchExperiment" is in the init of the integrations module, which it isn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll add TorchExperiment
to the __init__
of integrations module.
val_results = self.trainer.callback_metrics.get(self.objective_metric, float('inf')) | ||
metadata = {} | ||
import numpy as np | ||
return np.float64(val_results.detach().cpu().item()), metadata |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is val_results always a tensor? If not this would fail. If there is a possibility, that val_results another type then we should account for that.
@amitsubhashchejara Thanks for opening this PR. There is still some work to do, but I am sure you can do this. |
Sure, I'll make the changes! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests fail since lightning
is not installed, and the dependency is not explicit
- add
lightning
to thepyproject
depsetall_extras
- add
"lightning"
to the tagpython_dependencies
of the new experiment - make sure no
lightning
ortorch
imports happen outside the class
I've pushed some changes per your suggestions. |
"objective_metric": "val_loss", | ||
} | ||
|
||
return params |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the issue might be with this - try returning a list of dict
This pull request is to merge a new experiment
torch-lightning-experiment
for the issue195
. The new classTorchExperiment
is an experiment adapter for PyTorch Lightning experiments and is used to perform experiments using PyTorch Lightning modules. It allows for hyperparameter tuning and evaluation of the model's performance using specified metrics.The
TorchExperiment
class accepts aLightningModule
,DataModule
,Trainer
, and anobjective_matric
with default valueval_loss
. The_evaluate
function internally performs a training run and returns a score of theobjective_matric
as anumpy.float64
.Fixes #195