{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":14846303,"sourceType":"datasetVersion","datasetId":9495602}],"dockerImageVersionId":30407,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport sklearn\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC\nfrom sklearn.neighbors import KNeighborsClassifier\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout, Input\nfrom tensorflow import keras\nfrom keras import Model\nfrom keras.layers import Input, Conv2D, MaxPool2D, Dense, Flatten, AveragePooling2D","metadata":{"id":"yP36VocnbfQ0","execution":{"iopub.status.busy":"2026-02-15T08:54:38.505820Z","iopub.execute_input":"2026-02-15T08:54:38.506252Z","iopub.status.idle":"2026-02-15T08:54:38.515161Z","shell.execute_reply.started":"2026-02-15T08:54:38.506217Z","shell.execute_reply":"2026-02-15T08:54:38.513722Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mnist=tf.keras.datasets.mnist","metadata":{"id":"gBapAD3GcsZ_","execution":{"iopub.status.busy":"2026-02-15T08:54:38.517479Z","iopub.execute_input":"2026-02-15T08:54:38.518096Z","iopub.status.idle":"2026-02-15T08:54:38.530550Z","shell.execute_reply.started":"2026-02-15T08:54:38.518026Z","shell.execute_reply":"2026-02-15T08:54:38.528942Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nmnist_path = '/kaggle/input/datasets/abdullah3243/mnist-npz/mnist.npz'  \nwith np.load(mnist_path) as data:\n    X_train = data['x_train']\n    y_train = data['y_train']\n    X_test = data['x_test']\n    y_test = data['y_test']\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:54:38.535203Z","iopub.execute_input":"2026-02-15T08:54:38.536530Z","iopub.status.idle":"2026-02-15T08:54:38.825566Z","shell.execute_reply.started":"2026-02-15T08:54:38.536470Z","shell.execute_reply":"2026-02-15T08:54:38.824159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test = X_train / 255.0, X_test / 255.0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:54:38.827171Z","iopub.execute_input":"2026-02-15T08:54:38.827725Z","iopub.status.idle":"2026-02-15T08:54:38.982384Z","shell.execute_reply.started":"2026-02-15T08:54:38.827648Z","shell.execute_reply":"2026-02-15T08:54:38.981175Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Input(shape=(28,28)))\nmodel.add(Flatten())\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:54:38.985492Z","iopub.execute_input":"2026-02-15T08:54:38.986334Z","iopub.status.idle":"2026-02-15T08:54:39.036435Z","shell.execute_reply.started":"2026-02-15T08:54:38.986291Z","shell.execute_reply":"2026-02-15T08:54:39.035149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"nonsense_prediction=model(X_train[:1]).numpy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:54:39.038186Z","iopub.execute_input":"2026-02-15T08:54:39.039444Z","iopub.status.idle":"2026-02-15T08:54:39.084388Z","shell.execute_reply.started":"2026-02-15T08:54:39.039396Z","shell.execute_reply":"2026-02-15T08:54:39.082849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.nn.softmax(nonsense_prediction).numpy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:54:39.085929Z","iopub.execute_input":"2026-02-15T08:54:39.086411Z","iopub.status.idle":"2026-02-15T08:54:39.108305Z","shell.execute_reply.started":"2026-02-15T08:54:39.086376Z","shell.execute_reply":"2026-02-15T08:54:39.106937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss_fn = SparseCategoricalCrossentropy(from_logits=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:54:39.110787Z","iopub.execute_input":"2026-02-15T08:54:39.111301Z","iopub.status.idle":"2026-02-15T08:54:39.117385Z","shell.execute_reply.started":"2026-02-15T08:54:39.111264Z","shell.execute_reply":"2026-02-15T08:54:39.116025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"expected_loss = -tf.math.log(1/10) \nprint('Expected loss on random guess', expected_loss)\nprint('Actual loss:', loss_fn(y_train[:1], nonsense_prediction))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:55:18.219875Z","iopub.execute_input":"2026-02-15T08:55:18.221206Z","iopub.status.idle":"2026-02-15T08:55:18.231730Z","shell.execute_reply.started":"2026-02-15T08:55:18.221162Z","shell.execute_reply":"2026-02-15T08:55:18.230388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = Adam(learning_rate=0.01)\n\nmodel.compile(optimizer=optimizer, loss=loss_fn, metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:55:23.905510Z","iopub.execute_input":"2026-02-15T08:55:23.905921Z","iopub.status.idle":"2026-02-15T08:55:23.926610Z","shell.execute_reply.started":"2026-02-15T08:55:23.905888Z","shell.execute_reply":"2026-02-15T08:55:23.924171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_train, y_train, epochs=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-15T08:55:26.272524Z","iopub.execute_input":"2026-02-15T08:55:26.273600Z","iopub.status.idle":"2026-02-15T08:56:33.871064Z","shell.execute_reply.started":"2026-02-15T08:55:26.273547Z","shell.execute_reply":"2026-02-15T08:56:33.869764Z"}},"outputs":[],"execution_count":null}]}