{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Import Item","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport torch\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-06T17:42:56.386009Z","iopub.execute_input":"2023-12-06T17:42:56.386274Z","iopub.status.idle":"2023-12-06T17:43:00.828595Z","shell.execute_reply.started":"2023-12-06T17:42:56.386248Z","shell.execute_reply":"2023-12-06T17:43:00.827728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reprocibility","metadata":{}},{"cell_type":"code","source":"import os\nimport random\n\nimport numpy as np\nimport torch\n\nSEED = 42\n\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed_everything(SEED)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:00.830252Z","iopub.execute_input":"2023-12-06T17:43:00.830686Z","iopub.status.idle":"2023-12-06T17:43:00.842527Z","shell.execute_reply.started":"2023-12-06T17:43:00.830658Z","shell.execute_reply":"2023-12-06T17:43:00.841784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data preprocessing","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv') \ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:00.843692Z","iopub.execute_input":"2023-12-06T17:43:00.844047Z","iopub.status.idle":"2023-12-06T17:43:06.457164Z","shell.execute_reply.started":"2023-12-06T17:43:00.844020Z","shell.execute_reply":"2023-12-06T17:43:06.456383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:06.459196Z","iopub.execute_input":"2023-12-06T17:43:06.459495Z","iopub.status.idle":"2023-12-06T17:43:06.484836Z","shell.execute_reply.started":"2023-12-06T17:43:06.459470Z","shell.execute_reply":"2023-12-06T17:43:06.484120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_df.values","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:06.485657Z","iopub.execute_input":"2023-12-06T17:43:06.485892Z","iopub.status.idle":"2023-12-06T17:43:06.489937Z","shell.execute_reply.started":"2023-12-06T17:43:06.485872Z","shell.execute_reply":"2023-12-06T17:43:06.488974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data[:, 1:]\ny = train_data[:, 0]","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:06.490985Z","iopub.execute_input":"2023-12-06T17:43:06.491276Z","iopub.status.idle":"2023-12-06T17:43:06.499680Z","shell.execute_reply.started":"2023-12-06T17:43:06.491253Z","shell.execute_reply":"2023-12-06T17:43:06.498805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:06.500673Z","iopub.execute_input":"2023-12-06T17:43:06.500986Z","iopub.status.idle":"2023-12-06T17:43:06.512326Z","shell.execute_reply.started":"2023-12-06T17:43:06.500939Z","shell.execute_reply":"2023-12-06T17:43:06.511465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:06.513399Z","iopub.execute_input":"2023-12-06T17:43:06.513651Z","iopub.status.idle":"2023-12-06T17:43:06.888154Z","shell.execute_reply.started":"2023-12-06T17:43:06.513630Z","shell.execute_reply":"2023-12-06T17:43:06.887371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:06.889248Z","iopub.execute_input":"2023-12-06T17:43:06.889647Z","iopub.status.idle":"2023-12-06T17:43:06.895661Z","shell.execute_reply.started":"2023-12-06T17:43:06.889617Z","shell.execute_reply":"2023-12-06T17:43:06.894668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_tr = torch.tensor(X_train / 255, dtype=torch.float32)\ny_tr = torch.tensor(y_train)\nX_val = torch.tensor(X_val / 255, dtype=torch.float32)\ny_val = torch.tensor(y_val)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:06.898905Z","iopub.execute_input":"2023-12-06T17:43:06.899194Z","iopub.status.idle":"2023-12-06T17:43:07.074163Z","shell.execute_reply.started":"2023-12-06T17:43:06.899165Z","shell.execute_reply":"2023-12-06T17:43:07.073368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:07.075369Z","iopub.execute_input":"2023-12-06T17:43:07.075737Z","iopub.status.idle":"2023-12-06T17:43:07.080431Z","shell.execute_reply.started":"2023-12-06T17:43:07.075704Z","shell.execute_reply":"2023-12-06T17:43:07.079767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Model","metadata":{}},{"cell_type":"code","source":"model = nn.Sequential(\n    nn.Linear(784, 392),\n    nn.ReLU(),\n    nn.Linear(392,196),\n    nn.ReLU(),\n    nn.Linear(196, 10)  \n)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:07.081268Z","iopub.execute_input":"2023-12-06T17:43:07.081857Z","iopub.status.idle":"2023-12-06T17:43:07.103840Z","shell.execute_reply.started":"2023-12-06T17:43:07.081831Z","shell.execute_reply":"2023-12-06T17:43:07.103178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 200\nLR = 0.5","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:07.104790Z","iopub.execute_input":"2023-12-06T17:43:07.105066Z","iopub.status.idle":"2023-12-06T17:43:07.108881Z","shell.execute_reply.started":"2023-12-06T17:43:07.105043Z","shell.execute_reply":"2023-12-06T17:43:07.108068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(model.parameters(), lr=LR)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:07.109922Z","iopub.execute_input":"2023-12-06T17:43:07.110224Z","iopub.status.idle":"2023-12-06T17:43:07.119670Z","shell.execute_reply.started":"2023-12-06T17:43:07.110200Z","shell.execute_reply":"2023-12-06T17:43:07.118818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loss = []\nval_loss = []\nfor epoch in range(EPOCHS):\n    # Trainig\n    model.train()\n    #Forward\n    logits = model(X_tr)\n    # Loss\n    loss = criterion(logits, y_tr)\n    # Zero grad\n    optimizer.zero_grad()\n    # Backward\n    loss.backward()\n    # Update\n    optimizer.step()\n    \n    print(f\"{epoch=} Training_loss={loss.item():.4f}\")\n    \n    train_loss.append(loss.item())\n    \n    # Validation\n    \n    model.eval()\n    with torch.no_grad():\n        val_logits = model(X_val)\n        \n        # Loss\n    vloss = criterion(val_logits, y_val)\n    \n    print(f\"Validation_loss={vloss.item():.4f}\")\n    val_loss.append(vloss.item())","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:43:07.120710Z","iopub.execute_input":"2023-12-06T17:43:07.121003Z","iopub.status.idle":"2023-12-06T17:44:17.418916Z","shell.execute_reply.started":"2023-12-06T17:43:07.120979Z","shell.execute_reply":"2023-12-06T17:44:17.417948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(train_loss)\nplt.plot(val_loss)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:44:21.834753Z","iopub.execute_input":"2023-12-06T17:44:21.835119Z","iopub.status.idle":"2023-12-06T17:44:22.061297Z","shell.execute_reply.started":"2023-12-06T17:44:21.835089Z","shell.execute_reply":"2023-12-06T17:44:22.060306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference","metadata":{}},{"cell_type":"code","source":"test_data = torch.tensor(test_df.values / 255, dtype=torch.float32)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:45:14.031267Z","iopub.execute_input":"2023-12-06T17:45:14.032363Z","iopub.status.idle":"2023-12-06T17:45:14.141566Z","shell.execute_reply.started":"2023-12-06T17:45:14.032315Z","shell.execute_reply":"2023-12-06T17:45:14.140732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    model.eval()\n    logits = model(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:45:14.879036Z","iopub.execute_input":"2023-12-06T17:45:14.879834Z","iopub.status.idle":"2023-12-06T17:45:15.021489Z","shell.execute_reply.started":"2023-12-06T17:45:14.879800Z","shell.execute_reply":"2023-12-06T17:45:15.020201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logits","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:45:23.533162Z","iopub.execute_input":"2023-12-06T17:45:23.533501Z","iopub.status.idle":"2023-12-06T17:45:23.541552Z","shell.execute_reply.started":"2023-12-06T17:45:23.533474Z","shell.execute_reply":"2023-12-06T17:45:23.540656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logits[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:45:38.910833Z","iopub.execute_input":"2023-12-06T17:45:38.911560Z","iopub.status.idle":"2023-12-06T17:45:38.918069Z","shell.execute_reply.started":"2023-12-06T17:45:38.911529Z","shell.execute_reply":"2023-12-06T17:45:38.917276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"softmax = nn.Softmax(dim=1)\npredictions = softmax(logits)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:45:54.737864Z","iopub.execute_input":"2023-12-06T17:45:54.738618Z","iopub.status.idle":"2023-12-06T17:45:54.747379Z","shell.execute_reply.started":"2023-12-06T17:45:54.738585Z","shell.execute_reply":"2023-12-06T17:45:54.746499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:45:59.341938Z","iopub.execute_input":"2023-12-06T17:45:59.342328Z","iopub.status.idle":"2023-12-06T17:45:59.349068Z","shell.execute_reply.started":"2023-12-06T17:45:59.342294Z","shell.execute_reply":"2023-12-06T17:45:59.348255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = logits.argmax(dim=1)\ny_pred[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:46:25.850825Z","iopub.execute_input":"2023-12-06T17:46:25.851536Z","iopub.status.idle":"2023-12-06T17:46:25.858209Z","shell.execute_reply.started":"2023-12-06T17:46:25.851505Z","shell.execute_reply":"2023-12-06T17:46:25.857362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsubmit.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:46:37.043290Z","iopub.execute_input":"2023-12-06T17:46:37.043908Z","iopub.status.idle":"2023-12-06T17:46:37.062477Z","shell.execute_reply.started":"2023-12-06T17:46:37.043869Z","shell.execute_reply":"2023-12-06T17:46:37.061440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit[\"Label\"] = y_pred","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:44:17.966946Z","iopub.execute_input":"2023-12-06T17:44:17.967224Z","iopub.status.idle":"2023-12-06T17:44:17.972675Z","shell.execute_reply.started":"2023-12-06T17:44:17.967201Z","shell.execute_reply":"2023-12-06T17:44:17.971853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv(\"submition.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:47:35.286764Z","iopub.execute_input":"2023-12-06T17:47:35.287122Z","iopub.status.idle":"2023-12-06T17:47:35.345936Z","shell.execute_reply.started":"2023-12-06T17:47:35.287095Z","shell.execute_reply":"2023-12-06T17:47:35.345078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit","metadata":{"execution":{"iopub.status.busy":"2023-12-06T17:47:42.497879Z","iopub.execute_input":"2023-12-06T17:47:42.498808Z","iopub.status.idle":"2023-12-06T17:47:42.508885Z","shell.execute_reply.started":"2023-12-06T17:47:42.498772Z","shell.execute_reply":"2023-12-06T17:47:42.507938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}