Last active
July 27, 2020 18:32
-
-
Save nunenuh/e58dad14fb901d3b966b335cd176b90f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class StandardMNIST(LightningModule): | |
def __init__(self): | |
super().__init__() | |
# mnist images are (1, 28, 28) (channels, width, height) | |
self.layer1 = torch.nn.Linear(28 * 28, 128) | |
self.layer2 = torch.nn.Linear(128, 256) | |
self.layer3 = torch.nn.Linear(256, 10) | |
def forward(self, x): | |
batch_size, channels, width, height = x.size() | |
# (b, 1, 28, 28) -> (b, 1*28*28) | |
x = x.view(batch_size, -1) | |
x = self.layer1(x) | |
x = torch.relu(x) | |
x = self.layer2(x) | |
x = torch.relu(x) | |
x = self.layer3(x) | |
x = torch.log_softmax(x, dim=1) | |
return x | |
def training_step(self, batch, batch_idx): | |
data, target = batch | |
logits = self.forward(data) | |
loss = F.nll_loss(logits, target) | |
return {'loss': loss} | |
def configure_optimizers(self): | |
return torch.optim.Adam(self.parameters(), lr=1e-3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment