Skip to content

Instantly share code, notes, and snippets.

@psatler
Last active November 12, 2018 19:04
Show Gist options
  • Save psatler/bc5c9e65c58b0737b3949b36ce24feb6 to your computer and use it in GitHub Desktop.
Save psatler/bc5c9e65c58b0737b3949b36ce24feb6 to your computer and use it in GitHub Desktop.

PyTorch Challenge

# single layer NN
import torch

def activation(x):
    """ Sigmoid activation function 
    
        Arguments
        ---------
        x: torch.Tensor
    """
    return 1/(1+torch.exp(-x))
    
### Generate some data
torch.manual_seed(7) # Set the random seed so things are predictable

# Features are 3 random normal variables
features = torch.randn((1, 5))
print("features ", features)
# True weights for our data, random normal variables again
weights = torch.randn_like(features)
print("weights ", weights)
# and a true bias term
bias = torch.randn((1, 1))


## Calculate the output of this network using matrix multiplication
def output(features, weights, bias):
    transposedWeight = weights.view(5,1)
    mm = torch.mm(features, transposedWeight) # matrix mult
    h = mm + bias
    return activation(h)

# y = activation(torch.mm(features, weights.view(5,1) + bias))

print(output(features, weights, bias))

Defining the Network using class

from torch import nn 
import torch.nn.functional as F

class Network(nn.Module):
    def __init__(self):
        super().__init__()
        # Inputs to hidden layer linear transformation
        self.hidden = nn.Linear(784, 256)
        # Output layer, 10 units - one for each digit
        self.output = nn.Linear(256, 10)
        
    def forward(self, x):
        # Hidden layer with sigmoid activation
        x = F.sigmoid(self.hidden(x))
        # Output layer with softmax activation
        x = F.softmax(self.output(x), dim=1)
        
        return x

Training

from torch import optim

model = nn.Sequential(nn.Linear(784, 128),
                      nn.ReLU(),
                      nn.Linear(128, 64),
                      nn.ReLU(),
                      nn.Linear(64, 10),
                      nn.LogSoftmax(dim=1))

criterion = nn.NLLLoss()
optimizer = optim.SGD(model.parameters(), lr=0.003)

epochs = 5
for e in range(epochs):
    running_loss = 0
    for images, labels in trainloader:
        # Flatten MNIST images into a 784 long vector
        images = images.view(images.shape[0], -1)
    
        # TODO: Training pass
        optimizer.zero_grad()
        
        output = model.forward(images)
        loss = criterion(output, labels)
        loss.backward()
        optimizer.step()
        
        running_loss += loss.item()
    else:
        print(f"Training loss: {running_loss/len(trainloader)}")
        
        
        
%matplotlib inline
import helper

images, labels = next(iter(trainloader))

img = images[0].view(1, 784)
# Turn off gradients to speed up this part
with torch.no_grad():
    logits = model.forward(img)

# Output of the network are logits, need to take softmax for probabilities
ps = F.softmax(logits, dim=1)
helper.view_classify(img.view(1, 28, 28), ps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment