@@ -37,25 +37,58 @@ class NetLightRegression(L.LightningModule):
3737
3838 Examples:
3939 >>> from torch.utils.data import DataLoader
40- >>> from torchvision.datasets import MNIST
41- >>> from torchvision.transforms import ToTensor
42- >>> train_data = MNIST(PATH_DATASETS,
43- train=True,
44- download=True,
45- transform=ToTensor())
46- >>> train_loader = DataLoader(train_data,
47- batch_size=BATCH_SIZE)
48- >>> net_light_base = NetLightRegression(l1=128,
49- epochs=10,
50- batch_size=BATCH_SIZE,
51- initialization='xavier',
52- act_fn=nn.ReLU(),
53- optimizer='Adam',
54- dropout_prob=0.1,
55- lr_mult=0.1,
56- patience=5)
57- >>> trainer = L.Trainer(max_epochs=10)
58- >>> trainer.fit(net_light_base, train_loader)
40+ from spotPython.data.diabetes import Diabetes
41+ from spotPython.light.netlightregression import NetLightRegression
42+ from torch import nn
43+ import lightning as L
44+ PATH_DATASETS = './data'
45+ BATCH_SIZE = 8
46+ dataset = Diabetes()
47+ train_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
48+ test_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
49+ val_loader = DataLoader(dataset, batch_size=BATCH_SIZE)
50+ batch_x, batch_y = next(iter(train_loader))
51+ print(batch_x.shape)
52+ print(batch_y.shape)
53+ net_light_base = NetLightRegression(l1=128,
54+ epochs=10,
55+ batch_size=BATCH_SIZE,
56+ initialization='xavier',
57+ act_fn=nn.ReLU(),
58+ optimizer='Adam',
59+ dropout_prob=0.1,
60+ lr_mult=0.1,
61+ patience=5,
62+ _L_in=10,
63+ _L_out=1)
64+ trainer = L.Trainer(max_epochs=2, enable_progress_bar=True)
65+ trainer.fit(net_light_base, train_loader)
66+ trainer.validate(net_light_base, val_loader)
67+ trainer.test(net_light_base, test_loader)
68+
69+ | Name | Type | Params | In sizes | Out sizes
70+ -------------------------------------------------------------
71+ 0 | layers | Sequential | 15.9 K | [8, 10] | [8, 1]
72+ -------------------------------------------------------------
73+ 15.9 K Trainable params
74+ 0 Non-trainable params
75+ 15.9 K Total params
76+ 0.064 Total estimated model params size (MB)
77+
78+ ─────────────────────────────────────────────────────────────
79+ Validate metric DataLoader 0
80+ ─────────────────────────────────────────────────────────────
81+ hp_metric 29010.7734375
82+ val_loss 29010.7734375
83+ ─────────────────────────────────────────────────────────────
84+ ─────────────────────────────────────────────────────────────
85+ Test metric DataLoader 0
86+ ─────────────────────────────────────────────────────────────
87+ hp_metric 29010.7734375
88+ val_loss 29010.7734375
89+ ─────────────────────────────────────────────────────────────
90+
91+ [{'val_loss': 28981.529296875, 'hp_metric': 28981.529296875}]
5992 """
6093
6194 def __init__ (
@@ -93,18 +126,6 @@ def __init__(
93126
94127 Raises:
95128 ValueError: If l1 is less than 4.
96- Examples:
97- >>> from torch.utils.data import DataLoader
98- >>> from torchvision.datasets import MNIST
99- >>> from torchvision.transforms import ToTensor
100- >>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
101- >>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
102- >>> net_light_base = NetLightRegression(l1=128, epochs=10, batch_size=BATCH_SIZE,
103- initialization='xavier', act_fn=nn.ReLU(),
104- optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
105- patience=5)
106- >>> trainer = L.Trainer(max_epochs=10)
107- >>> trainer.fit(net_light_base, train_loader)
108129
109130 """
110131 super ().__init__ ()
@@ -148,19 +169,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
148169 x (torch.Tensor): A tensor containing a batch of input data.
149170
150171 Returns:
151- torch.Tensor: A tensor containing the probabilities for each class.
152- Examples:
153- >>> from torch.utils.data import DataLoader
154- >>> from torchvision.datasets import MNIST
155- >>> from torchvision.transforms import ToTensor
156- >>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
157- >>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
158- >>> net_light_base = NetLightRegression(l1=128,
159- epochs=10,
160- batch_size=BATCH_SIZE,
161- initialization='xavier', act_fn=nn.ReLU(),
162- optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
163- patience=5)
172+ torch.Tensor: A tensor containing the output of the model.
164173
165174 """
166175 x = self .layers (x )
@@ -175,20 +184,6 @@ def training_step(self, batch: tuple) -> torch.Tensor:
175184
176185 Returns:
177186 torch.Tensor: A tensor containing the loss for this batch.
178- Examples:
179- >>> from torch.utils.data import DataLoader
180- >>> from torchvision.datasets import MNIST
181- >>> from torchvision.transforms import ToTensor
182- >>> train_data = MNIST(PATH_DATASETS, train=True, download=True, transform=ToTensor())
183- >>> train_loader = DataLoader(train_data, batch_size=BATCH_SIZE)
184- >>> net_light_base = NetLightRegression(l1=128,
185- epochs=10,
186- batch_size=BATCH_SIZE,
187- initialization='xavier', act_fn=nn.ReLU(),
188- optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
189- patience=5)
190- >>> trainer = L.Trainer(max_epochs=10)
191- >>> trainer.fit(net_light_base, train_loader)
192187
193188 """
194189 x , y = batch
@@ -200,7 +195,7 @@ def training_step(self, batch: tuple) -> torch.Tensor:
200195 # self.log("train_mae_loss", mae_loss, on_step=True, on_epoch=True, prog_bar=True)
201196 return val_loss
202197
203- def validation_step (self , batch : tuple , batch_idx : int , prog_bar : bool = False ):
198+ def validation_step (self , batch : tuple , batch_idx : int , prog_bar : bool = False ) -> torch . Tensor :
204199 """
205200 Performs a single validation step.
206201
@@ -210,21 +205,7 @@ def validation_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False):
210205 prog_bar (bool, optional): Whether to display the progress bar. Defaults to False.
211206
212207 Returns:
213- (NoneType): None
214- Examples:
215- >>> from torch.utils.data import DataLoader
216- >>> from torchvision.datasets import MNIST
217- >>> from torchvision.transforms import ToTensor
218- >>> val_data = MNIST(PATH_DATASETS, train=False, download=True, transform=ToTensor())
219- >>> val_loader = DataLoader(val_data, batch_size=BATCH_SIZE)
220- >>> net_light_base = NetLightRegression(l1=128,
221- epochs=10,
222- batch_size=BATCH_SIZE,
223- initialization='xavier', act_fn=nn.ReLU(),
224- optimizer='Adam', dropout_prob=0.1, lr_mult=0.1,
225- patience=5)
226- >>> trainer = L.Trainer(max_epochs=10)
227- >>> trainer.fit(net_light_base, val_loader)
208+ torch.Tensor: A tensor containing the loss for this batch.
228209
229210 """
230211 x , y = batch
@@ -237,7 +218,7 @@ def validation_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False):
237218 self .log ("hp_metric" , val_loss , prog_bar = prog_bar )
238219 return val_loss
239220
240- def test_step (self , batch : tuple , batch_idx : int , prog_bar : bool = False ) -> tuple :
221+ def test_step (self , batch : tuple , batch_idx : int , prog_bar : bool = False ) -> torch . Tensor :
241222 """
242223 Performs a single test step.
243224
@@ -247,7 +228,7 @@ def test_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False) -> tup
247228 prog_bar (bool, optional): Whether to display the progress bar. Defaults to False.
248229
249230 Returns:
250- tuple : A tuple containing the loss and accuracy for this batch.
231+ torch.Tensor : A tensor containing the loss for this batch.
251232 """
252233 x , y = batch
253234 y_hat = self (x )
0 commit comments