{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":262368611,"sourceType":"kernelVersion"},{"sourceId":264026204,"sourceType":"kernelVersion"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-09-18T09:38:51.686638Z","iopub.execute_input":"2025-09-18T09:38:51.687032Z","iopub.status.idle":"2025-09-18T09:38:52.75875Z","shell.execute_reply.started":"2025-09-18T09:38:51.686999Z","shell.execute_reply":"2025-09-18T09:38:52.757665Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"This is the inference notebook to this [training notebook](https://www.kaggle.com/code/bridgeoverwater/beginner-digit-recognizer-baseline-gpu)","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import DataLoader, TensorDataset\nfrom sklearn.linear_model import SGDClassifier\nfrom torch.optim import SGD\nfrom sklearn.model_selection import StratifiedGroupKFold, GroupKFold, KFold, StratifiedKFold\nfrom tqdm import tqdm ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T09:38:52.75972Z","iopub.execute_input":"2025-09-18T09:38:52.760184Z","iopub.status.idle":"2025-09-18T09:38:58.950096Z","shell.execute_reply.started":"2025-09-18T09:38:52.760157Z","shell.execute_reply":"2025-09-18T09:38:58.949105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_data.head()\nlen(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T09:38:58.952381Z","iopub.execute_input":"2025-09-18T09:38:58.952816Z","iopub.status.idle":"2025-09-18T09:39:04.450478Z","shell.execute_reply.started":"2025-09-18T09:38:58.95279Z","shell.execute_reply":"2025-09-18T09:39:04.449669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T09:39:12.378023Z","iopub.execute_input":"2025-09-18T09:39:12.378889Z","iopub.status.idle":"2025-09-18T09:39:12.418471Z","shell.execute_reply.started":"2025-09-18T09:39:12.378852Z","shell.execute_reply":"2025-09-18T09:39:12.417583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CFG:\n    batch_size = 32\n    lr = 1e-3\n    num_epochs = 50\n    n_folds = 5\n    ckpt_path = \"/kaggle/input/gpu-digit-recognizer-baseline/last_epoch-04.bin\" #file path","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T07:55:51.723216Z","iopub.execute_input":"2025-09-18T07:55:51.723559Z","iopub.status.idle":"2025-09-18T07:55:51.729914Z","shell.execute_reply.started":"2025-09-18T07:55:51.723534Z","shell.execute_reply":"2025-09-18T07:55:51.728047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"state_dict = torch.load(CFG.ckpt_path, weights_only=False) # Load the weights\nmodel = nn.Sequential(nn.Linear(784, 1024),\n                      nn.LeakyReLU(negative_slope=0.05),\n                      nn.Linear(1024, 512),\n                      nn.LeakyReLU(negative_slope=0.05),\n                      nn.Linear(512, 256),\n                      nn.LeakyReLU(negative_slope=0.05),\n                      nn.Linear(256, 10))\nmodel.load_state_dict(state_dict) #u\nmodel.cuda()\noptimizer = SGD(model.parameters(), lr=CFG.lr)\ncriterion = nn.CrossEntropyLoss()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T07:58:59.294037Z","iopub.execute_input":"2025-09-18T07:58:59.294319Z","iopub.status.idle":"2025-09-18T07:58:59.305885Z","shell.execute_reply.started":"2025-09-18T07:58:59.294297Z","shell.execute_reply":"2025-09-18T07:58:59.305199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def prepare_loaders(fold, batch_size):\n    X = test_data.iloc[:,:].values / 255\n    y = np.zeros(len(test_data), dtype=int)\n    test_dataset = TensorDataset(torch.tensor(X), torch.tensor(y))\n    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, pin_memory=True, drop_last=False)\n    return test_loader","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T08:28:07.700449Z","iopub.execute_input":"2025-09-18T08:28:07.700741Z","iopub.status.idle":"2025-09-18T08:28:07.70589Z","shell.execute_reply.started":"2025-09-18T08:28:07.70072Z","shell.execute_reply":"2025-09-18T08:28:07.705034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_loader = prepare_loaders(fold=0, batch_size=CFG.batch_size)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T08:28:09.849736Z","iopub.execute_input":"2025-09-18T08:28:09.850058Z","iopub.status.idle":"2025-09-18T08:28:10.052132Z","shell.execute_reply.started":"2025-09-18T08:28:09.850034Z","shell.execute_reply":"2025-09-18T08:28:10.051336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = []\nmodel.eval()\nwith torch.no_grad():\n    for data in tqdm(test_loader):\n        feature, target = data\n        prediction = model(feature.float().cuda())\n        pred += [prediction.cpu()]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T08:31:28.080856Z","iopub.execute_input":"2025-09-18T08:31:28.081181Z","iopub.status.idle":"2025-09-18T08:31:29.442062Z","shell.execute_reply.started":"2025-09-18T08:31:28.081158Z","shell.execute_reply":"2025-09-18T08:31:29.441257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_pred = torch.cat(pred)\ntest_pred.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T08:31:31.319297Z","iopub.execute_input":"2025-09-18T08:31:31.319576Z","iopub.status.idle":"2025-09-18T08:31:31.326048Z","shell.execute_reply.started":"2025-09-18T08:31:31.319557Z","shell.execute_reply":"2025-09-18T08:31:31.324704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv') \nsub['Label'] = np.argmax(test_pred, 1)\n\nprint(sub.shape)\nsub.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T08:32:37.164268Z","iopub.execute_input":"2025-09-18T08:32:37.164572Z","iopub.status.idle":"2025-09-18T08:32:37.183053Z","shell.execute_reply.started":"2025-09-18T08:32:37.164551Z","shell.execute_reply":"2025-09-18T08:32:37.182348Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_df = pd.DataFrame(sub)\npred_df.to_csv('submission.csv',index=False)\ndisplay(pred_df.head(5))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-18T08:33:08.559345Z","iopub.execute_input":"2025-09-18T08:33:08.559661Z","iopub.status.idle":"2025-09-18T08:33:08.593388Z","shell.execute_reply.started":"2025-09-18T08:33:08.559637Z","shell.execute_reply":"2025-09-18T08:33:08.592494Z"}},"outputs":[],"execution_count":null}]}