diff --git a/__pycache__/knn.cpython-311.pyc b/__pycache__/knn.cpython-311.pyc index 8d7e7845954ee751abc727671f08df64a2dd4c24..f8b2c864ff5a35a5ee45a4aa909937ad1326d20a 100644 Binary files a/__pycache__/knn.cpython-311.pyc and b/__pycache__/knn.cpython-311.pyc differ diff --git a/__pycache__/mlp.cpython-311.pyc b/__pycache__/mlp.cpython-311.pyc index 14eb718b7f10cfb9e0df0a26c9232185a86fbcc1..bb937b794b6ea34fcb11112df2f42871158716af 100644 Binary files a/__pycache__/mlp.cpython-311.pyc and b/__pycache__/mlp.cpython-311.pyc differ diff --git a/main.py b/main.py index 324157472c24e419a3d19f7e0f711a4a40fa1c2d..1f99474e688214777398c3c9261b49762df54245 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ 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 * + diff --git a/resultats/mlp.png b/resultats/mlp.png index 9243fc8ec1de2f9d6babe5f203e7ae9f38e7a554..c9cd4d59f9330fd1e902560bc908bbf8b114ef25 100644 Binary files a/resultats/mlp.png and b/resultats/mlp.png differ diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..78e3810e9376ff01bad7e05bf618273c28992e47 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,5 @@ +import os +import sys +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + + diff --git a/tests/test_distance_matrix.py b/tests/test_distance_matrix.py new file mode 100644 index 0000000000000000000000000000000000000000..f602997877bc8d1b6e9103df2b26ef7cdb25ef80 --- /dev/null +++ b/tests/test_distance_matrix.py @@ -0,0 +1,10 @@ +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() diff --git a/tests/test_evaluate_knn.py b/tests/test_evaluate_knn.py new file mode 100644 index 0000000000000000000000000000000000000000..c9f1d86af11d1509398a64c43fc81b5c7f89e413 --- /dev/null +++ b/tests/test_evaluate_knn.py @@ -0,0 +1,10 @@ +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 diff --git a/tests/test_knn_predict.py b/tests/test_knn_predict.py new file mode 100644 index 0000000000000000000000000000000000000000..01f332e657bdff98000214f8811fb1c5e68ac7ee --- /dev/null +++ b/tests/test_knn_predict.py @@ -0,0 +1,12 @@ +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 diff --git a/tests/test_learn_once_cross_entropy.py b/tests/test_learn_once_cross_entropy.py new file mode 100644 index 0000000000000000000000000000000000000000..bc45a7c36f1d4c81686bb4c7684437aaaa26e591 --- /dev/null +++ b/tests/test_learn_once_cross_entropy.py @@ -0,0 +1,20 @@ +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 diff --git a/tests/test_learn_once_mse.py b/tests/test_learn_once_mse.py new file mode 100644 index 0000000000000000000000000000000000000000..09cd3a62da549e98c9296ca914845da907adf49d --- /dev/null +++ b/tests/test_learn_once_mse.py @@ -0,0 +1,23 @@ +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 diff --git a/tests/test_mode.py b/tests/test_mode.py new file mode 100644 index 0000000000000000000000000000000000000000..7a1b675207ea006ee7b68f4070283caffc9ac61b --- /dev/null +++ b/tests/test_mode.py @@ -0,0 +1,8 @@ +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 diff --git a/tests/test_one_hot.py b/tests/test_one_hot.py new file mode 100644 index 0000000000000000000000000000000000000000..4af3b889f3b5a48276a0d4e1138e7470a8d0472c --- /dev/null +++ b/tests/test_one_hot.py @@ -0,0 +1,8 @@ +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 diff --git a/tests/test_predict_mlp.py b/tests/test_predict_mlp.py new file mode 100644 index 0000000000000000000000000000000000000000..1ad3a103b8833598b876b226f1a05c709c54c7e9 --- /dev/null +++ b/tests/test_predict_mlp.py @@ -0,0 +1,19 @@ +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 diff --git a/tests/test_read_cifar.py b/tests/test_read_cifar.py new file mode 100644 index 0000000000000000000000000000000000000000..a86881ba20b80ec97a597e98a4a4926ec92882e2 --- /dev/null +++ b/tests/test_read_cifar.py @@ -0,0 +1,8 @@ +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 diff --git a/tests/test_run_mlp_training.py b/tests/test_run_mlp_training.py new file mode 100644 index 0000000000000000000000000000000000000000..826d564b9adf7bc9ad6a83a0fbfb8a896bb0b7f6 --- /dev/null +++ b/tests/test_run_mlp_training.py @@ -0,0 +1,15 @@ +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 diff --git a/tests/test_softmax.py b/tests/test_softmax.py new file mode 100644 index 0000000000000000000000000000000000000000..a669a4c7dffe562b41174c712606b4e3208a077c --- /dev/null +++ b/tests/test_softmax.py @@ -0,0 +1,8 @@ +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 diff --git a/tests/test_split_dataset.py b/tests/test_split_dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..ad44fe31919d80426982a3e231011ef6b409ed9f --- /dev/null +++ b/tests/test_split_dataset.py @@ -0,0 +1,11 @@ +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 diff --git a/tests/test_train_mlp.py b/tests/test_train_mlp.py new file mode 100644 index 0000000000000000000000000000000000000000..854cbd9db668e338cbb1367dccfe3a20ca53dc33 --- /dev/null +++ b/tests/test_train_mlp.py @@ -0,0 +1,19 @@ +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 diff --git a/tests/test_unpickle.py b/tests/test_unpickle.py new file mode 100644 index 0000000000000000000000000000000000000000..88dda61d97aa5d4684bd50ad2dab3e0d081df630 --- /dev/null +++ b/tests/test_unpickle.py @@ -0,0 +1,4 @@ +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