Skip to content

Commit d272f29

Browse files
committed
updated docs
1 parent 600c755 commit d272f29

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,34 @@ To use lightning do 2 things:
4242
```python
4343
import pytorch_lightning as ptl
4444
import torch
45+
from torch.nn import functional as F
46+
from torch.utils.data import DataLoader
47+
from torchvision.datasets import MNIST
4548

4649
class CoolModel(ptl.LightningModule):
4750

4851
def __init(self):
52+
# not the best model...
4953
self.l1 = torch.nn.Linear(28*28, 10)
5054

5155
def forward(self, x):
52-
return self.l1(x)
56+
return torch.relu(self.l1(x))
57+
58+
def my_loss(self, y_hat, y):
59+
return F.cross_entropy(y_hat, y)
5360

5461
def training_step(self, batch, batch_nb):
5562
x, y = batch
5663
y_hat = self.forward(x)
57-
return {'tng_loss': some_loss(y_hat, y)}
64+
return {'tng_loss': self.my_loss(y_hat, y)}
5865

5966
def validation_step(self, batch, batch_nb):
6067
x, y = batch
6168
y_hat = self.forward(x)
62-
return {'val_loss': some_loss(y_hat, y)}
69+
return {'val_loss': self.my_loss(y_hat, y)}
6370

6471
def configure_optimizers(self):
65-
return [optim.Adam(self.parameters(), lr=0.02)]
72+
return [torch.optim.Adam(self.parameters(), lr=0.02)]
6673

6774
@ptl.data_loader
6875
def tng_dataloader(self):
@@ -74,8 +81,7 @@ class CoolModel(ptl.LightningModule):
7481

7582
@ptl.data_loader
7683
def test_dataloader(self):
77-
return DataLoader(MNIST('path/to/save', train=False), batch_size=32)
78-
84+
return DataLoader(MNIST('path/to/save', train=False), batch_size=32)
7985
```
8086

8187
2. Fit with a [trainer](https://williamfalcon.github.io/pytorch-lightning/Trainer/)

docs/LightningModule/RequiredTrainerInterface.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,53 @@ Otherwise, to Define a Lightning Module, implement the following methods:
2626
- [update_tng_log_metrics](RequiredTrainerInterface.md#update_tng_log_metrics)
2727
- [add_model_specific_args](RequiredTrainerInterface.md#add_model_specific_args)
2828

29+
---
30+
**Minimal example**
31+
```python
32+
import pytorch_lightning as ptl
33+
import torch
34+
from torch.nn import functional as F
35+
from torch.utils.data import DataLoader
36+
from torchvision.datasets import MNIST
37+
38+
class CoolModel(ptl.LightningModule):
39+
40+
def __init(self):
41+
# not the best model...
42+
self.l1 = torch.nn.Linear(28*28, 10)
43+
44+
def forward(self, x):
45+
return torch.relu(self.l1(x))
46+
47+
def my_loss(self, y_hat, y):
48+
return F.cross_entropy(y_hat, y)
49+
50+
def training_step(self, batch, batch_nb):
51+
x, y = batch
52+
y_hat = self.forward(x)
53+
return {'tng_loss': self.my_loss(y_hat, y)}
54+
55+
def validation_step(self, batch, batch_nb):
56+
x, y = batch
57+
y_hat = self.forward(x)
58+
return {'val_loss': self.my_loss(y_hat, y)}
59+
60+
def configure_optimizers(self):
61+
return [torch.optim.Adam(self.parameters(), lr=0.02)]
62+
63+
@ptl.data_loader
64+
def tng_dataloader(self):
65+
return DataLoader(MNIST('path/to/save', train=True), batch_size=32)
66+
67+
@ptl.data_loader
68+
def val_dataloader(self):
69+
return DataLoader(MNIST('path/to/save', train=False), batch_size=32)
70+
71+
@ptl.data_loader
72+
def test_dataloader(self):
73+
return DataLoader(MNIST('path/to/save', train=False), batch_size=32)
74+
```
75+
2976
---
3077

3178
### training_step

pytorch_lightning/root_module/root_module.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ def configure_optimizers(self):
6464
"""
6565
raise NotImplementedError
6666

67-
def loss(self, *args, **kwargs):
68-
"""
69-
Expand model_out into your components
70-
:param model_out:
71-
:return:
72-
"""
73-
raise NotImplementedError
74-
7567
def summarize(self):
7668
model_summary = ModelSummary(self)
7769
print(model_summary)

0 commit comments

Comments
 (0)