{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:12.894055Z","iopub.execute_input":"2025-08-14T18:58:12.894343Z","iopub.status.idle":"2025-08-14T18:58:12.903237Z","shell.execute_reply.started":"2025-08-14T18:58:12.894326Z","shell.execute_reply":"2025-08-14T18:58:12.902226Z"}},"outputs":[],"execution_count":null},{"cell_type":"raw","source":"# Starting","metadata":{}},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten\n\nimport warnings \nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:12.905013Z","iopub.execute_input":"2025-08-14T18:58:12.905302Z","iopub.status.idle":"2025-08-14T18:58:12.922436Z","shell.execute_reply.started":"2025-08-14T18:58:12.905283Z","shell.execute_reply":"2025-08-14T18:58:12.92132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(X_train, y_train),(X_test,y_test) = keras.datasets.mnist.load_data()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:12.923801Z","iopub.execute_input":"2025-08-14T18:58:12.92422Z","iopub.status.idle":"2025-08-14T18:58:13.275339Z","shell.execute_reply.started":"2025-08-14T18:58:12.924145Z","shell.execute_reply":"2025-08-14T18:58:13.274204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train[0]\n# First Image of MNIST","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:13.27624Z","iopub.execute_input":"2025-08-14T18:58:13.27651Z","iopub.status.idle":"2025-08-14T18:58:13.285231Z","shell.execute_reply.started":"2025-08-14T18:58:13.276489Z","shell.execute_reply":"2025-08-14T18:58:13.284428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train[0]\n# Above data represents 5 ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:13.287337Z","iopub.execute_input":"2025-08-14T18:58:13.288128Z","iopub.status.idle":"2025-08-14T18:58:13.310197Z","shell.execute_reply.started":"2025-08-14T18:58:13.288079Z","shell.execute_reply":"2025-08-14T18:58:13.309141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Size of training data\nX_train.shape\n# It contain 60000 images","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:13.311514Z","iopub.execute_input":"2025-08-14T18:58:13.311901Z","iopub.status.idle":"2025-08-14T18:58:13.334759Z","shell.execute_reply.started":"2025-08-14T18:58:13.311869Z","shell.execute_reply":"2025-08-14T18:58:13.333713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Size of testing data\nX_test.shape\n# It contain 10000 images","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:13.336263Z","iopub.execute_input":"2025-08-14T18:58:13.336559Z","iopub.status.idle":"2025-08-14T18:58:13.356979Z","shell.execute_reply.started":"2025-08-14T18:58:13.336538Z","shell.execute_reply":"2025-08-14T18:58:13.355939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# To visualize image\nimport matplotlib.pyplot as plt\nplt.imshow(X_train[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:13.357961Z","iopub.execute_input":"2025-08-14T18:58:13.358267Z","iopub.status.idle":"2025-08-14T18:58:13.559562Z","shell.execute_reply.started":"2025-08-14T18:58:13.358247Z","shell.execute_reply":"2025-08-14T18:58:13.558457Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"We have to standerdize the data. Bring is in 0 to 1 range. ","metadata":{}},{"cell_type":"code","source":"X_train.shape[0]\nX_train_reshape.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:13.560877Z","iopub.execute_input":"2025-08-14T18:58:13.561141Z","iopub.status.idle":"2025-08-14T18:58:13.568023Z","shell.execute_reply.started":"2025-08-14T18:58:13.561122Z","shell.execute_reply":"2025-08-14T18:58:13.566896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from sklearn.preprocessing import MinMaxScaler\nX_train_reshape = X_train.reshape(X_train.shape[0],-1)\nX_test_reshape = X_test.reshape(X_test.shape[0],-1)\n\nmms = MinMaxScaler()\nX_train_scaled = mms.fit_transform(X_train_reshape)\nX_test_scaled = mms.transform(X_test_reshape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:13.570586Z","iopub.execute_input":"2025-08-14T18:58:13.570975Z","iopub.status.idle":"2025-08-14T18:58:14.253199Z","shell.execute_reply.started":"2025-08-14T18:58:13.57095Z","shell.execute_reply":"2025-08-14T18:58:14.252312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# As the upper method failed, as there are 3 dimensions\n# We can also impliment below\n# X_train = X_train/255\n# X_test = X_test/255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T18:58:14.254151Z","iopub.execute_input":"2025-08-14T18:58:14.254429Z","iopub.status.idle":"2025-08-14T18:58:14.258902Z","shell.execute_reply.started":"2025-08-14T18:58:14.254384Z","shell.execute_reply":"2025-08-14T18:58:14.257992Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model Building","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\n# model.add(Flatten(input_shape=(28,28)))\n# This should be done if It is not flatten in MinMax Scaler\n\nmodel.add(Dense(256,activation='relu', input_dim = 784))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(64, activation='relu' ))\nmodel.add(Dense(10,activation='softmax'))\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T19:12:50.904542Z","iopub.execute_input":"2025-08-14T19:12:50.904977Z","iopub.status.idle":"2025-08-14T19:12:50.998452Z","shell.execute_reply.started":"2025-08-14T19:12:50.904942Z","shell.execute_reply":"2025-08-14T19:12:50.995653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy', optimizer='adam',metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T19:15:31.605489Z","iopub.execute_input":"2025-08-14T19:15:31.606291Z","iopub.status.idle":"2025-08-14T19:15:31.618242Z","shell.execute_reply.started":"2025-08-14T19:15:31.606264Z","shell.execute_reply":"2025-08-14T19:15:31.617079Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"history = model.fit(X_train_scaled, y_train, epochs=20, validation_split=0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T19:17:14.670642Z","iopub.execute_input":"2025-08-14T19:17:14.670942Z","iopub.status.idle":"2025-08-14T19:19:58.16911Z","shell.execute_reply.started":"2025-08-14T19:17:14.670921Z","shell.execute_reply":"2025-08-14T19:19:58.168015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_prob = model.predict(X_test_scaled)\n# As it will give us the probability of being 0, 1, 2, ..., 9\n# So we have to find a maximum number","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T19:20:48.604065Z","iopub.execute_input":"2025-08-14T19:20:48.604416Z","iopub.status.idle":"2025-08-14T19:20:49.601606Z","shell.execute_reply.started":"2025-08-14T19:20:48.604378Z","shell.execute_reply":"2025-08-14T19:20:49.600398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = y_prob.argmax(axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T19:20:50.552694Z","iopub.execute_input":"2025-08-14T19:20:50.553066Z","iopub.status.idle":"2025-08-14T19:20:50.558931Z","shell.execute_reply.started":"2025-08-14T19:20:50.553039Z","shell.execute_reply":"2025-08-14T19:20:50.557683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nacc = accuracy_score(y_test, y_pred)\nprint(f\"Accuracy score is {acc*100:.2f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T19:20:53.058339Z","iopub.execute_input":"2025-08-14T19:20:53.058836Z","iopub.status.idle":"2025-08-14T19:20:53.068031Z","shell.execute_reply.started":"2025-08-14T19:20:53.058794Z","shell.execute_reply":"2025-08-14T19:20:53.066977Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"For 1 layer having 256, accuracy was `97.8%` accuracy. With 3 layers accuracy is `97.86%`","metadata":{}},{"cell_type":"markdown","source":"## So thats it for Today","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T19:21:59.411966Z","iopub.execute_input":"2025-08-14T19:21:59.41233Z","iopub.status.idle":"2025-08-14T19:21:59.63736Z","shell.execute_reply.started":"2025-08-14T19:21:59.412308Z","shell.execute_reply":"2025-08-14T19:21:59.635794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.xlabel('Epochs')\nplt.ylabel('Loss')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T19:22:08.05826Z","iopub.execute_input":"2025-08-14T19:22:08.058612Z","iopub.status.idle":"2025-08-14T19:22:08.251266Z","shell.execute_reply.started":"2025-08-14T19:22:08.058588Z","shell.execute_reply":"2025-08-14T19:22:08.250306Z"}},"outputs":[],"execution_count":null}]}