# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="uVKZFzf9I2Hb"
#

# + [markdown] id="rQ2grrj7Iopw"
# # Lab 04 : Train vanilla neural network -- exercise
#
#
# # Training a one-layer net on  FASHION-MNIST

# + colab={"base_uri": "https://localhost:8080/"} id="bFNyy3YRIop4" executionInfo={"status": "ok", "timestamp": 1631301424198, "user_tz": -480, "elapsed": 37751, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="5414625d-ca7c-44a1-aa9b-5725d12d8891"
# For Google Colaboratory
import sys, os
if 'google.colab' in sys.modules:
    # mount google drive
    from google.colab import drive
    drive.mount('/content/gdrive')
    # find automatically the path of the folder containing "file_name" :
    file_name = 'train_vanilla_nn_exercise.ipynb'
    import subprocess
    path_to_file = subprocess.check_output('find . -type f -name ' + str(file_name), shell=True).decode("utf-8")
    path_to_file = path_to_file.replace(file_name,"").replace('\n',"")
    # if previous search failed or too long, comment the previous line and simply write down manually the path below :
    #path_to_file = '/content/gdrive/My Drive/CS5242_2021_codes/codes/labs_lecture03/lab04_train_vanilla_nn'
    print(path_to_file)
    # change current path to the folder containing "file_name"
    os.chdir(path_to_file)
    # !pwd

# + id="WLTJzmiAIop6" executionInfo={"status": "ok", "timestamp": 1631301442998, "user_tz": -480, "elapsed": 5418, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}}
import torch
import torch.nn as nn
import torch.optim as optim
from random import randint
import utils

# + [markdown] id="b4J_SGkGIop7"
# ### Download the TRAINING SET (data+labels)

# + colab={"base_uri": "https://localhost:8080/", "height": 522, "referenced_widgets": ["03cf8083664a43d2bdb3fd32d611daee", "e39f972153ba4152bf87e0265cbe31ae", "eae27f3de9554bcfb63130600c8fafd7", "a0cf7a1a0a9c47e0acc1f1c854f12ff7", "03b3484e39054f4eb608e9bc47048e87", "1959eab79eb2459fa631c3b43188d8ac", "0a3c3020ec7442e8b7fa66ce449ab003", "2a0442ad85414c3dbf790e0c032c461e", "513c449d33f54d6c8e3d9e2b4709622c", "<KEY>", "<KEY>", "78cd728f4cce4d9b8a5ea17365faf946", "34a661b627c34a32b5de949845985e48", "<KEY>", "53afa963948141328b94795291ca20b9", "<KEY>", "<KEY>", "<KEY>", "4381ce8a8cc342a7b4c3e80918b1bd31", "8b9e288437314f309588ff8e246d885c", "65cbed60f1db4e3584fe12b10454328c", "ef3aa32f16a9478f9e5e0d1e4e90ed4e", "<KEY>", "447386feb4ea479996f97b7ef95c1603", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "2883079520ef43ce9d19baf7a08b0d98", "4d8a9daa958a4ed590824d7181c928a2", "35d875f7956143158a0be1c4ca0d7601", "d229669dbd9541d29e0678b94ad47907", "<KEY>", "86d3527962d84587a7c5584fef1cc87a", "d9ba6fb596284340be6c7e3c9a545c4b", "<KEY>", "78960b70a80d4550ae2d00dfd0f6921d", "5e111661570f4d36a09d712f2ab5d3d6"]} id="qn4pfya3Iop8" executionInfo={"status": "ok", "timestamp": 1631301456448, "user_tz": -480, "elapsed": 13459, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="05787fe8-69c7-48b1-eca0-c1be5f0ce090"
from utils import check_fashion_mnist_dataset_exists
data_path=check_fashion_mnist_dataset_exists()

train_data=torch.load(data_path+'fashion-mnist/train_data.pt')
train_label=torch.load(data_path+'fashion-mnist/train_label.pt')
print(train_data.size())
print(train_label.size())

# + [markdown] id="u_sFlECBIop9"
# ### Download the TEST SET (data only)

# + colab={"base_uri": "https://localhost:8080/"} id="wZpbN_MzIop-" executionInfo={"status": "ok", "timestamp": 1631301461653, "user_tz": -480, "elapsed": 518, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="cce43c62-ab53-44bc-ec5f-ddefcc0bea0a"
test_data=torch.load(data_path+'fashion-mnist/test_data.pt')
print(test_data.size())


# + [markdown] id="uIXKA-iAIop-"
# ### Make a one layer net class

# + id="mCp7shxGIop_" executionInfo={"status": "ok", "timestamp": 1631302603825, "user_tz": -480, "elapsed": 552, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}}
class one_layer_net(nn.Module):

    def __init__(self, input_size, output_size):
        super(one_layer_net , self).__init__()
        
        # complete here
        self.linear_layer = nn.Linear(input_size, output_size, bias = True)
        
    def forward(self, x):
        
        x = self.linear_layer(x)# complete here
        p = torch.softmax(x, dim=1)# complete here
        
        return p


