Created
January 14, 2021 22:02
-
-
Save imflash217/4b91225c886c2201fc563a355dff73c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn.Functional as F | |
import pytorch_lightning as pl | |
########################################################################################### | |
## Pytorch_Lightning version | |
## | |
class FlashModel(pl.LightningModule): | |
"""DOCSTRING""" | |
def __init__(self, model): | |
super().__init__() | |
self.model = model | |
def training_step(self, batch, batch_idx): | |
x, y = batch | |
y_hat = self.model(x) | |
loss = F.cross_entropy(y_hat, y) | |
return loss | |
########################################################################################### | |
## Under the hood PL does the following => | |
## | |
## Step-1: Put the model in train mode | |
model.train() | |
torch.set_grad_enabled = True | |
losses = [] | |
for batch in train_dataloader: | |
## Step-2: Forward | |
loss = training_step(batch) | |
losses.append(loss.detach()) | |
## Step-3: Backward | |
loss.backward() | |
## Step-4: apply optimizer step and clear grads | |
optimizer.step() | |
optimizer.zero_grad() | |
########################################################################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment