{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30635,"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)\nimport torch\nfrom torch import nn\nfrom torchvision import transforms\nfrom torchvision.transforms import ToTensor \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","execution":{"iopub.status.busy":"2024-01-26T10:52:09.868561Z","iopub.execute_input":"2024-01-26T10:52:09.869012Z","iopub.status.idle":"2024-01-26T10:52:14.656024Z","shell.execute_reply.started":"2024-01-26T10:52:09.868976Z","shell.execute_reply":"2024-01-26T10:52:14.654820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\")\n\nlen(train_data),len(test_data)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:55:25.091373Z","iopub.execute_input":"2024-01-26T10:55:25.092071Z","iopub.status.idle":"2024-01-26T10:55:31.153383Z","shell.execute_reply.started":"2024-01-26T10:55:25.092034Z","shell.execute_reply":"2024-01-26T10:55:31.152287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:55:37.417635Z","iopub.execute_input":"2024-01-26T10:55:37.418118Z","iopub.status.idle":"2024-01-26T10:55:37.449084Z","shell.execute_reply.started":"2024-01-26T10:55:37.418046Z","shell.execute_reply":"2024-01-26T10:55:37.447853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:55:43.650592Z","iopub.execute_input":"2024-01-26T10:55:43.651089Z","iopub.status.idle":"2024-01-26T10:55:46.260348Z","shell.execute_reply.started":"2024-01-26T10:55:43.651049Z","shell.execute_reply":"2024-01-26T10:55:46.259170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train_data.drop(columns=[\"label\"])\ny=train_data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:55:51.572941Z","iopub.execute_input":"2024-01-26T10:55:51.573430Z","iopub.status.idle":"2024-01-26T10:55:51.725056Z","shell.execute_reply.started":"2024-01-26T10:55:51.573392Z","shell.execute_reply":"2024-01-26T10:55:51.723884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T09:58:27.989483Z","iopub.execute_input":"2024-01-26T09:58:27.989797Z","iopub.status.idle":"2024-01-26T09:58:28.013241Z","shell.execute_reply.started":"2024-01-26T09:58:27.989770Z","shell.execute_reply":"2024-01-26T09:58:28.011517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(x[2:3].values)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:56:12.021241Z","iopub.execute_input":"2024-01-26T10:56:12.021884Z","iopub.status.idle":"2024-01-26T10:56:12.030595Z","shell.execute_reply.started":"2024-01-26T10:56:12.021772Z","shell.execute_reply":"2024-01-26T10:56:12.029228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimg=torch.reshape(torch.tensor(x[2:3].values),(1,28,28))\nplt.imshow(img.permute(1,2,0),cmap=\"gray\")\nplt.axis(False)\nplt.title(y[2])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:56:14.520697Z","iopub.execute_input":"2024-01-26T10:56:14.521141Z","iopub.status.idle":"2024-01-26T10:56:14.772016Z","shell.execute_reply.started":"2024-01-26T10:56:14.521092Z","shell.execute_reply":"2024-01-26T10:56:14.770814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=torch.reshape(torch.tensor(x.values),(42000,1,28,28))\nx","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:56:26.995518Z","iopub.execute_input":"2024-01-26T10:56:26.995949Z","iopub.status.idle":"2024-01-26T10:56:27.288983Z","shell.execute_reply.started":"2024-01-26T10:56:26.995919Z","shell.execute_reply":"2024-01-26T10:56:27.288004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:56:31.995697Z","iopub.execute_input":"2024-01-26T10:56:31.996151Z","iopub.status.idle":"2024-01-26T10:56:32.004081Z","shell.execute_reply.started":"2024-01-26T10:56:31.996091Z","shell.execute_reply":"2024-01-26T10:56:32.002932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(9,9))\nrows,cols=4,4\nfor i in range(1,rows*cols+1):\n    idx=torch.randint(0,len(x),size=[1]).item()\n    img=x[idx]\n    fig.add_subplot(rows,cols,i)\n    plt.imshow(img.permute(1,2,0),cmap=\"gray\")\n    plt.title(y[idx])\n    plt.axis(False)    ","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:56:42.071845Z","iopub.execute_input":"2024-01-26T10:56:42.072337Z","iopub.status.idle":"2024-01-26T10:56:43.222849Z","shell.execute_reply.started":"2024-01-26T10:56:42.072301Z","shell.execute_reply":"2024-01-26T10:56:43.221709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:47:01.897749Z","iopub.execute_input":"2024-01-26T10:47:01.898496Z","iopub.status.idle":"2024-01-26T10:47:01.905817Z","shell.execute_reply.started":"2024-01-26T10:47:01.898453Z","shell.execute_reply":"2024-01-26T10:47:01.904537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape,y.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:16:14.217472Z","iopub.execute_input":"2024-01-26T10:16:14.217874Z","iopub.status.idle":"2024-01-26T10:16:14.225236Z","shell.execute_reply.started":"2024-01-26T10:16:14.217841Z","shell.execute_reply":"2024-01-26T10:16:14.224039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=[]\nfor i in range(x.shape[0]):\n    train_data.append([x[i],y[i]])\n    \ntrain_data[0]","metadata":{"execution":{"iopub.status.busy":"2024-01-26T10:59:33.540377Z","iopub.execute_input":"2024-01-26T10:59:33.540835Z","iopub.status.idle":"2024-01-26T10:59:34.113584Z","shell.execute_reply.started":"2024-01-26T10:59:33.540802Z","shell.execute_reply":"2024-01-26T10:59:34.112717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=torch.reshape(torch.Tensor(test_data.values),(test_data.shape[0],1,28,28))\ntest_data","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:00:52.666353Z","iopub.execute_input":"2024-01-26T11:00:52.666856Z","iopub.status.idle":"2024-01-26T11:00:52.763503Z","shell.execute_reply.started":"2024-01-26T11:00:52.666819Z","shell.execute_reply":"2024-01-26T11:00:52.762159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(9,9))\nrows,cols=4,4\nfor i in range(1,rows*cols+1):\n    idx=torch.randint(0,len(test_data),size=[1]).item()\n    img,label=train_data[idx]\n    fig.add_subplot(rows,cols,i)\n    plt.imshow(img.permute(1,2,0),cmap=\"gray\")\n    plt.axis(False) \n    plt.title(label)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:01:34.705353Z","iopub.execute_input":"2024-01-26T11:01:34.705771Z","iopub.status.idle":"2024-01-26T11:01:36.033158Z","shell.execute_reply.started":"2024-01-26T11:01:34.705739Z","shell.execute_reply":"2024-01-26T11:01:36.032249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test=train_data[:int(0.8*len(train_data))],train_data[int(0.8*len(train_data)):]\nlen(x_train),len(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:03:30.809178Z","iopub.execute_input":"2024-01-26T11:03:30.809665Z","iopub.status.idle":"2024-01-26T11:03:30.820534Z","shell.execute_reply.started":"2024-01-26T11:03:30.809628Z","shell.execute_reply":"2024-01-26T11:03:30.818665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\nbatch_size=32\n\ntrain_dataloader=DataLoader(dataset=x_train,batch_size=32,shuffle=True)\n\ntest_dataloader=DataLoader(dataset=x_test,batch_size=32,shuffle=False)\n\ntrain_dataloader,test_dataloader","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:03:44.276607Z","iopub.execute_input":"2024-01-26T11:03:44.277076Z","iopub.status.idle":"2024-01-26T11:03:44.287936Z","shell.execute_reply.started":"2024-01-26T11:03:44.277041Z","shell.execute_reply":"2024-01-26T11:03:44.286083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dataloader),len(test_dataloader)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:03:47.048883Z","iopub.execute_input":"2024-01-26T11:03:47.049379Z","iopub.status.idle":"2024-01-26T11:03:47.056992Z","shell.execute_reply.started":"2024-01-26T11:03:47.049341Z","shell.execute_reply":"2024-01-26T11:03:47.055612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features_batch, train_labels_batch = next(iter(train_dataloader))\ntrain_features_batch.shape, train_labels_batch.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:04:17.318847Z","iopub.execute_input":"2024-01-26T11:04:17.319342Z","iopub.status.idle":"2024-01-26T11:04:17.346032Z","shell.execute_reply.started":"2024-01-26T11:04:17.319301Z","shell.execute_reply":"2024-01-26T11:04:17.344895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_idx = torch.randint(0, len(train_features_batch), size=[1]).item()\nimg, label = train_features_batch[random_idx], train_labels_batch[random_idx]\nplt.imshow(img.permute(1,2,0), cmap=\"gray\")\nplt.title(label.item())\nplt.axis(False);\nprint(f\"Image size: {img.shape}\")\nprint(f\"Label: {label}, label size: {label.shape}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:06:01.524455Z","iopub.execute_input":"2024-01-26T11:06:01.524955Z","iopub.status.idle":"2024-01-26T11:06:01.708163Z","shell.execute_reply.started":"2024-01-26T11:06:01.524921Z","shell.execute_reply":"2024-01-26T11:06:01.706941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TinyVGG(nn.Module):\n    def __init__(self,input_features,hidden_features,output_features):\n        super().__init__()\n        \n        self.conv1=nn.Sequential(\n            nn.Conv2d(in_channels=input_features,\n                      out_channels=hidden_features,\n                      kernel_size=(3,3),\n                      padding=1,\n                      stride=1\n                     ),\n            nn.ReLU(),\n            nn.Conv2d(in_channels=hidden_features,\n                      out_channels=hidden_features,\n                      kernel_size=(3,3),\n                      padding=1,\n                      stride=1\n                     ),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=(2,2))\n        )\n        \n        self.conv2=nn.Sequential(\n            nn.Conv2d(in_channels=hidden_features,\n                      out_channels=hidden_features,\n                      kernel_size=(3,3),\n                      padding=1,\n                      stride=1\n                     ),\n            nn.ReLU(),\n            nn.Conv2d(in_channels=hidden_features,\n                      out_channels=hidden_features,\n                      kernel_size=(3,3),\n                      padding=1,\n                      stride=1,\n                     ),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=(2,2))\n        )\n        \n        self.classifier=nn.Sequential(\n            nn.Flatten(),\n            nn.Linear(in_features=hidden_features*7*7,\n                      out_features=output_features\n                     )\n        )\n        \n        \n    def forward(self,x):\n        return self.classifier(self.conv2(self.conv1(x)))","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:23:51.424975Z","iopub.execute_input":"2024-01-26T11:23:51.425773Z","iopub.status.idle":"2024-01-26T11:23:51.437638Z","shell.execute_reply.started":"2024-01-26T11:23:51.425731Z","shell.execute_reply":"2024-01-26T11:23:51.436193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1=TinyVGG(1,16,10)\nmodel1","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:23:54.799780Z","iopub.execute_input":"2024-01-26T11:23:54.800246Z","iopub.status.idle":"2024-01-26T11:23:54.811002Z","shell.execute_reply.started":"2024-01-26T11:23:54.800214Z","shell.execute_reply":"2024-01-26T11:23:54.809609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"","metadata":{}},{"cell_type":"code","source":"from torchinfo import summary\nsummary(model1,input_size=[32,1,28,28])","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:23:59.689077Z","iopub.execute_input":"2024-01-26T11:23:59.689557Z","iopub.status.idle":"2024-01-26T11:23:59.787945Z","shell.execute_reply.started":"2024-01-26T11:23:59.689522Z","shell.execute_reply":"2024-01-26T11:23:59.786526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_step(model,dataloader,loss_fn,optimizer):\n    model.train()\n    \n    train_loss,train_acc=0,0\n    for batch,(x,y) in enumerate(dataloader):\n        y_pred=model(x.float())\n        loss=loss_fn(y_pred,y)\n        optimizer.zero_grad()\n        \n        train_loss+=loss.item()\n        \n        loss.backward()\n        optimizer.step()\n        \n        y_labels=torch.argmax(torch.softmax(y_pred,dim=1),dim=1)\n        train_acc+=((y_labels==y).sum().item())/len(y)\n        \n    train_loss=train_loss/len(dataloader)\n    train_acc=train_acc/len(dataloader)\n    \n    return train_loss,train_acc    ","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:38:49.287377Z","iopub.execute_input":"2024-01-26T12:38:49.287821Z","iopub.status.idle":"2024-01-26T12:38:49.296308Z","shell.execute_reply.started":"2024-01-26T12:38:49.287790Z","shell.execute_reply":"2024-01-26T12:38:49.294938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_step(model,dataloader,loss_fn):\n    model.eval()\n    test_loss,test_acc=0,0\n    with torch.inference_mode():\n        for batch,(x,y) in enumerate(dataloader):\n            y_pred=model(x.float())\n            loss=loss_fn(y_pred,y)\n            \n            test_loss+=loss.item()\n            \n            y_labels=torch.argmax(torch.softmax(y_pred,dim=1),dim=1)\n            test_acc+=((y_labels==y).sum().item())/len(y)\n            \n    test_loss=test_loss/len(dataloader)\n    test_acc=test_acc/len(dataloader)\n    \n    return test_loss,test_acc           ","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:44:00.846842Z","iopub.execute_input":"2024-01-26T12:44:00.847636Z","iopub.status.idle":"2024-01-26T12:44:00.854550Z","shell.execute_reply.started":"2024-01-26T12:44:00.847600Z","shell.execute_reply":"2024-01-26T12:44:00.853607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(model,train_dataloader,test_dataloader,loss_fn,optimizer,epochs):\n    from tqdm.auto import tqdm\n    \n    results={\"train_loss\":[],\"train_acc\":[],\"test_loss\":[],\"test_acc\":[]}\n    \n    for i in tqdm(range(epochs)):\n        train_loss,train_acc=train_step(model1,train_dataloader,loss_fn,optimizer)\n        test_loss,test_acc=test_step(model1,test_dataloader,loss_fn)\n        \n        print(\n            f\"Epoch: {i+1} | \"\n            f\"Train_loss: {train_loss:.4f} | \"\n            f\"Train_acc: {train_acc:.4f} | \"\n            f\"Test_loss: {test_loss:.4f} | \"\n            f\"Test_acc: {test_acc:.4f}\"\n        )\n        \n        results[\"train_loss\"].append(train_loss)\n        results[\"train_acc\"].append(train_acc)\n        results[\"test_loss\"].append(test_loss)\n        results[\"test_acc\"].append(test_acc)\n        \n    return results      ","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:44:04.954606Z","iopub.execute_input":"2024-01-26T12:44:04.955431Z","iopub.status.idle":"2024-01-26T12:44:04.962843Z","shell.execute_reply.started":"2024-01-26T12:44:04.955391Z","shell.execute_reply":"2024-01-26T12:44:04.961784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_batch,label_batch=next(iter(train_dataloader))\nimg_batch.shape,label_batch.shape\n\nmodel1(img_batch.float())","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:37:13.296507Z","iopub.execute_input":"2024-01-26T12:37:13.297162Z","iopub.status.idle":"2024-01-26T12:37:13.378418Z","shell.execute_reply.started":"2024-01-26T12:37:13.297114Z","shell.execute_reply":"2024-01-26T12:37:13.377180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_fn=nn.CrossEntropyLoss()\noptimizer=torch.optim.Adam(params=model1.parameters(),lr=0.01)\n\nfrom timeit import default_timer as timer \nstart_time = timer()\n\n\nresults=train(model=model1,\n              train_dataloader=train_dataloader,\n              test_dataloader=test_dataloader,\n              loss_fn=loss_fn,\n              optimizer=optimizer,\n              epochs=20\n             )\n\nend_time = timer()\nprint(f\"Total training time: {end_time-start_time:.3f} seconds\")","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:44:26.831854Z","iopub.execute_input":"2024-01-26T12:44:26.833015Z","iopub.status.idle":"2024-01-26T12:49:55.345226Z","shell.execute_reply.started":"2024-01-26T12:44:26.832974Z","shell.execute_reply":"2024-01-26T12:49:55.343178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_loss_curves(results):\n    train_loss=results[\"train_loss\"]\n    train_acc=results[\"train_acc\"]\n    \n    test_loss=results[\"test_loss\"]\n    test_acc=results[\"test_acc\"]\n    \n    epochs=range(len(results[\"test_loss\"]))\n    \n    plt.figure(figsize=(15,7))\n    \n    plt.subplot(1,2,1)\n    plt.plot(epochs,train_loss,label=\"Train Loss\")\n    plt.plot(epochs,test_loss,label=\"Test Loss\")\n    plt.legend()\n    plt.xlabel(\"Epochs\")\n    plt.title(\"Loss\")\n    \n    \n    plt.subplot(1,2,2)\n    plt.plot(epochs,train_acc,label=\"Train Accuracy\")\n    plt.plot(epochs,test_acc,label=\"Test Accuracy\")\n    plt.legend()\n    plt.xlabel(\"Epochs\")\n    plt.title(\"Accuracy\")","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:50:24.885987Z","iopub.execute_input":"2024-01-26T12:50:24.887169Z","iopub.status.idle":"2024-01-26T12:50:24.896041Z","shell.execute_reply.started":"2024-01-26T12:50:24.887089Z","shell.execute_reply":"2024-01-26T12:50:24.894634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_loss_curves(results)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:50:37.812573Z","iopub.execute_input":"2024-01-26T12:50:37.813073Z","iopub.status.idle":"2024-01-26T12:50:38.485655Z","shell.execute_reply.started":"2024-01-26T12:50:37.813036Z","shell.execute_reply":"2024-01-26T12:50:38.484364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:51:32.302541Z","iopub.execute_input":"2024-01-26T12:51:32.303358Z","iopub.status.idle":"2024-01-26T12:51:32.311352Z","shell.execute_reply.started":"2024-01-26T12:51:32.303281Z","shell.execute_reply":"2024-01-26T12:51:32.310353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_dataloader=DataLoader(dataset=test_data,batch_size=batch_size,shuffle=False)\ntest_data_dataloader","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:52:23.877331Z","iopub.execute_input":"2024-01-26T12:52:23.877802Z","iopub.status.idle":"2024-01-26T12:52:23.886322Z","shell.execute_reply.started":"2024-01-26T12:52:23.877770Z","shell.execute_reply":"2024-01-26T12:52:23.885296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_predictions(model,dataloader):\n    final=[]\n    model.eval()\n    with torch.inference_mode():\n        for batch,x in enumerate(dataloader):\n            y_pred=model(x)\n            \n            y_labels=torch.argmax(torch.softmax(y_pred,dim=1),dim=1)\n            final+=y_labels\n            \n    return final            ","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:59:37.917314Z","iopub.execute_input":"2024-01-26T12:59:37.917779Z","iopub.status.idle":"2024-01-26T12:59:37.924549Z","shell.execute_reply.started":"2024-01-26T12:59:37.917747Z","shell.execute_reply":"2024-01-26T12:59:37.923157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=make_predictions(model1,test_data_dataloader)\nlen(predictions)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T12:59:40.470900Z","iopub.execute_input":"2024-01-26T12:59:40.471365Z","iopub.status.idle":"2024-01-26T12:59:44.917961Z","shell.execute_reply.started":"2024-01-26T12:59:40.471330Z","shell.execute_reply":"2024-01-26T12:59:44.916815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsample.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T13:00:19.497476Z","iopub.execute_input":"2024-01-26T13:00:19.497976Z","iopub.status.idle":"2024-01-26T13:00:19.589816Z","shell.execute_reply.started":"2024-01-26T13:00:19.497942Z","shell.execute_reply":"2024-01-26T13:00:19.588535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list=sample[\"ImageId\"].tolist()\nlen(id_list)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T13:01:07.256703Z","iopub.execute_input":"2024-01-26T13:01:07.257238Z","iopub.status.idle":"2024-01-26T13:01:07.266480Z","shell.execute_reply.started":"2024-01-26T13:01:07.257184Z","shell.execute_reply":"2024-01-26T13:01:07.265237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out={\"ImageId\":i,\"Label\":predictions}\nd_out=pd.DataFrame(out)\nd_out.to_csv(\"Output1.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T13:01:52.852269Z","iopub.execute_input":"2024-01-26T13:01:52.853043Z","iopub.status.idle":"2024-01-26T13:01:54.562340Z","shell.execute_reply.started":"2024-01-26T13:01:52.853002Z","shell.execute_reply":"2024-01-26T13:01:54.561306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}