-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
import gzip | ||
import numpy as np | ||
|
||
DATA_URL = 'http://yann.lecun.com/exdb/mnist/' | ||
|
||
# Download and import the MNIST dataset from Yann LeCun's website. | ||
# Reserve 10,000 examples from the training set for validation. | ||
# Each image is an array of 784 (28x28) float values from 0 (white) to 1 (black). | ||
def load_data(one_hot=True, reshape=None, validation_size=10000): | ||
x_tr = load_images('train-images-idx3-ubyte.gz') | ||
y_tr = load_labels('train-labels-idx1-ubyte.gz') | ||
x_te = load_images('t10k-images-idx3-ubyte.gz') | ||
y_te = load_labels('t10k-labels-idx1-ubyte.gz') | ||
|
||
x_tr = x_tr[:-validation_size] | ||
y_tr = y_tr[:-validation_size] | ||
|
||
if one_hot: | ||
y_tr, y_te = [to_one_hot(y) for y in (y_tr, y_te)] | ||
|
||
if reshape: | ||
x_tr, x_te = [x.reshape(*reshape) for x in (x_tr, x_te)] | ||
|
||
return x_tr, y_tr, x_te, y_te | ||
|
||
def load_images(filename): | ||
maybe_download(filename) | ||
with gzip.open(filename, 'rb') as f: | ||
data = np.frombuffer(f.read(), np.uint8, offset=16) | ||
return data.reshape(-1, 28 * 28) / np.float32(256) | ||
|
||
def load_labels(filename): | ||
maybe_download(filename) | ||
with gzip.open(filename, 'rb') as f: | ||
data = np.frombuffer(f.read(), np.uint8, offset=8) | ||
return data | ||
|
||
# Download the file, unless it's already here. | ||
def maybe_download(filename): | ||
if not os.path.exists(filename): | ||
from urllib.request import urlretrieve | ||
print("Downloading %s" % filename) | ||
urlretrieve(DATA_URL + filename, filename) | ||
|
||
# Convert class labels from scalars to one-hot vectors. | ||
def to_one_hot(labels, num_classes=10): | ||
return np.eye(num_classes)[labels] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import numpy as np | ||
import mnist as mnist | ||
|
||
def feed_forward(X, weights): | ||
a = [X] | ||
for w in weights: | ||
a.append(np.maximum(a[-1].dot(w),0)) # Calculer l'avancée | ||
# np.maximum(val, 0) -> relu | ||
# a[-1].dot(w) -> valeur * poids | ||
# a.append -> rajouter la valeur à la fin du tableau | ||
return a | ||
|
||
def grads(X, Y, weights, square): | ||
grads = np.empty_like(weights) # grads représente la matrice de correction des poids | ||
a = feed_forward(X, weights) # on nourrit le réseau et on stocke les valeurs des neurones dans 'a' | ||
if square: | ||
delta = a[-1]*a[-1] - Y*Y # on met l'erreur au carré | ||
else: | ||
delta = a[-1] - Y # on calcule l'erreur simple | ||
grads[-1] = a[-2].T.dot(delta) | ||
for i in range(len(a)-2, 0, -1): | ||
delta = (a[i] > 0) * delta.dot(weights[i].T) | ||
grads[i-1] = a[i-1].T.dot(delta) | ||
return grads / len(X) | ||
|
||
def save(weights, filename): | ||
np.save(filename,weights) | ||
print('Data saved successfully into ',filename) | ||
|
||
if __name__ == '__main__': | ||
learn = True | ||
save_it = True | ||
square = True | ||
filename = 'four layers.npy' | ||
filename = 'blended/' + filename | ||
trX, trY, teX, teY = mnist.load_data() # Load model | ||
if learn: | ||
weights = [np.random.randn(*w) * 0.1 for w in [(784, 400), (400,200), (200,100), (100, 10)]] # Initialiser les poids | ||
num_epochs, batch_size, learn_rate = 30, 20, 0.03 # Initialiser les hyperparamètres | ||
prediction = np.argmax(feed_forward(teX, weights)[-1], axis=1) | ||
print(0, np.mean(prediction == np.argmax(teY, axis=1))) | ||
for i in range(num_epochs): | ||
for j in range(0, len(trX), batch_size): | ||
X, Y = trX[j:j+batch_size], trY[j:j+batch_size] | ||
weights -= learn_rate * grads(X, Y, weights, square) | ||
prediction = np.argmax(feed_forward(teX, weights)[-1], axis=1) | ||
print(i+1, np.mean(prediction == np.argmax(teY, axis=1))) | ||
if save_it: | ||
save(weights,filename) | ||
|
||
if not learn: | ||
weights = np.load(filename) | ||
prediction = np.argmax(feed_forward(teX, weights)[-1], axis=1) | ||
print(np.mean(prediction == np.argmax(teY, axis=1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import numpy as np | ||
import sys | ||
import matplotlib.image as image | ||
import json | ||
|
||
# Erreur si le nombre d'arguments n'est pas bon. | ||
try: | ||
assert len(sys.argv) == 3 | ||
except AssertionError: | ||
print("Erreur ! Veuillez donnez deux arguments, le nom du fichier de poids et de l'image à prédire.") | ||
exit() | ||
|
||
# On charge les poids | ||
filename = sys.argv[1] | ||
try: | ||
weights = np.load(filename) | ||
except FileNotFoundError: | ||
print("Erreur ! Le fichier de poids n'a pas pu être ouvert, vérifiez qu'il existe bien.") | ||
print("Fichier : ",filename) | ||
exit() | ||
|
||
# On charge l'image | ||
img = sys.argv[2] | ||
try: | ||
img = image.imread(img) | ||
except FileNotFoundError: | ||
print("Erreur ! L'image n'a pas pu être ouverte, vérifiez qu'elle existe bien.") | ||
print("Image : ",img) | ||
exit() | ||
|
||
def predire(X, weights): | ||
a = [X] | ||
for w in weights: | ||
a.append(np.maximum(a[-1].dot(w),0)) | ||
return a | ||
|
||
out = {} | ||
topred = 1 - img.reshape(784,4).mean(axis=1) | ||
prediction = predire(topred, weights)[-1] | ||
out['accuracy'] = list(prediction) | ||
out['prediction'] = int(np.argmax(prediction)) | ||
out_json = json.dumps(out) | ||
print(out_json) |