Skip to content

Conversation

amitsubhashchejara
Copy link

This pull request is to merge a new experiment torch-lightning-experiment for the issue 195. The new class TorchExperiment 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 a LightningModule, DataModule, Trainer, and an objective_matric with default value val_loss. The _evaluate function internally performs a training run and returns a score of the objective_matric as a numpy.float64.

Fixes #195

"""

model = self.lightning_module(**params)
self.trainer.fit(model, self.datamodule)
Copy link
Owner

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?

Copy link
Author

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):
Copy link
Owner

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
Copy link
Owner

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

Copy link
Author

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")
Copy link
Owner

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'))
Copy link
Owner

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?

Copy link
Owner

@SimonBlanke SimonBlanke left a 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)
"""

Copy link
Owner

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.

Copy link
Author

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.

Copy link
Collaborator

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
Copy link
Owner

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.

Copy link
Author

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
Copy link
Owner

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.

@SimonBlanke
Copy link
Owner

@amitsubhashchejara Thanks for opening this PR. There is still some work to do, but I am sure you can do this.

@amitsubhashchejara
Copy link
Author

Sure, I'll make the changes!

Copy link
Collaborator

@fkiraly fkiraly left a 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 the pyproject depset all_extras
  • add "lightning" to the tag python_dependencies of the new experiment
  • make sure no lightning or torch imports happen outside the class

@amitsubhashchejara
Copy link
Author

I've pushed some changes per your suggestions.

"objective_metric": "val_loss",
}

return params
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH] torch / lightning integration

3 participants