{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":10749413,"sourceType":"datasetVersion","datasetId":6666403}],"dockerImageVersionId":30886,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Highlishts\n- 05 Fastbook Chapter\n- ","metadata":{}},{"cell_type":"markdown","source":"## Neural Network With Numpy & Math\n\n","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pwd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:34.077087Z","iopub.execute_input":"2025-02-14T12:37:34.077472Z","iopub.status.idle":"2025-02-14T12:37:34.198482Z","shell.execute_reply.started":"2025-02-14T12:37:34.077445Z","shell.execute_reply":"2025-02-14T12:37:34.197209Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Lecture\n- https://www.youtube.com/watch?v=w8yWXqWQYmU\n- https://medium.com/towards-data-science/perceptron-learning-algorithm-d5db0deab975","metadata":{}},{"cell_type":"code","source":"# Imports PIL module\nfrom PIL import Image\nim=[]\nfor i in range(1,15):\n    im.append(Image.open(f\"/kaggle/input/picsoftutorial/{i}.png\"))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:35.189626Z","iopub.execute_input":"2025-02-14T12:37:35.190048Z","iopub.status.idle":"2025-02-14T12:37:35.234210Z","shell.execute_reply.started":"2025-02-14T12:37:35.190013Z","shell.execute_reply":"2025-02-14T12:37:35.233124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[6]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:35.677616Z","iopub.execute_input":"2025-02-14T12:37:35.677954Z","iopub.status.idle":"2025-02-14T12:37:35.864923Z","shell.execute_reply.started":"2025-02-14T12:37:35.677930Z","shell.execute_reply":"2025-02-14T12:37:35.863492Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:37.419571Z","iopub.execute_input":"2025-02-14T12:37:37.419944Z","iopub.status.idle":"2025-02-14T12:37:37.589630Z","shell.execute_reply.started":"2025-02-14T12:37:37.419910Z","shell.execute_reply":"2025-02-14T12:37:37.588396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:37.638633Z","iopub.execute_input":"2025-02-14T12:37:37.639036Z","iopub.status.idle":"2025-02-14T12:37:37.748348Z","shell.execute_reply.started":"2025-02-14T12:37:37.638998Z","shell.execute_reply":"2025-02-14T12:37:37.747152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[2]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:37.834341Z","iopub.execute_input":"2025-02-14T12:37:37.834660Z","iopub.status.idle":"2025-02-14T12:37:38.064041Z","shell.execute_reply.started":"2025-02-14T12:37:37.834637Z","shell.execute_reply":"2025-02-14T12:37:38.062928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:38.065268Z","iopub.execute_input":"2025-02-14T12:37:38.065562Z","iopub.status.idle":"2025-02-14T12:37:38.069821Z","shell.execute_reply.started":"2025-02-14T12:37:38.065537Z","shell.execute_reply":"2025-02-14T12:37:38.068534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\ndf =pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:38.208478Z","iopub.execute_input":"2025-02-14T12:37:38.208828Z","iopub.status.idle":"2025-02-14T12:37:41.136106Z","shell.execute_reply.started":"2025-02-14T12:37:38.208802Z","shell.execute_reply":"2025-02-14T12:37:41.135134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:41.137493Z","iopub.execute_input":"2025-02-14T12:37:41.137836Z","iopub.status.idle":"2025-02-14T12:37:41.156360Z","shell.execute_reply.started":"2025-02-14T12:37:41.137807Z","shell.execute_reply":"2025-02-14T12:37:41.155197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_x = df.iloc[:,1:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:41.158287Z","iopub.execute_input":"2025-02-14T12:37:41.158665Z","iopub.status.idle":"2025-02-14T12:37:41.171911Z","shell.execute_reply.started":"2025-02-14T12:37:41.158623Z","shell.execute_reply":"2025-02-14T12:37:41.170841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_x.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:41.173067Z","iopub.execute_input":"2025-02-14T12:37:41.173332Z","iopub.status.idle":"2025-02-14T12:37:41.207304Z","shell.execute_reply.started":"2025-02-14T12:37:41.173311Z","shell.execute_reply":"2025-02-14T12:37:41.206094Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Convert To Array and apply Transform\n- input in rows and columns","metadata":{}},{"cell_type":"code","source":"X = np.array(df_x).T\nX","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:41.472386Z","iopub.execute_input":"2025-02-14T12:37:41.472773Z","iopub.status.idle":"2025-02-14T12:37:41.573973Z","shell.execute_reply.started":"2025-02-14T12:37:41.472735Z","shell.execute_reply":"2025-02-14T12:37:41.572797Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Extracting Label","metadata":{}},{"cell_type":"code","source":"Y = np.array(df.iloc[:,:1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:41.834356Z","iopub.execute_input":"2025-02-14T12:37:41.834692Z","iopub.status.idle":"2025-02-14T12:37:41.839928Z","shell.execute_reply.started":"2025-02-14T12:37:41.834666Z","shell.execute_reply":"2025-02-14T12:37:41.838550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:42.054417Z","iopub.execute_input":"2025-02-14T12:37:42.054772Z","iopub.status.idle":"2025-02-14T12:37:42.061054Z","shell.execute_reply.started":"2025-02-14T12:37:42.054744Z","shell.execute_reply":"2025-02-14T12:37:42.059876Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[3]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:42.239494Z","iopub.execute_input":"2025-02-14T12:37:42.239875Z","iopub.status.idle":"2025-02-14T12:37:42.387940Z","shell.execute_reply.started":"2025-02-14T12:37:42.239845Z","shell.execute_reply":"2025-02-14T12:37:42.386878Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Checking Dimension\n- 42000 images in columns","metadata":{}},{"cell_type":"code","source":"X.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:42.620434Z","iopub.execute_input":"2025-02-14T12:37:42.620787Z","iopub.status.idle":"2025-02-14T12:37:42.626829Z","shell.execute_reply.started":"2025-02-14T12:37:42.620758Z","shell.execute_reply":"2025-02-14T12:37:42.625846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import math\nmath.sqrt(X.shape[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:44.410789Z","iopub.execute_input":"2025-02-14T12:37:44.411182Z","iopub.status.idle":"2025-02-14T12:37:44.417380Z","shell.execute_reply.started":"2025-02-14T12:37:44.411116Z","shell.execute_reply":"2025-02-14T12:37:44.416154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"math.sqrt(X.shape[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:44.662486Z","iopub.execute_input":"2025-02-14T12:37:44.662841Z","iopub.status.idle":"2025-02-14T12:37:44.669324Z","shell.execute_reply.started":"2025-02-14T12:37:44.662813Z","shell.execute_reply":"2025-02-14T12:37:44.668154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[4]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:44.852390Z","iopub.execute_input":"2025-02-14T12:37:44.852748Z","iopub.status.idle":"2025-02-14T12:37:45.000305Z","shell.execute_reply.started":"2025-02-14T12:37:44.852699Z","shell.execute_reply":"2025-02-14T12:37:44.999293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Visualise Image","metadata":{}},{"cell_type":"code","source":"height , width = int(math.sqrt(X.shape[0])), int(math.sqrt(X.shape[0]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:46.959332Z","iopub.execute_input":"2025-02-14T12:37:46.959658Z","iopub.status.idle":"2025-02-14T12:37:46.964788Z","shell.execute_reply.started":"2025-02-14T12:37:46.959633Z","shell.execute_reply":"2025-02-14T12:37:46.963431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img0 = X[:,0].reshape(height,width)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:47.171622Z","iopub.execute_input":"2025-02-14T12:37:47.171997Z","iopub.status.idle":"2025-02-14T12:37:47.176354Z","shell.execute_reply.started":"2025-02-14T12:37:47.171969Z","shell.execute_reply":"2025-02-14T12:37:47.175257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(img0, cmap=\"gray\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:47.328443Z","iopub.execute_input":"2025-02-14T12:37:47.328842Z","iopub.status.idle":"2025-02-14T12:37:47.524827Z","shell.execute_reply.started":"2025-02-14T12:37:47.328809Z","shell.execute_reply":"2025-02-14T12:37:47.523670Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img1 = X[:,1].reshape(height,width)\nplt.imshow(img1, cmap=\"gray\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:49.814947Z","iopub.execute_input":"2025-02-14T12:37:49.815346Z","iopub.status.idle":"2025-02-14T12:37:50.008166Z","shell.execute_reply.started":"2025-02-14T12:37:49.815313Z","shell.execute_reply":"2025-02-14T12:37:50.007048Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Initialise Weights and Bias\n- weights = 10 x 784 , X = 784 x (no. of images)\n- 1 neuron for each label weights for 0 label 1 x 784\n- 1 neuron for each label weights for 1 label 1 x 784\n- 1 neuron for each label weights for 2 label 1 x 784\n- 1 neuron for each label weights for 3 label 1 x 784\n- 1 neuron for each label weights for 4 label 1 x 784\n- .....\n- Total : 10 for 1 to 9 images","metadata":{}},{"cell_type":"code","source":"W = np.random.random((10, 784))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:50.222690Z","iopub.execute_input":"2025-02-14T12:37:50.223045Z","iopub.status.idle":"2025-02-14T12:37:50.227986Z","shell.execute_reply.started":"2025-02-14T12:37:50.223016Z","shell.execute_reply":"2025-02-14T12:37:50.226656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(W[0,:]) # 0th image size 784","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:50.408478Z","iopub.execute_input":"2025-02-14T12:37:50.408872Z","iopub.status.idle":"2025-02-14T12:37:50.415496Z","shell.execute_reply.started":"2025-02-14T12:37:50.408839Z","shell.execute_reply":"2025-02-14T12:37:50.414092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"B = np.random.random((10,1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:50.642761Z","iopub.execute_input":"2025-02-14T12:37:50.643128Z","iopub.status.idle":"2025-02-14T12:37:50.648012Z","shell.execute_reply.started":"2025-02-14T12:37:50.643094Z","shell.execute_reply":"2025-02-14T12:37:50.646775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[5]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:50.803952Z","iopub.execute_input":"2025-02-14T12:37:50.804324Z","iopub.status.idle":"2025-02-14T12:37:50.958296Z","shell.execute_reply.started":"2025-02-14T12:37:50.804295Z","shell.execute_reply":"2025-02-14T12:37:50.957137Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 0th image + Weights + Bias","metadata":{}},{"cell_type":"code","source":"sum( X[:,0] * W[0,:] , B[0,:])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:51.539103Z","iopub.execute_input":"2025-02-14T12:37:51.539507Z","iopub.status.idle":"2025-02-14T12:37:51.547856Z","shell.execute_reply.started":"2025-02-14T12:37:51.539474Z","shell.execute_reply":"2025-02-14T12:37:51.546835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sum( X[:,0] * W[0,:] ) + B[0,:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:51.756185Z","iopub.execute_input":"2025-02-14T12:37:51.756544Z","iopub.status.idle":"2025-02-14T12:37:51.763307Z","shell.execute_reply.started":"2025-02-14T12:37:51.756512Z","shell.execute_reply":"2025-02-14T12:37:51.761996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X[:,0] @ W[0,:] + B[0,:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:51.984253Z","iopub.execute_input":"2025-02-14T12:37:51.984562Z","iopub.status.idle":"2025-02-14T12:37:51.991308Z","shell.execute_reply.started":"2025-02-14T12:37:51.984538Z","shell.execute_reply":"2025-02-14T12:37:51.990327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[6]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:53.005690Z","iopub.execute_input":"2025-02-14T12:37:53.006042Z","iopub.status.idle":"2025-02-14T12:37:53.142914Z","shell.execute_reply.started":"2025-02-14T12:37:53.006017Z","shell.execute_reply":"2025-02-14T12:37:53.141669Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Activation\n- sigmoid\n- Relu\n- Softmax","metadata":{}},{"cell_type":"code","source":"def sigmoid(x):\n  return 1 / (1 + math.exp(-x))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:53.493953Z","iopub.execute_input":"2025-02-14T12:37:53.494333Z","iopub.status.idle":"2025-02-14T12:37:53.498692Z","shell.execute_reply.started":"2025-02-14T12:37:53.494302Z","shell.execute_reply":"2025-02-14T12:37:53.497503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def relu(x):\n    return np.maximum(0, x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:37:53.684296Z","iopub.execute_input":"2025-02-14T12:37:53.684621Z","iopub.status.idle":"2025-02-14T12:37:53.688996Z","shell.execute_reply.started":"2025-02-14T12:37:53.684597Z","shell.execute_reply":"2025-02-14T12:37:53.688010Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.special import softmax","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:00.241677Z","iopub.execute_input":"2025-02-14T12:38:00.242069Z","iopub.status.idle":"2025-02-14T12:38:00.351090Z","shell.execute_reply.started":"2025-02-14T12:38:00.242039Z","shell.execute_reply":"2025-02-14T12:38:00.350086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"relu(X[:,0] @ W[0,:] + B[0,:])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:03.546379Z","iopub.execute_input":"2025-02-14T12:38:03.546928Z","iopub.status.idle":"2025-02-14T12:38:03.553294Z","shell.execute_reply.started":"2025-02-14T12:38:03.546896Z","shell.execute_reply":"2025-02-14T12:38:03.552209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sigmoid((X[:,0] @ W[0,:] + B[0,:])[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:04.978997Z","iopub.execute_input":"2025-02-14T12:38:04.979353Z","iopub.status.idle":"2025-02-14T12:38:04.985743Z","shell.execute_reply.started":"2025-02-14T12:38:04.979322Z","shell.execute_reply":"2025-02-14T12:38:04.984476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"softmax((X[:,0] @ W[0,:] + B[0,:])[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:05.229041Z","iopub.execute_input":"2025-02-14T12:38:05.229372Z","iopub.status.idle":"2025-02-14T12:38:05.236171Z","shell.execute_reply.started":"2025-02-14T12:38:05.229346Z","shell.execute_reply":"2025-02-14T12:38:05.234887Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 1st and 2nd Layer","metadata":{}},{"cell_type":"code","source":"im[7]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:07.429287Z","iopub.execute_input":"2025-02-14T12:38:07.429670Z","iopub.status.idle":"2025-02-14T12:38:07.469866Z","shell.execute_reply.started":"2025-02-14T12:38:07.429638Z","shell.execute_reply":"2025-02-14T12:38:07.468694Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# m is 42000\nW.shape, X.shape, B.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:07.650992Z","iopub.execute_input":"2025-02-14T12:38:07.651329Z","iopub.status.idle":"2025-02-14T12:38:07.657832Z","shell.execute_reply.started":"2025-02-14T12:38:07.651301Z","shell.execute_reply":"2025-02-14T12:38:07.656391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# A_0 = X\n# W_1 = W\n# B_1 = B ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:07.861980Z","iopub.execute_input":"2025-02-14T12:38:07.862318Z","iopub.status.idle":"2025-02-14T12:38:07.866770Z","shell.execute_reply.started":"2025-02-14T12:38:07.862291Z","shell.execute_reply":"2025-02-14T12:38:07.865496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Z = W @ X + B","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:08.091871Z","iopub.execute_input":"2025-02-14T12:38:08.092206Z","iopub.status.idle":"2025-02-14T12:38:08.230540Z","shell.execute_reply.started":"2025-02-14T12:38:08.092180Z","shell.execute_reply":"2025-02-14T12:38:08.229367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"A = relu(Z)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:08.273925Z","iopub.execute_input":"2025-02-14T12:38:08.274255Z","iopub.status.idle":"2025-02-14T12:38:08.278824Z","shell.execute_reply.started":"2025-02-14T12:38:08.274229Z","shell.execute_reply":"2025-02-14T12:38:08.277835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"A.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:09.082792Z","iopub.execute_input":"2025-02-14T12:38:09.083156Z","iopub.status.idle":"2025-02-14T12:38:09.089124Z","shell.execute_reply.started":"2025-02-14T12:38:09.083126Z","shell.execute_reply":"2025-02-14T12:38:09.088001Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2nd layer","metadata":{}},{"cell_type":"code","source":"im[8]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:11.180829Z","iopub.execute_input":"2025-02-14T12:38:11.181174Z","iopub.status.idle":"2025-02-14T12:38:11.323328Z","shell.execute_reply.started":"2025-02-14T12:38:11.181147Z","shell.execute_reply":"2025-02-14T12:38:11.322258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"A.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:11.453113Z","iopub.execute_input":"2025-02-14T12:38:11.453454Z","iopub.status.idle":"2025-02-14T12:38:11.459660Z","shell.execute_reply.started":"2025-02-14T12:38:11.453430Z","shell.execute_reply":"2025-02-14T12:38:11.458290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W2 = np.random.random((10,10))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:13.139330Z","iopub.execute_input":"2025-02-14T12:38:13.139697Z","iopub.status.idle":"2025-02-14T12:38:13.144435Z","shell.execute_reply.started":"2025-02-14T12:38:13.139667Z","shell.execute_reply":"2025-02-14T12:38:13.143150Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"B2 = np.random.random((10,1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:13.390931Z","iopub.execute_input":"2025-02-14T12:38:13.391263Z","iopub.status.idle":"2025-02-14T12:38:13.395851Z","shell.execute_reply.started":"2025-02-14T12:38:13.391238Z","shell.execute_reply":"2025-02-14T12:38:13.394668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Z2 = W2 @ A + B2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:13.584836Z","iopub.execute_input":"2025-02-14T12:38:13.585175Z","iopub.status.idle":"2025-02-14T12:38:13.591618Z","shell.execute_reply.started":"2025-02-14T12:38:13.585147Z","shell.execute_reply":"2025-02-14T12:38:13.590594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Z2.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:13.827092Z","iopub.execute_input":"2025-02-14T12:38:13.827451Z","iopub.status.idle":"2025-02-14T12:38:13.833781Z","shell.execute_reply.started":"2025-02-14T12:38:13.827425Z","shell.execute_reply":"2025-02-14T12:38:13.832584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"A2 = softmax(Z2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:15.294696Z","iopub.execute_input":"2025-02-14T12:38:15.295137Z","iopub.status.idle":"2025-02-14T12:38:15.355274Z","shell.execute_reply.started":"2025-02-14T12:38:15.295105Z","shell.execute_reply":"2025-02-14T12:38:15.354081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"A2.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:15.516350Z","iopub.execute_input":"2025-02-14T12:38:15.516677Z","iopub.status.idle":"2025-02-14T12:38:15.522819Z","shell.execute_reply.started":"2025-02-14T12:38:15.516652Z","shell.execute_reply":"2025-02-14T12:38:15.521705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"A2[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:15.788795Z","iopub.execute_input":"2025-02-14T12:38:15.789120Z","iopub.status.idle":"2025-02-14T12:38:15.795086Z","shell.execute_reply.started":"2025-02-14T12:38:15.789095Z","shell.execute_reply":"2025-02-14T12:38:15.793929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[9]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:21.263253Z","iopub.execute_input":"2025-02-14T12:38:21.263659Z","iopub.status.idle":"2025-02-14T12:38:21.512476Z","shell.execute_reply.started":"2025-02-14T12:38:21.263616Z","shell.execute_reply":"2025-02-14T12:38:21.511242Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## One Hot Encoding of Label","metadata":{}},{"cell_type":"code","source":"Y[:10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:42:36.675871Z","iopub.execute_input":"2025-02-14T12:42:36.676243Z","iopub.status.idle":"2025-02-14T12:42:36.682599Z","shell.execute_reply.started":"2025-02-14T12:42:36.676209Z","shell.execute_reply":"2025-02-14T12:42:36.681496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = np.zeros((A2.shape[0] , A2.shape[1]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:22.021846Z","iopub.execute_input":"2025-02-14T12:38:22.022167Z","iopub.status.idle":"2025-02-14T12:38:22.026900Z","shell.execute_reply.started":"2025-02-14T12:38:22.022144Z","shell.execute_reply":"2025-02-14T12:38:22.025810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y.shape, Y.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:38:51.911949Z","iopub.execute_input":"2025-02-14T12:38:51.912284Z","iopub.status.idle":"2025-02-14T12:38:51.918854Z","shell.execute_reply.started":"2025-02-14T12:38:51.912258Z","shell.execute_reply":"2025-02-14T12:38:51.917662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ni=0\nfor ind_label in Y:\n    y[ind_label[0],i] = 1\n    i=i+1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:42:10.630648Z","iopub.execute_input":"2025-02-14T12:42:10.631037Z","iopub.status.idle":"2025-02-14T12:42:10.661348Z","shell.execute_reply.started":"2025-02-14T12:42:10.631007Z","shell.execute_reply":"2025-02-14T12:42:10.659899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im[13]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:48:10.459960Z","iopub.execute_input":"2025-02-14T12:48:10.460339Z","iopub.status.idle":"2025-02-14T12:48:10.558991Z","shell.execute_reply.started":"2025-02-14T12:48:10.460311Z","shell.execute_reply":"2025-02-14T12:48:10.557852Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y=y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:50:52.929698Z","iopub.execute_input":"2025-02-14T12:50:52.930081Z","iopub.status.idle":"2025-02-14T12:50:52.934662Z","shell.execute_reply.started":"2025-02-14T12:50:52.930053Z","shell.execute_reply":"2025-02-14T12:50:52.933298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"A2.shape , y.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:50:54.778832Z","iopub.execute_input":"2025-02-14T12:50:54.779155Z","iopub.status.idle":"2025-02-14T12:50:54.785474Z","shell.execute_reply.started":"2025-02-14T12:50:54.779130Z","shell.execute_reply":"2025-02-14T12:50:54.784268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"A2-Y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T12:50:59.344568Z","iopub.execute_input":"2025-02-14T12:50:59.344915Z","iopub.status.idle":"2025-02-14T12:50:59.352066Z","shell.execute_reply.started":"2025-02-14T12:50:59.344890Z","shell.execute_reply":"2025-02-14T12:50:59.351093Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#(np.square(A2 - Y)).mean(axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T13:03:56.615476Z","iopub.execute_input":"2025-02-14T13:03:56.615873Z","iopub.status.idle":"2025-02-14T13:03:56.620440Z","shell.execute_reply.started":"2025-02-14T13:03:56.615844Z","shell.execute_reply":"2025-02-14T13:03:56.619146Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss = np.square(np.subtract(A2,Y)).mean(axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T13:08:01.834702Z","iopub.execute_input":"2025-02-14T13:08:01.835084Z","iopub.status.idle":"2025-02-14T13:08:01.841345Z","shell.execute_reply.started":"2025-02-14T13:08:01.835054Z","shell.execute_reply":"2025-02-14T13:08:01.840204Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Derivatives\n\n- f'(x) = lim (h->0) [(f(x + h) - f(x)) / h]","metadata":{}},{"cell_type":"code","source":"im[12]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T13:11:37.932248Z","iopub.execute_input":"2025-02-14T13:11:37.932697Z","iopub.status.idle":"2025-02-14T13:11:38.001033Z","shell.execute_reply.started":"2025-02-14T13:11:37.932654Z","shell.execute_reply":"2025-02-14T13:11:38.000059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nim[11]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T13:11:55.757790Z","iopub.execute_input":"2025-02-14T13:11:55.758123Z","iopub.status.idle":"2025-02-14T13:11:55.873570Z","shell.execute_reply.started":"2025-02-14T13:11:55.758097Z","shell.execute_reply":"2025-02-14T13:11:55.872547Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Derivative of Loss W.R.T Weights\n- recreate Loss function\n- hifen","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\n# Step 1: Define the activation function (step function)\ndef unit_step_func(x):\n    return np.where(x >= 0, 1, 0)\n\n# Step 2: Initialize weights and bias\ndef initialize_weights(n_features):\n    weights = np.zeros(n_features)  # Initialize weights to 0\n    bias = 0  # Initialize bias to 0\n    return weights, bias\n\n# Step 3: Train the perceptron\ndef train_perceptron(X, y, learning_rate=0.01, n_iters=1000):\n    n_samples, n_features = X.shape\n    weights, bias = initialize_weights(n_features)\n\n    for _ in range(n_iters):\n        for idx, x_i in enumerate(X):\n            # Compute the linear output\n            linear_output = np.dot(x_i, weights) + bias\n            # Apply the activation function\n            y_pred = unit_step_func(linear_output)\n\n            # Update weights and bias\n            update = learning_rate * (y[idx] - y_pred)\n            if update:\n                print(\"update = \" , update)\n            weights += update * x_i\n            bias += update\n\n    return weights, bias\n\n# Step 4: Make predictions\ndef predict(X, weights, bias):\n    linear_output = np.dot(X, weights) + bias\n    y_pred = unit_step_func(linear_output)\n    return y_pred\n\n# Example usage\nif __name__ == \"__main__\":\n    # Example dataset (X = features, y = labels)\n    X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])  # Input features\n    y = np.array([0, 0, 0, 1])  # Labels (AND gate)\n\n    # Train the perceptron\n    weights, bias = train_perceptron(X, y, learning_rate=0.01, n_iters=1000)\n\n    # Make predictions\n    predictions = predict(X, weights, bias)\n    print(\"Weights:\", weights)\n    print(\"Bias:\", bias)\n    print(\"Predictions:\", predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T13:31:39.925606Z","iopub.execute_input":"2025-02-17T13:31:39.925929Z","iopub.status.idle":"2025-02-17T13:31:39.983892Z","shell.execute_reply.started":"2025-02-17T13:31:39.925905Z","shell.execute_reply":"2025-02-17T13:31:39.983119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}