<jupyter_start><jupyter_text>Building neural networks with PyTorchPyTorch provides a module `nn` that makes building networks much simpler. Here we'll build and train a neural network that can recognise hand written digits.You can define the network somewhat more concisely and clearly using the `torch.nn.functional` module. This is the most common way you'll see networks defined as many operations are simple element-wise functions. We normally import this module as `F`, `import torch.nn.functional as F`.**Note** : For those who don't have numpy and matplotlib installed run the following commands.* pip install numpy* pip install matplotlib<jupyter_code># Import necessary packages

%matplotlib inline
%config InlineBackend.figure_format = 'retina'


import numpy as np
import torch

import helper

import matplotlib.pyplot as plt

#Obtaining the data set
from torchvision import datasets, transforms

#Tranforming the data
transform = transforms.Compose([transforms.ToTensor(),
                               transforms.Normalize((0.5),(0.5))])
#Download dataset
trainset =  datasets.MNIST('~/.pytorch/MNIST_data', download=True, train=True, transform = transform)

trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

#Image sample
dataiter = iter(trainloader)
images, labels = dataiter.next()
print(type(images))
print(images.shape)
print(labels.shape)

plt.imshow(images[1].numpy().squeeze(), cmap='Greys_r')
#Build a Neural Network
from torch import nn
import torch.nn.functional as F

class Network(nn.Module):
    def __init__(self):
        super().__init__()
        #layers are 784 inputs 128 hidden1, 64 hidden2, 10 output
        self.fc1 = nn.Linear(784, 128)
        self.fc2 = nn.Linear(128, 64)
        #output layer
        self.fc3 = nn.Linear(64, 10)

    def forward(self ,x):
        x=self.fc1(x)
        x=F.relu(x)
        x=self.fc2(x)
        x=F.relu(x)
        x=self.fc3(x)
        x=F.softmax(x,dim=1)
        
        return x
    
model = Network()


# Forward Pass
dataiter = iter(trainloader)
images, labels = dataiter.next()

#Resize images (batch_size, color channel, pixels)
images.resize_(64, 1, 784) #28*28

#forward pass
img_index = 0
ps = model.forward(images[img_index,:])

img = images[img_index,:]
helper.view_classify(img.view(1,28,28),ps)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10515112813963388, 'fraction_numerical': 0.024265644955300127, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '181025', 'license': 'MIT', 'n_tokens_mistral': 778, 'n_tokens_neox': 731, 'n_words': 225, 'path': 'Building a Neural Network.ipynb', 'repo_name': 'D-coder22/DC-Pytorch-I', 'size': '2349'}