Last active
March 19, 2019 00:27
-
-
Save Tumurtogtokh/f90c550c3206a7d48d7d11457c23cfa7 to your computer and use it in GitHub Desktop.
Code snippet for: https://wp.me/p5EUYy-fq
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
n_input = 784 | |
n_hidden = 200 | |
n_output = 10 | |
# Model | |
model = nn.Sequential(nn.Linear(n_input, 128), | |
nn.ReLU(), | |
nn.Linear(128, 64), | |
nn.ReLU(), | |
nn.Linear(64,10), | |
nn.LogSoftmax(dim=1)) | |
# Defining gradient descent | |
learning_rate = 0.01 | |
criterion = nn.NLLLoss() | |
optimiser = optim.SGD(model.parameters(), lr=learning_rate) | |
# Training the network | |
epochs = 5 | |
for e in range(epochs): | |
train_loss = 0 | |
for images, labels in train_loader: | |
# Flattening an input image to vector | |
images = images.view(images.shape[0], -1) | |
# Initialising model parameters' gradient to zero | |
optimiser.zero_grad() | |
# Propogating forward: input -> hidden -> output | |
output = model.forward(images) | |
# Calculating error at output | |
loss = criterion(output, labels) | |
# Propogating an error backward: output -> hidden -> input | |
loss.backward() | |
# Updating weight | |
optimiser.step() | |
# Recoring loss of error | |
train_loss += loss.item() | |
print(f'Training loss:{train_loss/len(train_loader)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment