{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "31ec7db3", "metadata": {}, "outputs": [], "source": [ "#https://www.youtube.com/watch?v=w8yWXqWQYmU\n", "\n", "data = np.array(data)\n", "m, n = data.shape\n", "np.random.shuffle(data) # shuffle before splitting into dev and training sets\n", "\n", "data_dev = data[0:1000].T\n", "Y_dev = data_dev[0]\n", "X_dev = data_dev[1:n]\n", "X_dev = X_dev / 255.\n", "\n", "data_train = data[1000:m].T\n", "Y_train = data_train[0]\n", "X_train = data_train[1:n]\n", "X_train = X_train / 255.\n", "_,m_train = X_train.shape" ] }, { "cell_type": "code", "execution_count": 2, "id": "4e2a280c", "metadata": {}, "outputs": [], "source": [ "def init_params():\n", " W1 = np.random.rand(10, 784) - 0.5\n", " b1 = np.random.rand(10, 1) - 0.5\n", " W2 = np.random.rand(10, 10) - 0.5\n", " b2 = np.random.rand(10, 1) - 0.5\n", " return W1, b1, W2, b2\n", "\n", "def ReLU(Z):\n", " return np.maximum(Z, 0)\n", "\n", "def softmax(Z):\n", " A = np.exp(Z) / sum(np.exp(Z))\n", " return A\n", " \n", "def forward_prop(W1, b1, W2, b2, X):\n", " \"\"\"W1,W2 are wights, b1,b2 are biasis X is the input\"\"\"\n", " Z1 = W1.dot(X) + b1 # value of layer 1 \n", " A1 = ReLU(Z1) # value of layer 1 after applying activation\n", " Z2 = W2.dot(A1) + b2 # value of layer 2\n", " A2 = softmax(Z2) # value of layer 2 after applying activation\n", " return Z1, A1, Z2, A2\n", "\n", "def ReLU_deriv(Z):\n", " return Z > 0\n", "\n", "def one_hot(Y):\n", " one_hot_Y = np.zeros((Y.size, Y.max() + 1))\n", " one_hot_Y[np.arange(Y.size), Y] = 1\n", " one_hot_Y = one_hot_Y.T\n", " return one_hot_Y\n", "\n", "def backward_prop(Z1, A1, Z2, A2, W1, W2, X, Y):\n", " one_hot_Y = one_hot(Y)\n", " dZ2 = A2 - one_hot_Y #by how much layer 2 is off\n", " dW2 = 1 / m * dZ2.dot(A1.T) # how much the weight contributed for the error of layer 2\n", " db2 = 1 / m * np.sum(dZ2) # how much the bias contributed for the error of layer 2\n", " dZ1 = W2.T.dot(dZ2) * ReLU_deriv(Z1) #by how much layer 1 is off\n", " dW1 = 1 / m * dZ1.dot(X.T)\n", " db1 = 1 / m * np.sum(dZ1)\n", " return dW1, db1, dW2, db2\n", "\n", "def update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha):\n", " \"\"\"alpha is the learning rate, by how much we want to change the weights and biasis each epoch\"\"\"\n", " W1 = W1 - alpha * dW1\n", " b1 = b1 - alpha * db1 \n", " W2 = W2 - alpha * dW2 \n", " b2 = b2 - alpha * db2 \n", " return W1, b1, W2, b2" ] }, { "cell_type": "code", "execution_count": 1, "id": "86bd1d48", "metadata": {}, "outputs": [], "source": [ "def get_predictions(A2):\n", " return np.argmax(A2, 0)\n", "\n", "def get_accuracy(predictions, Y):\n", " print(predictions, Y)\n", " return np.sum(predictions == Y) / Y.size\n", "\n", "def gradient_descent(X, Y, alpha, iterations):\n", " W1, b1, W2, b2 = init_params()\n", " for i in range(iterations):\n", " Z1, A1, Z2, A2 = forward_prop(W1, b1, W2, b2, X)\n", " dW1, db1, dW2, db2 = backward_prop(Z1, A1, Z2, A2, W1, W2, X, Y)\n", " W1, b1, W2, b2 = update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)\n", " if i % 10 == 0: # print info for every 10th epoch\n", " print(\"Iteration: \", i)\n", " predictions = get_predictions(A2)\n", " print(get_accuracy(predictions, Y))\n", " return W1, b1, W2, b2" ] }, { "cell_type": "code", "execution_count": null, "id": "f1e3bf4e", "metadata": {}, "outputs": [], "source": [ "def make_predictions(X, W1, b1, W2, b2):\n", " _, _, _, A2 = forward_prop(W1, b1, W2, b2, X)\n", " predictions = get_predictions(A2)\n", " return predictions\n", "\n", "def test_prediction(index, W1, b1, W2, b2):\n", " current_image = X_train[:, index, None]\n", " prediction = make_predictions(X_train[:, index, None], W1, b1, W2, b2)\n", " label = Y_train[index]\n", " print(\"Prediction: \", prediction)\n", " print(\"Label: \", label)\n", " \n", " current_image = current_image.reshape((28, 28)) * 255\n", " plt.gray()\n", " plt.imshow(current_image, interpolation='nearest')\n", " plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }