Created
March 19, 2019 02:00
-
-
Save Tumurtogtokh/8d1ac3171c60a1f7ad83607c3a336d8f to your computer and use it in GitHub Desktop.
Pytorch_NNdemo002
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
# 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): | |
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment