<jupyter_start><jupyter_text>Defining Neural network<jupyter_code>def activation(x):
    return 1/(1+torch.exp(-x))
    
##Flatten the input images
inputs = images.view(images.shape[0], -1)

##Defining weights and bias
w1 = torch.randn(784, 256)
b1 = torch.randn(256)

w2 = torch.randn(256, 10)
b2 = torch.randn(10)

h = activation(torch.mm(inputs, w1)+b1) #input layer
output = torch.mm(h, w2)+b2  #hidden layer

print(output.shape)<jupyter_output>torch.Size([64, 10])<jupyter_text>Building model with nn Module<jupyter_code>from torch import nn

class Network(nn.Module):
    def __init__(self):
        super.__init__()
        
        self.hidden = nn.Linear(784, 256)
        
        self.output = nn.Linear(256, 10)
        
        self.sigmoid = nn.sigmoid()
        self.softmax = nn.softmax()
        
    def forward(self, x):
        x = self.hidden(x)
        x = self.output(x)
        x = self.sigmoid(x)
        x = self.softmax(x)
        
        return x<jupyter_output><empty_output><jupyter_text>Building Feed forward network<jupyter_code># Model Building
model = nn.Sequential(nn.Linear(784, 128),
                     nn.ReLU(),
                     nn.Linear(128, 64),
                     nn.ReLU(),
                     nn.Linear(64, 10))

#Defining Loss
criteron = nn.CrossEntropyLoss()

#Getting data
images,labels = next(iter(trainloader))

#Flattening image
images = images.view(images.shape[0], -1)

#Forward pass, get out logits
logits = model(images)

loss = criteron(logits, labels)

print(loss)<jupyter_output>tensor(2.3359, grad_fn=<NllLossBackward>)<jupyter_text>Using PyTorch Autograd to calculate gradientsAutograd is used to trace the backward operation on gradients<jupyter_code>t = torch.randn(2,2, requires_grad=True)
t<jupyter_output><empty_output><jupyter_text>Building complete Network<jupyter_code>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.001)

epochs = 10

for i in range(epochs):
    running_loss = 0
    for images, labels in trainloader:
        images = images.view(images.shape[0], -1)
        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)}")<jupyter_output>training loss: 2.2170375224623853
training loss: 1.927981970788065
training loss: 1.4544711576850176
training loss: 1.018509976772357
training loss: 0.765755649918178
training loss: 0.6320645317022227
training loss: 0.5550009676578965
training loss: 0.5048553200482305
training loss: 0.46987719560609953
training loss: 0.4438949217165965<jupyter_text>Predicting the model<jupyter_code>import numpy as np

def view_classify(img, ps, version="MNIST"):
    ''' Function for viewing an image and it's predicted classes.
    '''
    ps = ps.data.numpy().squeeze()

    fig, (ax1, ax2) = plt.subplots(figsize=(6,9), ncols=2)
    ax1.imshow(img.resize_(1, 28, 28).numpy().squeeze())
    ax1.axis('off')
    ax2.barh(np.arange(10), ps)
    ax2.set_aspect(0.1)
    ax2.set_yticks(np.arange(10))
    if version == "MNIST":
        ax2.set_yticklabels(np.arange(10))
    elif version == "Fashion":
        ax2.set_yticklabels(['T-shirt/top',
                            'Trouser',
                            'Pullover',
                            'Dress',
                            'Coat',
                            'Sandal',
                            'Shirt',
                            'Sneaker',
                            'Bag',
                            'Ankle Boot'], size='small');
    ax2.set_title('Class Probability')
    ax2.set_xlim(0, 1.1)

    plt.tight_layout()

%matplotlib inline

images, labels = next(iter(trainloader))

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

# Output of the network are log-probabilities, need to take exponential for probabilities
ps = torch.exp(logps)
view_classify(img.view(1, 28, 28), ps)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12137805156285648, 'fraction_numerical': 0.06844626967830253, 'mean_word_length': 2.5497975708502025, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '213518', 'license': 'MIT', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1490, 'n_words': 302, 'path': 'basic projects/MNIST digit classifier.ipynb', 'repo_name': 'prasad5141/DeepLearning-projects', 'size': '4383'}