-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathtorch_lightning_experiment.py
More file actions
377 lines (324 loc) · 13.1 KB
/
torch_lightning_experiment.py
File metadata and controls
377 lines (324 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Experiment adapter for PyTorch Lightning experiments."""
# copyright: hyperactive developers, MIT License (see LICENSE file)
__author__ = ["amitsubhashchejara"]
import numpy as np
from hyperactive.base import BaseExperiment
class TorchExperiment(BaseExperiment):
"""Experiment adapter for PyTorch Lightning experiments.
This class 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 experiment trains a Lightning module with given hyperparameters and returns
the validation metric value for optimization.
Parameters
----------
data_module : type
A PyTorch Lightning DataModule class (not an instance) that
handles data loading and preparation. It will be instantiated
with hyperparameters during optimization.
lightning_module : type
A PyTorch Lightning Module class (not an instance) that will be instantiated
with hyperparameters during optimization.
trainer_kwargs : dict, optional (default=None)
A dictionary of keyword arguments to pass to the PyTorch Lightning Trainer.
dm_kwargs : dict, optional (default=None)
A dictionary of keyword arguments to pass to the Data Module upon instantiation.
objective_metric : str, optional (default='val_loss')
The metric used to evaluate the model's performance. This should correspond
to a metric logged in the LightningModule during validation.
Examples
--------
>>> from hyperactive.experiment.integrations import TorchExperiment
>>> import torch
>>> import lightning as L
>>> from torch import nn
>>> from torch.utils.data import DataLoader
>>>
>>> # Define a simple Lightning Module
>>> class SimpleLightningModule(L.LightningModule):
... def __init__(self, input_dim=10, hidden_dim=16, lr=1e-3):
... super().__init__()
... self.save_hyperparameters()
... self.model = nn.Sequential(
... nn.Linear(input_dim, hidden_dim),
... nn.ReLU(),
... nn.Linear(hidden_dim, 2)
... )
... self.lr = lr
...
... def forward(self, x):
... return self.model(x)
...
... def training_step(self, batch, batch_idx):
... x, y = batch
... y_hat = self(x)
... loss = nn.functional.cross_entropy(y_hat, y)
... self.log("train_loss", loss)
... return loss
...
... def validation_step(self, batch, batch_idx):
... x, y = batch
... y_hat = self(x)
... val_loss = nn.functional.cross_entropy(y_hat, y)
... self.log("val_loss", val_loss, on_epoch=True)
... return val_loss
...
... def configure_optimizers(self):
... return torch.optim.Adam(self.parameters(), lr=self.lr)
>>>
>>> # Create DataModule
>>> class RandomDataModule(L.LightningDataModule):
... def __init__(self, batch_size=32):
... super().__init__()
... self.batch_size = batch_size
...
... def setup(self, stage=None):
... dataset = torch.utils.data.TensorDataset(
... torch.randn(100, 10),
... torch.randint(0, 2, (100,))
... )
... self.train, self.val = torch.utils.data.random_split(
... dataset, [80, 20]
... )
...
... def train_dataloader(self):
... return DataLoader(self.train, batch_size=self.batch_size)
...
... def val_dataloader(self):
... return DataLoader(self.val, batch_size=self.batch_size)
>>>
>>> # Create Experiment
>>> experiment = TorchExperiment(
... data_module=RandomDataModule,
... lightning_module=SimpleLightningModule,
... trainer_kwargs={'max_epochs': 3},
... dm_kwargs={'batch_size': 16},
... objective_metric="val_loss"
... )
>>>
>>> params = {"input_dim": 10, "hidden_dim": 16, "lr": 1e-3}
>>>
>>> val_result, metadata = experiment._evaluate(params)
"""
_tags = {
"property:randomness": "random",
"property:higher_or_lower_is_better": "lower",
"authors": ["amitsubhashchejara"],
"python_dependencies": ["torch", "lightning"],
}
def __init__(
self,
data_module,
lightning_module,
trainer_kwargs=None,
dm_kwargs=None,
objective_metric: str = "val_loss",
):
self.data_module = data_module
self.lightning_module = lightning_module
self.trainer_kwargs = trainer_kwargs or {}
self.dm_kwargs = dm_kwargs or {}
self.objective_metric = objective_metric
super().__init__()
self._trainer_kwargs = {
"max_epochs": 10,
"enable_checkpointing": False,
"logger": False,
"enable_progress_bar": False,
"enable_model_summary": False,
}
if trainer_kwargs is not None:
self._trainer_kwargs.update(trainer_kwargs)
def _paramnames(self):
"""Return the parameter names of the search.
Returns
-------
list of str, or None
The parameter names of the search parameters.
If not known or arbitrary, return None.
"""
import inspect
sig = inspect.signature(self.lightning_module.__init__)
return [p for p in sig.parameters.keys() if p != "self"]
def _evaluate(self, params):
"""Evaluate the parameters.
Parameters
----------
params : dict with string keys
Parameters to evaluate.
Returns
-------
float
The value of the parameters as per evaluation.
dict
Additional metadata about the search.
"""
import lightning as L
try:
model = self.lightning_module(**params)
trainer = L.Trainer(**self._trainer_kwargs)
data = self.data_module(**self.dm_kwargs)
trainer.fit(model, data)
val_result = trainer.callback_metrics.get(self.objective_metric)
metadata = {}
if val_result is None:
available_metrics = list(trainer.callback_metrics.keys())
raise ValueError(
f"Metric '{self.objective_metric}' not found. "
f"Available: {available_metrics}"
)
if hasattr(val_result, "item"):
val_result = np.float64(val_result.detach().cpu().item())
elif isinstance(val_result, int | float):
val_result = np.float64(val_result)
else:
val_result = np.float64(float(val_result))
return val_result, metadata
except Exception as e:
print(f"Training failed with params {params}: {e}")
return np.float64(float("inf")), {}
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests.
Returns
-------
params : dict or list of dict, default = {}
Parameters to create testing instances of the class.
"""
import lightning as L
import torch
from torch import nn
from torch.utils.data import DataLoader
class SimpleLightningModule(L.LightningModule):
def __init__(self, input_dim=10, hidden_dim=16, lr=1e-3):
super().__init__()
self.save_hyperparameters()
self.model = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, 2),
)
self.lr = lr
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = nn.functional.cross_entropy(y_hat, y)
self.log("train_loss", loss)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
val_loss = nn.functional.cross_entropy(y_hat, y)
self.log("val_loss", val_loss, on_epoch=True)
return val_loss
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=self.lr)
class RandomDataModule(L.LightningDataModule):
def __init__(self, batch_size=32):
super().__init__()
self.batch_size = batch_size
def setup(self, stage=None):
dataset = torch.utils.data.TensorDataset(
torch.randn(100, 10), torch.randint(0, 2, (100,))
)
self.train, self.val = torch.utils.data.random_split(dataset, [80, 20])
def train_dataloader(self):
return DataLoader(self.train, batch_size=self.batch_size)
def val_dataloader(self):
return DataLoader(self.val, batch_size=self.batch_size)
params = {
"data_module": RandomDataModule,
"lightning_module": SimpleLightningModule,
"trainer_kwargs": {
"max_epochs": 1,
"enable_progress_bar": False,
"enable_model_summary": False,
"logger": False,
},
"dm_kwargs": {"batch_size": 16},
"objective_metric": "val_loss",
}
class RegressionModule(L.LightningModule):
def __init__(self, num_layers=2, hidden_size=32, dropout=0.1):
super().__init__()
self.save_hyperparameters()
layers = []
input_size = 20
for _ in range(num_layers):
layers.extend(
[
nn.Linear(input_size, hidden_size),
nn.ReLU(),
nn.Dropout(dropout),
]
)
input_size = hidden_size
layers.append(nn.Linear(hidden_size, 1))
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x).squeeze()
loss = nn.functional.mse_loss(y_hat, y)
self.log("train_loss", loss)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x).squeeze()
val_loss = nn.functional.mse_loss(y_hat, y)
self.log("val_loss", val_loss, on_epoch=True)
return val_loss
def configure_optimizers(self):
return torch.optim.SGD(self.parameters(), lr=0.01)
class RegressionDataModule(L.LightningDataModule):
def __init__(self, batch_size=16, num_samples=150):
super().__init__()
self.batch_size = batch_size
self.num_samples = num_samples
def setup(self, stage=None):
X = torch.randn(self.num_samples, 20)
y = torch.randn(self.num_samples)
dataset = torch.utils.data.TensorDataset(X, y)
train_size = int(0.8 * self.num_samples)
val_size = self.num_samples - train_size
self.train, self.val = torch.utils.data.random_split(
dataset, [train_size, val_size]
)
def train_dataloader(self):
return DataLoader(self.train, batch_size=self.batch_size)
def val_dataloader(self):
return DataLoader(self.val, batch_size=self.batch_size)
params2 = {
"data_module": RegressionDataModule,
"lightning_module": RegressionModule,
"trainer_kwargs": {
"max_epochs": 1,
"enable_progress_bar": False,
"enable_model_summary": False,
"logger": False,
},
"dm_kwargs": {"batch_size": 8, "num_samples": 200},
"objective_metric": "val_loss",
}
return [params, params2]
@classmethod
def _get_score_params(cls):
"""Return settings for testing score/evaluate functions.
Returns a list, the i-th element should be valid arguments for
self.evaluate and self.score, of an instance constructed with
self.get_test_params()[i].
Returns
-------
list of dict
The parameters to be used for scoring.
"""
score_params1 = {"input_dim": 10, "hidden_dim": 20, "lr": 0.001}
score_params2 = {"num_layers": 3, "hidden_size": 64, "dropout": 0.2}
return [score_params1, score_params2]