{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":63087,"databundleVersionId":6874572,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:38:12.503376Z","iopub.execute_input":"2024-02-16T10:38:12.503733Z","iopub.status.idle":"2024-02-16T10:38:13.461649Z","shell.execute_reply.started":"2024-02-16T10:38:12.503707Z","shell.execute_reply":"2024-02-16T10:38:13.460802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:38:15.527268Z","iopub.execute_input":"2024-02-16T10:38:15.527794Z","iopub.status.idle":"2024-02-16T10:38:15.532495Z","shell.execute_reply.started":"2024-02-16T10:38:15.527765Z","shell.execute_reply":"2024-02-16T10:38:15.531637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/copy-of-digit-recognizer-testing/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:41:19.725707Z","iopub.execute_input":"2024-02-16T10:41:19.726026Z","iopub.status.idle":"2024-02-16T10:41:22.062275Z","shell.execute_reply.started":"2024-02-16T10:41:19.725999Z","shell.execute_reply":"2024-02-16T10:41:22.061331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:41:27.893857Z","iopub.execute_input":"2024-02-16T10:41:27.894209Z","iopub.status.idle":"2024-02-16T10:41:27.915434Z","shell.execute_reply.started":"2024-02-16T10:41:27.894183Z","shell.execute_reply":"2024-02-16T10:41:27.914782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df['label']\nx = df.drop('label',axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:42:28.587420Z","iopub.execute_input":"2024-02-16T10:42:28.587751Z","iopub.status.idle":"2024-02-16T10:42:28.650301Z","shell.execute_reply.started":"2024-02-16T10:42:28.587727Z","shell.execute_reply":"2024-02-16T10:42:28.649648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:42:32.527791Z","iopub.execute_input":"2024-02-16T10:42:32.528099Z","iopub.status.idle":"2024-02-16T10:42:32.535515Z","shell.execute_reply.started":"2024-02-16T10:42:32.528075Z","shell.execute_reply":"2024-02-16T10:42:32.534507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = x.iloc[0,:].values.reshape(28,28)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:42:35.373442Z","iopub.execute_input":"2024-02-16T10:42:35.373794Z","iopub.status.idle":"2024-02-16T10:42:35.377634Z","shell.execute_reply.started":"2024-02-16T10:42:35.373767Z","shell.execute_reply":"2024-02-16T10:42:35.376984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:42:38.912766Z","iopub.execute_input":"2024-02-16T10:42:38.913072Z","iopub.status.idle":"2024-02-16T10:42:39.166597Z","shell.execute_reply.started":"2024-02-16T10:42:38.913047Z","shell.execute_reply":"2024-02-16T10:42:39.165914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1 = x.iloc[1,:].values.reshape(28,28)\nplt.imshow(img1)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:42:43.350604Z","iopub.execute_input":"2024-02-16T10:42:43.351527Z","iopub.status.idle":"2024-02-16T10:42:43.505147Z","shell.execute_reply.started":"2024-02-16T10:42:43.351494Z","shell.execute_reply":"2024-02-16T10:42:43.504416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img2 = x.iloc[3,:].values.reshape(28,28)\nplt.imshow(img2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = x/255 # Scaling -> Xs = X-Xmin / Xmax-Xmin (here for pixel min value is 0 and max is 255)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:42:52.907202Z","iopub.execute_input":"2024-02-16T10:42:52.907511Z","iopub.status.idle":"2024-02-16T10:42:52.967063Z","shell.execute_reply.started":"2024-02-16T10:42:52.907487Z","shell.execute_reply":"2024-02-16T10:42:52.966031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:42:56.711140Z","iopub.execute_input":"2024-02-16T10:42:56.711493Z","iopub.status.idle":"2024-02-16T10:43:09.882675Z","shell.execute_reply.started":"2024-02-16T10:42:56.711451Z","shell.execute_reply":"2024-02-16T10:43:09.881739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ya = to_categorical(y,num_classes=10)\nya.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:43:17.434620Z","iopub.execute_input":"2024-02-16T10:43:17.435199Z","iopub.status.idle":"2024-02-16T10:43:17.443526Z","shell.execute_reply.started":"2024-02-16T10:43:17.435171Z","shell.execute_reply":"2024-02-16T10:43:17.442238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Build","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:43:21.603677Z","iopub.execute_input":"2024-02-16T10:43:21.604010Z","iopub.status.idle":"2024-02-16T10:43:21.611502Z","shell.execute_reply.started":"2024-02-16T10:43:21.603985Z","shell.execute_reply":"2024-02-16T10:43:21.610035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Batch GD\nmodel = Sequential()\nmodel.add(Dense(16,activation='relu',input_shape=(784,)))\nmodel.add(Dense(14,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy',metrics=['accuracy'])\nh1 = model.fit(x,ya,batch_size=42000,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:43:25.193568Z","iopub.execute_input":"2024-02-16T10:43:25.193884Z","iopub.status.idle":"2024-02-16T10:43:28.602225Z","shell.execute_reply.started":"2024-02-16T10:43:25.193860Z","shell.execute_reply":"2024-02-16T10:43:28.601448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mini-batch GD\nmodel = Sequential()\nmodel.add(Dense(16,activation='relu',input_shape=(784,)))\nmodel.add(Dense(14,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy',metrics=['accuracy'])\nh2 = model.fit(x,ya,batch_size=64,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:43:30.593652Z","iopub.execute_input":"2024-02-16T10:43:30.593975Z","iopub.status.idle":"2024-02-16T10:43:41.503421Z","shell.execute_reply.started":"2024-02-16T10:43:30.593950Z","shell.execute_reply":"2024-02-16T10:43:41.502317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Stochastic GD\nmodel = Sequential()\nmodel.add(Dense(16,activation='relu',input_shape=(784,)))\nmodel.add(Dense(14,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy',metrics=['accuracy'])\nh3 = model.fit(x,ya,batch_size=1,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:44:03.608584Z","iopub.execute_input":"2024-02-16T10:44:03.608920Z","iopub.status.idle":"2024-02-16T10:54:26.150191Z","shell.execute_reply.started":"2024-02-16T10:44:03.608894Z","shell.execute_reply":"2024-02-16T10:54:26.149288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:54:37.865358Z","iopub.execute_input":"2024-02-16T10:54:37.865726Z","iopub.status.idle":"2024-02-16T10:54:37.883404Z","shell.execute_reply.started":"2024-02-16T10:54:37.865698Z","shell.execute_reply":"2024-02-16T10:54:37.882013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(h1.history['loss'],marker='x',c='red',label='BGD')\n\nplt.plot(h2.history['loss'],marker='x',c='blue',label='Mini-BGD')\n\nplt.plot(h3.history['loss'],marker='x',c='black',label='stochastic')\n\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:54:48.585372Z","iopub.execute_input":"2024-02-16T10:54:48.586674Z","iopub.status.idle":"2024-02-16T10:54:48.769753Z","shell.execute_reply.started":"2024-02-16T10:54:48.586590Z","shell.execute_reply":"2024-02-16T10:54:48.768624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[:5]","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:55:04.870833Z","iopub.execute_input":"2024-02-16T10:55:04.871169Z","iopub.status.idle":"2024-02-16T10:55:04.879253Z","shell.execute_reply.started":"2024-02-16T10:55:04.871141Z","shell.execute_reply":"2024-02-16T10:55:04.877994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = x.iloc[0,:].values.reshape(1,784)\nmodel.predict(img)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:55:08.282683Z","iopub.execute_input":"2024-02-16T10:55:08.283053Z","iopub.status.idle":"2024-02-16T10:55:08.403931Z","shell.execute_reply.started":"2024-02-16T10:55:08.283023Z","shell.execute_reply":"2024-02-16T10:55:08.403289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(img).max()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:55:12.495628Z","iopub.execute_input":"2024-02-16T10:55:12.495989Z","iopub.status.idle":"2024-02-16T10:55:12.560675Z","shell.execute_reply.started":"2024-02-16T10:55:12.495961Z","shell.execute_reply":"2024-02-16T10:55:12.559706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(img).argmax()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:55:15.697124Z","iopub.execute_input":"2024-02-16T10:55:15.697537Z","iopub.status.idle":"2024-02-16T10:55:15.760765Z","shell.execute_reply.started":"2024-02-16T10:55:15.697499Z","shell.execute_reply":"2024-02-16T10:55:15.759423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict_on_batch(x.iloc[:5,:]).argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T10:55:19.723315Z","iopub.execute_input":"2024-02-16T10:55:19.723849Z","iopub.status.idle":"2024-02-16T10:55:19.781998Z","shell.execute_reply.started":"2024-02-16T10:55:19.723822Z","shell.execute_reply":"2024-02-16T10:55:19.781051Z"},"trusted":true},"execution_count":null,"outputs":[]}]}