Created
July 27, 2020 18:46
-
-
Save nunenuh/7b0700f0c6ef58f1bc3ff0a50bed68d5 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
# build your model | |
class CustomMNIST(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) | |
# train your model | |
model = CustomMNIST() | |
trainer = Trainer(max_epochs=5, gpus=1) | |
trainer.fit(model, mnist_train_loader) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment