<jupyter_start><jupyter_text>Part 2 - Classification with PyTorch<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Hyperparameters<jupyter_code>device = 'cuda'
n_epochs = 10
batch_size_train = 128
batch_size_test = 32
learning_rate = 0.1
momentum = 0.9<jupyter_output><empty_output><jupyter_text>Prepare FashionMNIST image dataset

Using torchvision api to download MNIST data. MNIST data contains 10 classes of fashion.<jupyter_code>transform = torchvision.transforms.Compose([
        torchvision.transforms.ToTensor()
    ])

train_data = torchvision.datasets.FashionMNIST('./data', train=True, download=True,
                                       transform=transform)
validation_data = torchvision.datasets.FashionMNIST('./data', train=False, download=True,
                                      transform=transform)

train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size_train, 
                                           shuffle=True)
validation_loader = torch.utils.data.DataLoader(validation_data, batch_size=batch_size_test, 
                                           shuffle=True)<jupyter_output><empty_output><jupyter_text>Define Model<jupyter_code>class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        """
        Define Neural Netowork layers:
        1. Input layer of 784 nodes
        2. 2 hidden layers
            - First hiddden layer has 256 nodes
            - Second hidden layer has 50 nodes
        3. Output layers according to number of classes 
        """

    def forward(self, x):
        """
        Connect Neural Network
        1. All layers use RELU as activation function
        2. Use log softmax for output layer
        """

        return None
# Initialize model and send to GPU
network = Net().to(device)

# Set algorithm for parameter update
optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum)<jupyter_output><empty_output><jupyter_text>Train Model  Evaluation Function<jupyter_code>def evaluate():
    network.eval()
    val_loss, correct = 0, 0
    with torch.no_grad():
        for data, target in validation_loader:
            data, target = data.to(device), target.to(device)
            data = data.view(data.shape[0], -1)
            output = network(data)
            val_loss += F.nll_loss(output, target, size_average=False).item() 
            
            pred = output.data.max(1, keepdim=True)[1]
            correct += pred.eq(target.data.view_as(pred)).sum()
    val_loss /= len(validation_loader.dataset)
    val_losses.append(val_loss)
    print('\nValidation set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        val_loss, correct, len(validation_loader.dataset),
        100. * correct / len(validation_loader.dataset)))
network<jupyter_output><empty_output><jupyter_text>Training Loop<jupyter_code>train_losses, train_counter, val_losses = [], [], []
"""
Build training Loop. Print training loss for every 10 data batch interval
"""<jupyter_output><empty_output><jupyter_text>View Evaluation<jupyter_code># Evaluate on one batch only
examples = enumerate(validation_loader)
batch_idx, (example_data, example_targets) = next(examples)

# To run model without performing grading parameters
with torch.no_grad():

    # Prepare dataset
    pred_data = example_data
    pred_data = pred_data.view(pred_data.shape[0], -1)

    # Set model to evaluation mode
    network.eval()

    # Inference
    predictions = network(pred_data.to(device))

    # Format predictions to get labels
    predicted_labels = predictions.data.max(1, keepdim=True)[1].cpu().detach().numpy()
fashion_labels = ["T-shirt/top", "Trouser", "Pullover", 
                  "Dress", "Coat", "Sandal", "Shirt", 
                  "Sneaker", "Bag", "Ankle boot"]
fig = plt.figure()
for i in range(6):
    plt.subplot(2,3,i+1)
    plt.tight_layout()
    plt.imshow(example_data[i][0], cmap='gray', interpolation='none')
    plt.title("Predicted: {}".format(fashion_labels[predicted_labels[i][0]]))
    plt.xticks([])
    plt.yticks([])
fig<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10979768129120254, 'fraction_numerical': 0.011593543987269835, 'mean_word_length': 3.029304029304029, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '432704', 'license': 'Apache-2.0', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1320, 'n_words': 295, 'path': 'notebooks/Day_2/Classification_Exercise/Ex-Image_Classification.ipynb', 'repo_name': 'CertifaiAI/jetbot-labs', 'size': '4399'}