<jupyter_start><jupyter_text>Lab 02 : Training with epochs -- solution<jupyter_code># For Google Colaboratory
import sys, os
if 'google.colab' in sys.modules:
    # mount google drive
    from google.colab import drive
    drive.mount('/content/gdrive')
    path_to_file = '/content/gdrive/My Drive/CS4243_codes/codes/labs_lecture05/lab02_epoch'
    print(path_to_file)
    # move to Google Drive directory
    os.chdir(path_to_file)
    !pwd
import torch
import torch.nn as nn
import torch.optim as optim
from random import randint
import utils<jupyter_output><empty_output><jupyter_text>Download the data and print the sizes<jupyter_code>from utils import check_mnist_dataset_exists
data_path=check_mnist_dataset_exists()

train_data=torch.load(data_path+'mnist/train_data.pt')
train_label=torch.load(data_path+'mnist/train_label.pt')
test_data=torch.load(data_path+'mnist/test_data.pt')<jupyter_output><empty_output><jupyter_text>Make a ONE layer net class.<jupyter_code>class one_layer_net(nn.Module):

    def __init__(self, input_size, output_size):
        super(one_layer_net , self).__init__()
        self.linear_layer = nn.Linear( input_size, output_size , bias=False)
        
    def forward(self, x):
        scores = self.linear_layer(x)
        return scores<jupyter_output><empty_output><jupyter_text>Build the net<jupyter_code>net=one_layer_net(784,10)
print(net)<jupyter_output>one_layer_net(
  (linear_layer): Linear(in_features=784, out_features=10, bias=False)
)<jupyter_text>Choose the criterion, optimizer, batchsize, learning rate<jupyter_code>criterion = nn.CrossEntropyLoss()

optimizer=torch.optim.SGD( net.parameters() , lr=0.01 )

bs=50<jupyter_output><empty_output><jupyter_text>You only have stuff to do in this cell  Do 15 passes through the training set<jupyter_code>for epoch in range(15):
    
    shuffled_indices=torch.randperm(60000)
 
    for count in range(0,60000,bs):
    
        optimizer.zero_grad()
        
        indices=shuffled_indices[count:count+bs]
        minibatch_data =  train_data[indices]
        minibatch_label= train_label[indices]

        inputs = minibatch_data.view(bs,784)

        inputs.requires_grad_()

        scores=net( inputs ) 

        loss = criterion( scores , minibatch_label) 
 
        loss.backward()

        optimizer.step()<jupyter_output><empty_output><jupyter_text>Choose image at random from the test set and see how good/bad are the predictions<jupyter_code># choose a picture at random
idx=randint(0, 10000-1)
im=test_data[idx]

# diplay the picture
utils.show(im)

# feed it to the net and display the confidence scores
scores =  net( im.view(1,784)) 
probs= torch.softmax(scores, dim=1)
utils.show_prob_mnist(probs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0989051094890511, 'fraction_numerical': 0.020072992700729927, 'mean_word_length': 4.8195329087048835, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '37141', 'license': 'MIT', 'n_tokens_mistral': 999, 'n_tokens_neox': 954, 'n_words': 190, 'path': 'codes/labs_lecture05/lab02_epoch/epoch_solution.ipynb', 'repo_name': 'zzybluebell/CS4243', 'size': '2740'}