{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-08T13:06:08.015806Z","iopub.execute_input":"2022-07-08T13:06:08.016724Z","iopub.status.idle":"2022-07-08T13:06:08.021198Z","shell.execute_reply.started":"2022-07-08T13:06:08.016671Z","shell.execute_reply":"2022-07-08T13:06:08.020171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datos = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\") #Obtención del Data set","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:06:08.032706Z","iopub.execute_input":"2022-07-08T13:06:08.033021Z","iopub.status.idle":"2022-07-08T13:06:10.658972Z","shell.execute_reply.started":"2022-07-08T13:06:08.032985Z","shell.execute_reply":"2022-07-08T13:06:10.657942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datos.head() #Visualizar la tabla","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:06:10.660622Z","iopub.execute_input":"2022-07-08T13:06:10.660838Z","iopub.status.idle":"2022-07-08T13:06:10.679295Z","shell.execute_reply.started":"2022-07-08T13:06:10.660812Z","shell.execute_reply":"2022-07-08T13:06:10.678288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datos = np.array(datos) #Los datos se se agrupan en un array\nm, n = datos.shape\nnp.random.shuffle(datos) # Desordenar datos\n\ndatos_dev = datos[0:1000].T\nY_dev = datos_dev[0]\nX_dev = datos_dev[1:n]\nX_dev = X_dev / 255.\n\ndatos_train = datos[1000:m].T #Inicializar los datos\nY_train = datos_train[0]\nX_train = datos_train[1:n]\nX_train = X_train / 255.\n_,m_train = X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:06:10.681218Z","iopub.execute_input":"2022-07-08T13:06:10.681543Z","iopub.status.idle":"2022-07-08T13:06:11.530351Z","shell.execute_reply.started":"2022-07-08T13:06:10.681495Z","shell.execute_reply":"2022-07-08T13:06:11.529402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(m)\nprint(n)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:06:11.532239Z","iopub.execute_input":"2022-07-08T13:06:11.532498Z","iopub.status.idle":"2022-07-08T13:06:11.536977Z","shell.execute_reply.started":"2022-07-08T13:06:11.532464Z","shell.execute_reply":"2022-07-08T13:06:11.536166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:06:11.538064Z","iopub.execute_input":"2022-07-08T13:06:11.538284Z","iopub.status.idle":"2022-07-08T13:06:11.549402Z","shell.execute_reply.started":"2022-07-08T13:06:11.538257Z","shell.execute_reply":"2022-07-08T13:06:11.548840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_params(): #Parámetros de Bias y Peso\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\ndef ReLU(Z): #Función ReLU\n    return np.maximum(Z, 0)\n\ndef softmax(Z): #Función Softmax\n    A = np.exp(Z) / sum(np.exp(Z))\n    return A\n\ndef forward_prop(W1, b1, W2, b2, X): #Forward porpagation\n    Z1 = W1.dot(X) + b1\n    A1 = ReLU(Z1)\n    Z2 = W2.dot(A1) + b2\n    A2 = softmax(Z2)\n    return Z1, A1, Z2, A2\n\ndef deriv_ReLU(Z): #Derivada de ReLU\n    return Z > 0\n\ndef 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\ndef back_prop(Z1, A1, Z2, A2, W1, W2, X, Y): #Backwards Propagation\n    one_hot_Y = one_hot(Y)\n    dZ2 = A2 - one_hot_Y\n    dW2 = 1 / m * dZ2.dot(A1.T)\n    db2 = 1 / m * np.sum(dZ2)\n    dZ1 = W2.T.dot(dZ2) * deriv_ReLU(Z1)\n    \n    dW1 = 1 / m * dZ1.dot(X.T)\n    db1 = 1 / m * np.sum(dZ1)\n    return dW1, db1, dW2, db2\n\ndef update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha): #Actualización de parámetros\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","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:06:11.550480Z","iopub.execute_input":"2022-07-08T13:06:11.550701Z","iopub.status.idle":"2022-07-08T13:06:11.562100Z","shell.execute_reply.started":"2022-07-08T13:06:11.550673Z","shell.execute_reply":"2022-07-08T13:06:11.561216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_predictions(A2):\n    return np.argmax(A2, 0)\n\ndef get_accuracy(predictions, Y): #Calcula la precisión\n    print(predictions, Y)\n    return np.sum(predictions == Y) / Y.size\n\ndef gradient_descent(X, Y, alpha, iterations): #Descenso del Gradiente\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 = back_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:\n            print(\"Iteration: \", i)\n            predictions = get_predictions(A2)\n            print(get_accuracy(predictions, Y))\n    return W1, b1, W2, b2","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:06:11.563346Z","iopub.execute_input":"2022-07-08T13:06:11.563832Z","iopub.status.idle":"2022-07-08T13:06:11.573207Z","shell.execute_reply.started":"2022-07-08T13:06:11.563788Z","shell.execute_reply":"2022-07-08T13:06:11.572373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 0.10, 500)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:06:11.574520Z","iopub.execute_input":"2022-07-08T13:06:11.575331Z","iopub.status.idle":"2022-07-08T13:07:18.921449Z","shell.execute_reply.started":"2022-07-08T13:06:11.575297Z","shell.execute_reply":"2022-07-08T13:07:18.920486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions(X, W1, b1, W2, b2):\n    _, _, _, A2 = forward_prop(W1, b1, W2, b2, X) #Hace la predicción\n    predictions = get_predictions(A2)\n    return predictions\n\ndef 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) #Muesta la predicción\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":{"execution":{"iopub.status.busy":"2022-07-08T13:07:18.923258Z","iopub.execute_input":"2022-07-08T13:07:18.924288Z","iopub.status.idle":"2022-07-08T13:07:18.933969Z","shell.execute_reply.started":"2022-07-08T13:07:18.924233Z","shell.execute_reply":"2022-07-08T13:07:18.933081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_prediction(0, W1, b1, W2, b2) #Resultados","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:44:01.245295Z","iopub.execute_input":"2022-07-08T13:44:01.246003Z","iopub.status.idle":"2022-07-08T13:44:01.422989Z","shell.execute_reply.started":"2022-07-08T13:44:01.245946Z","shell.execute_reply":"2022-07-08T13:44:01.422313Z"},"trusted":true},"execution_count":null,"outputs":[]}]}