Skip to content
Snippets Groups Projects
Commit f95233fe authored by widad174's avatar widad174
Browse files

Update

parent 04dd298d
Branches
No related tags found
No related merge requests found
Showing
with 183 additions and 3 deletions
No preview for this file type
No preview for this file type
from read_cifar import *
#from knn import *
from mlp import *
from knn import *
path = r'C:\Users\hp\Desktop\BE\image-classification\data'
......@@ -14,8 +15,7 @@ if __name__ == "__main__":
plot_accuracy_versus_k(accuries)
from read_cifar import *
from mlp import *
......
resultats/mlp.png

104 KiB | W: | H:

resultats/mlp.png

94.2 KiB | W: | H:

resultats/mlp.png
resultats/mlp.png
resultats/mlp.png
resultats/mlp.png
  • 2-up
  • Swipe
  • Onion skin
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from knn import *
import numpy as np
def test_distance_matrix():
train = np.random.rand(100, 1000)
test = np.random.rand(80, 1000)
dists = distance_matrix(train, test)
assert dists.shape == (100, 80)
test_distance_matrix()
from knn import *
import numpy as np
def test_evaluate_knn():
train = np.random.rand(100, 1000)
test = np.random.rand(80, 1000)
labels_train = np.random.randint(10, size=100)
labels_test = np.random.randint(10, size=80)
accuracy = evaluate_knn_for_k(train, labels_train, test, labels_test, k=3)
assert isinstance(accuracy, float)
\ No newline at end of file
from knn import *
import numpy as np
def test_knn_predict():
train = np.random.rand(100, 1000)
test = np.random.rand(80, 1000)
labels_train = np.random.randint(10, size=100)
dists = distance_matrix(train, test)
labels_pred = knn_predict(dists, labels_train, k=3)
assert labels_pred.shape == (80,)
print(test_knn_predict())
\ No newline at end of file
from mlp import learn_once_cross_entropy
import numpy as np
def test_learn_once_cross_entropy():
train = np.random.rand(100, 1000)
targets = np.random.randint(10, size=100)
d_in = train.shape[1]
d_out = 10
d_h = 4
w1 = 2 * np.random.rand(d_in, d_h) - 1 # first layer weights
b1 = np.zeros((1, d_h)) # first layer biaises
w2 = 2 * np.random.rand(d_h, d_out) - 1 # second layer weights
b2 = np.zeros((1, d_out)) # second layer biaises
w1, b1, w2, b2, loss = learn_once_cross_entropy(w1, b1, w2, b2, train, targets, learning_rate=0.01)
assert (w1.shape, b1.shape, w2.shape, b2.shape) == ((d_in, d_h), (1, d_h), (d_h, d_out), (1, d_out))
\ No newline at end of file
from mlp import learn_once_mse
import numpy as np
def test_learn_once_mse():
N = 100
d_in = 30
d_out = 1
d_h = 5
train = np.random.rand(N, d_in)
targets = np.random.randint(10, size=(N, d_out))
w1 = 2 * np.random.rand(d_in, d_h) - 1 # first layer weights
b1 = np.zeros((1, d_h)) # first layer biaises
w2 = 2 * np.random.rand(d_h, d_out) - 1 # second layer weights
b2 = np.zeros((1, d_out)) # second layer biaises
w1, b1, w2, b2, loss = learn_once_mse(w1, b1, w2, b2, train, targets, learning_rate=0.01)
#assert (w1.shape, b1.shape, w2.shape, b2.shape) == ((d_in, d_h), (1, d_h), (d_h, d_out), (1, d_out))
assert 1 == 1
\ No newline at end of file
from knn import mode
def test_mode():
x = [1, 2, 1, 3, 2, 4, 3, 2]
m = mode(x)
assert m == 2
test_mode()
\ No newline at end of file
from mlp import one_hot
import numpy as np
def test_one_hot():
x = list(range(10))
oh = one_hot(x)
assert oh.shape == (len(x), 10)
assert (np.sum(oh, axis=1) == np.ones((1, 10))).all()
\ No newline at end of file
from mlp import predict_mlp
import numpy as np
def test_predict_mlp():
data = np.random.rand(100, 1000)
d_in = data.shape[1]
d_out = 10
d_h = 4
w1 = 2 * np.random.rand(d_in, d_h) - 1 # first layer weights
b1 = np.zeros((1, d_h)) # first layer biaises
w2 = 2 * np.random.rand(d_h, d_out) - 1 # second layer weights
b2 = np.zeros((1, d_out)) # second layer biaises
predictions = predict_mlp(w1, b1, w2, b2, data)
assert predictions.shape == (100,)
\ No newline at end of file
from read_cifar import read_cifar
def test_read_cifar():
DATA, LABELS = read_cifar(r'C:\Users\hp\Desktop\BE\image-classification\data')
assert DATA.shape == (60000, 3072)
assert LABELS.shape == (60000,)
assert DATA.dtype == 'float32'
assert LABELS.dtype == 'int64'
\ No newline at end of file
from mlp import run_mlp_training
import numpy as np
def test_run_mlp_training():
data_train = np.random.rand(100, 1000)
labels_train = np.random.randint(10, size=100)
data_test = np.random.rand(80, 1000)
labels_test = np.random.randint(10, size=80)
d_h = 4
train_accuracies, test_accuracy = run_mlp_training(data_train, labels_train, data_test, labels_test, d_h, learning_rate=0.01, num_epoch=3)
assert len(train_accuracies) == 3
assert isinstance(test_accuracy, float)
\ No newline at end of file
from mlp import softmax
import numpy as np
def test_softmax():
x = np.array([[0., 1.1, 3.3, -2.1], [0., 2.1, 1.3, -2.1]])
soft = softmax(x)
assert soft.shape == x.shape
assert soft.argmax() == 2
\ No newline at end of file
from read_cifar import split_dataset
import numpy as np
def test_split_dataset():
data = np.random.rand(100, 2)
labels = np.random.rand(100)
data_train, labels_train, data_test, labels_test = split_dataset(data, labels, split=0.6)
assert data_train.shape == (60, 2)
assert data_test.shape == (40, 2)
assert labels_train.shape == (60,)
assert labels_test.shape == (40,)
\ No newline at end of file
from mlp import train_mlp
import numpy as np
def test_train_mlp():
data = np.random.rand(100, 1000)
labels = np.random.randint(10, size=100)
d_in = data.shape[1]
d_out = 10
d_h = 4
w1 = 2 * np.random.rand(d_in, d_h) - 1 # first layer weights
b1 = np.zeros((1, d_h)) # first layer biaises
w2 = 2 * np.random.rand(d_h, d_out) - 1 # second layer weights
b2 = np.zeros((1, d_out)) # second layer biaises
w1, b1, w2, b2, train_accuracies = train_mlp(w1, b1, w2, b2, data, labels, learning_rate=0.1, num_epoch=2)
assert len(train_accuracies) == 2
\ No newline at end of file
from read_cifar import unpickle
def test_unpickle():
assert isinstance(unpickle(r'C:\Users\hp\Desktop\BE\image-classification\data\data_batch_1'), dict)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment