0% found this document useful (0 votes)
1 views

Week1_Assignment1.ipynb - Colaboratory

Ml

Uploaded by

Preet Thakker
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Week1_Assignment1.ipynb - Colaboratory

Ml

Uploaded by

Preet Thakker
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8/19/22, 10:12 PM Week1_Assignment1.

ipynb - Colaboratory

#from google.colab import drive


#drive.mount('/content/drive')

#mnist = tf.keras.datasets.mnist
#(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

##from google.colab import files


##uploaded = files.upload()

#!pip install idx2numpy

#import idx2numpy
#train_images = idx2numpy.convert_from_file('/content/drive/MyDrive/Colab Notebooks/Data/t
#train_labels = idx2numpy.convert_from_file('/content/drive/MyDrive/Colab Notebooks/Data/t
#test_images = idx2numpy.convert_from_file('/content/drive/MyDrive/Colab Notebooks/Data/t1
#test_labels = idx2numpy.convert_from_file('/content/drive/MyDrive/Colab Notebooks/Data/t1

#train_images = train_images / 255.0


#test_images = test_images / 255.0

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf

mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
SCALE_FACTOR = 255 # TRES IMPORTANT SINON OVERFLOW SUR EXP
WIDTH = train_images.shape[1]
HEIGHT = train_images.shape[2]
train_images = train_images.reshape(train_images.shape[0],WIDTH*HEIGHT).T / SCALE_FACTOR
test_images = test_images.reshape(test_images.shape[0],WIDTH*HEIGHT).T / SCALE_FACTOR

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mni


11493376/11490434 [==============================] - 0s 0us/step
11501568/11490434 [==============================] - 0s 0us/step

train_images[0]

array([0., 0., 0., ..., 0., 0., 0.])

#train_images = train_images.reshape(-1, train_images.shape[0])


#train_images.shape

def init_parameters(size):
https://colab.research.google.com/drive/1pAnLtL9DOEXdqpwwiY71fbegQCwUctre#scrollTo=wGz6VrdI5feV&printMode=true 1/4
8/19/22, 10:12 PM Week1_Assignment1.ipynb - Colaboratory

W1 = np.random.rand(10, size) - 0.5


b1 = np.random.rand(10, 1) - 0.5
W2 = np.random.rand(10, 10) - 0.5
b2 = np.random.rand(10, 1) - 0.5
return W1, b1, W2, b2

def ReLU(Z):
return np.maximum(Z, 0)

def softmax(Z):
exp = np.exp(Z-np.max(Z))
return exp / exp.sum(axis = 0)
#A = np.exp(Z) / sum(np.exp(Z))
#return A

def forward_propogation(W1, b1, W2, b2, X):


Z1 = W1.dot(X) + b1
A1 = ReLU(Z1)
Z2 = W2.dot(A1) + b2
A2 = softmax(Z2)
return Z1, A1, Z2, A2

def deriv_ReLU(Z):
return Z > 0

def one_hot(Y):
one_hot_Y = np.zeros((Y.max() + 1, Y.size))
one_hot_Y[Y, np.arange(Y.size)] = 1
#one_hot_Y = one_hot_Y.T
return one_hot_Y

def backward_propogation(Z1, A1, Z2, A2, W2, X, Y, m):


one_hot_Y = one_hot(Y)
dZ2 = 2*(A2 - one_hot_Y)
dW2 = 1 / m * dZ2.dot(A1.T)
db2 = 1 / m * np.sum(dZ2, 1)
dZ1 = W2.T.dot(dZ2) * deriv_ReLU(Z1)
dW1 = 1 / m * dZ1.dot(X.T)
db1 = 1 / m * np.sum(dZ1, 1)
return dW1, db1, dW2, db2

def update_parameters(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha):
W1 = W1 - alpha * dW1
b1 = b1 - alpha * np.reshape(db1, (10,1))
W2 = W2 - alpha * dW2
b2 = b2 - alpha * np.reshape(db2, (10,1))
return W1, b1, W2, b2

https://colab.research.google.com/drive/1pAnLtL9DOEXdqpwwiY71fbegQCwUctre#scrollTo=wGz6VrdI5feV&printMode=true 2/4
8/19/22, 10:12 PM Week1_Assignment1.ipynb - Colaboratory

def get_predictions(A2):
return np.argmax(A2, 0)

def get_accuracy(predictions, Y):


print(predictions, Y)
return np.sum(predictions == Y) / Y.size

def gradient_descent(X, Y, alpha, iter):


size,m = X.shape
W1, b1, W2, b2 = init_parameters(size)
for i in range(iter):
Z1, A1, Z2, A2 = forward_propogation(W1, b1, W2, b2, X)
dW1, db1, dW2, db2 = backward_propogation(Z1, A1, Z2, A2, W2, X, Y, m)
W1, b1, W2, b2 = update_parameters(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)
if i % 10 == 0:
print("Iterations: ", i)
print("Accuracy: ", get_accuracy(get_predictions(A2), Y))
return W1, b1, W2, b2

W1, b1, W2, b2 = gradient_descent(train_images, train_labels, 0.1, 500)


Accuracy: 0.8608666666666667
Iterations: 310
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.86225
Iterations: 320
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.86365
Iterations: 330
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8654
Iterations: 340
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.86695
Iterations: 350
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.86835
Iterations: 360
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8698333333333333
Iterations: 370
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8709833333333333
Iterations: 380
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8718
Iterations: 390
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8727333333333334
Iterations: 400
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.874
Iterations: 410
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8748166666666667
https://colab.research.google.com/drive/1pAnLtL9DOEXdqpwwiY71fbegQCwUctre#scrollTo=wGz6VrdI5feV&printMode=true 3/4
8/19/22, 10:12 PM Week1_Assignment1.ipynb - Colaboratory
y
Iterations: 420
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8759
Iterations: 430
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8765666666666667
Iterations: 440
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8771
Iterations: 450
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8778666666666667
Iterations: 460
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8785666666666667
Iterations: 470
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8793666666666666
Iterations: 480
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8802333333333333
Iterations: 490
[5 0 4 ... 5 6 5] [5 0 4 ... 5 6 8]
Accuracy: 0.8810666666666667

check 0s completed at 10:12 PM

https://colab.research.google.com/drive/1pAnLtL9DOEXdqpwwiY71fbegQCwUctre#scrollTo=wGz6VrdI5feV&printMode=true 4/4

You might also like