# + [markdown] id="aB-OkhLbIoqA"
# ### Build the net

# + colab={"base_uri": "https://localhost:8080/"} id="P4lwTsApIoqB" executionInfo={"status": "ok", "timestamp": 1631302611907, "user_tz": -480, "elapsed": 613, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="b7ed8e61-dbe0-4e09-b268-dd9036bf7a12"
net=one_layer_net(784,10)
print(net)

# + [markdown] id="T07csKe-IoqB"
# ### Take the 4th image of the test set:

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="aUDQBE2iIoqC" executionInfo={"status": "ok", "timestamp": 1631302948826, "user_tz": -480, "elapsed": 547, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="315b0905-c887-4107-8ec3-22a24bf572f7"
im= test_data[3]# complete here
utils.show(im)

# + [markdown] id="lO9h9E-ZIoqC"
# ### And feed it to the UNTRAINED network:

# + colab={"base_uri": "https://localhost:8080/"} id="9VbLixjTIoqD" executionInfo={"status": "ok", "timestamp": 1631303093200, "user_tz": -480, "elapsed": 517, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="3acd8f82-27cf-4116-970e-cf571a8e8a55"
print(im.size())
p = net(im.view(1, 784))# complete here
print(p)

# + [markdown] id="r7rwsS3NIoqD"
# ### Display visually the confidence scores

# + colab={"base_uri": "https://localhost:8080/", "height": 406} id="lZ9nBW2KIoqD" executionInfo={"status": "ok", "timestamp": 1631303109070, "user_tz": -480, "elapsed": 713, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="4271a217-cdb7-4637-e3bc-e0dd1abdab77"
utils.show_prob_fashion_mnist(p)

# + colab={"base_uri": "https://localhost:8080/"} id="QW3iAHoOQ3bW" executionInfo={"status": "ok", "timestamp": 1631303506996, "user_tz": -480, "elapsed": 519, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="30a0fbef-7c27-459f-f351-0b7e7d024007"
print(train_label[0])
print(train_label[0].view(1))

# + [markdown] id="-8jrxer5IoqD"
# ### Train the network (only 5000 iterations) on the train set

# + id="lEuJD4dNIoqE" executionInfo={"status": "ok", "timestamp": 1631304718581, "user_tz": -480, "elapsed": 2149, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}}
criterion = nn.NLLLoss()
optimizer=torch.optim.SGD(net.parameters() , lr=0.01 )
# 训练 5000 次
for iter in range(1,5000):
    
    # choose a random integer between 0 and 59,999 
    # extract the corresponding picture and label
    # and reshape them to fit the network

    # complete here
    # complete here
    # complete here
    # 防止 neural network 记住顺序，能提高 5% 的预测率
    idx = randint(0, 60000-1)
    input = train_data[idx].view(1, 784)
    # label 需要 dimension
    label = train_label[idx].view(1)


    # feed the input to the net  
    input.requires_grad_() # for backprobagation -- we will discuss it later
    # complete here
    prob = net(input)
 
    # update the weights (all the magic happens here -- we will discuss it later)
    log_prob=torch.log(prob)
    loss = criterion(log_prob, label)    
    optimizer.zero_grad()       
    loss.backward()
    optimizer.step()

# + [markdown] id="U-GC4sBtIoqE"
# ### Take the 34th image of the test set:

# + colab={"base_uri": "https://localhost:8080/", "height": 430} id="gGJY8eBfIoqE" executionInfo={"status": "ok", "timestamp": 1631304868381, "user_tz": -480, "elapsed": 554, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="96d1a17b-1e67-4e3a-a1a5-9fd02c8a9877"
im= test_data[3]# complete here
utils.show(im)

# + [markdown] id="edN2XiXlIoqF"
# ### Feed it to the TRAINED net:

# + colab={"base_uri": "https://localhost:8080/"} id="R1L4zQyAIoqF" executionInfo={"status": "ok", "timestamp": 1631304761080, "user_tz": -480, "elapsed": 517, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="6f17eb91-66f1-44f6-f90c-bdcae7b9a689"
p = net(im.view(1, 784))# complete here
print(p)

# + [markdown] id="IlcihUadIoqF"
# ### Display visually the confidence scores

# + colab={"base_uri": "https://localhost:8080/", "height": 406} id="hdgp20pfIoqF" executionInfo={"status": "ok", "timestamp": 1631304764766, "user_tz": -480, "elapsed": 724, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "14753845709256584186"}} outputId="9cb3d3d8-72d7-48cf-803a-af7374415415"
utils.show_prob_fashion_mnist(prob)

# + [markdown] id="nYHHJ0R0IoqK"
# ### Choose image at random from the test set and see how good/bad are the predictions

# + id="Z5y9zwrqIoqK"
# 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
prob =  net( im.view(1,784)) 
utils.show_prob_fashion_mnist(prob)

# + id="EmNnKjdkIoqK"


StarcoderdataJupyterScriptsDedupFiltered