{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":30732,"isInternetEnabled":false,"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","execution":{"iopub.status.busy":"2024-06-05T11:36:07.019460Z","iopub.execute_input":"2024-06-05T11:36:07.020449Z","iopub.status.idle":"2024-06-05T11:36:07.030199Z","shell.execute_reply.started":"2024-06-05T11:36:07.020410Z","shell.execute_reply":"2024-06-05T11:36:07.029067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision.transforms as transforms\nimport tqdm\nfrom torch.utils.data import DataLoader,Dataset","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:07.032415Z","iopub.execute_input":"2024-06-05T11:36:07.033078Z","iopub.status.idle":"2024-06-05T11:36:07.044011Z","shell.execute_reply.started":"2024-06-05T11:36:07.033050Z","shell.execute_reply":"2024-06-05T11:36:07.042989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:07.045328Z","iopub.execute_input":"2024-06-05T11:36:07.045629Z","iopub.status.idle":"2024-06-05T11:36:09.605615Z","shell.execute_reply.started":"2024-06-05T11:36:07.045604Z","shell.execute_reply":"2024-06-05T11:36:09.604581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:09.608030Z","iopub.execute_input":"2024-06-05T11:36:09.608337Z","iopub.status.idle":"2024-06-05T11:36:09.625249Z","shell.execute_reply.started":"2024-06-05T11:36:09.608312Z","shell.execute_reply":"2024-06-05T11:36:09.624032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=df.drop(columns=['label'])\ny=df['label']","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:09.626377Z","iopub.execute_input":"2024-06-05T11:36:09.626704Z","iopub.status.idle":"2024-06-05T11:36:09.737109Z","shell.execute_reply.started":"2024-06-05T11:36:09.626671Z","shell.execute_reply":"2024-06-05T11:36:09.736050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:09.738363Z","iopub.execute_input":"2024-06-05T11:36:09.738666Z","iopub.status.idle":"2024-06-05T11:36:09.951308Z","shell.execute_reply.started":"2024-06-05T11:36:09.738641Z","shell.execute_reply":"2024-06-05T11:36:09.950180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MND(Dataset):\n    def __init__(self,x,y):\n        self.x=torch.tensor(x.values,dtype=torch.float32)\n        self.y=torch.tensor(y.values,dtype=torch.long)\n    def __len__(self):\n        return len(self.x)\n    def __getitem__(self,idx):\n        return self.x[idx],self.y[idx]","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:09.952820Z","iopub.execute_input":"2024-06-05T11:36:09.953262Z","iopub.status.idle":"2024-06-05T11:36:09.959732Z","shell.execute_reply.started":"2024-06-05T11:36:09.953223Z","shell.execute_reply":"2024-06-05T11:36:09.958600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d=MND(X_train,y_train)\ntest_d=MND(X_test,y_test)\ntrain_l=DataLoader(train_d,batch_size=64,shuffle=True)\ntest_l=DataLoader(test_d,batch_size=64,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:09.961412Z","iopub.execute_input":"2024-06-05T11:36:09.961897Z","iopub.status.idle":"2024-06-05T11:36:10.024587Z","shell.execute_reply.started":"2024-06-05T11:36:09.961862Z","shell.execute_reply":"2024-06-05T11:36:10.023708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__(self):\n        super(Net,self).__init__()\n        self.fc1=nn.Linear(784,512)\n        self.dropout1 = nn.Dropout(0.5)\n        self.fc2=nn.Linear(512,256)\n        self.fc3=nn.Linear(256,128)\n        self.fc4=nn.Linear(128,128)\n        self.dropout2 =nn.Dropout(0.5)\n        self.fc5=nn.Linear(128,64)\n        self.fc6=nn.Linear(64,10)\n\n\n    \n    def forward(self,x):\n        x=x.view(-1,784)\n        leaky_relu = nn.LeakyReLU(negative_slope=0.1)\n        x=leaky_relu(self.fc1(x))\n        x=self.dropout1(x)\n        x=leaky_relu(self.fc2(x))\n        x=leaky_relu(self.fc3(x))\n        x=leaky_relu(self.fc4(x))\n        x=self.dropout2(x)\n        x=leaky_relu(self.fc5(x))\n        x=self.fc6(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:10.025899Z","iopub.execute_input":"2024-06-05T11:36:10.026300Z","iopub.status.idle":"2024-06-05T11:36:10.035903Z","shell.execute_reply.started":"2024-06-05T11:36:10.026268Z","shell.execute_reply":"2024-06-05T11:36:10.034982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net=Net()\ncrit=nn.CrossEntropyLoss()\noptimizer=optim.Adam(params=net.parameters(),lr=0.0001)\nfor epoch in range(40):\n    running_loss=0\n    Progress=tqdm.tqdm(enumerate(train_l),total=len(train_l),desc=f'Epoch : {epoch+1}')\n    for i,data in Progress:\n        inputs,labels=data\n        optimizer.zero_grad()\n        outputs=net(inputs)\n        loss=crit(outputs,labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n        if(i%100==99):\n            Progress.set_postfix(loss=running_loss/100)\n            running_loss=0.0","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:36:10.038390Z","iopub.execute_input":"2024-06-05T11:36:10.038695Z","iopub.status.idle":"2024-06-05T11:38:37.091969Z","shell.execute_reply.started":"2024-06-05T11:36:10.038671Z","shell.execute_reply":"2024-06-05T11:38:37.090967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correct = 0\ntotal = 0\nwith torch.no_grad():\n    for data in test_l:\n        images,labels=data\n        output=net(images)\n        _,predicted=torch.max(output.data,1)\n        total += labels.size(0)\n        correct+=(predicted==labels).sum().item()\nprint(correct/total)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:38:37.093273Z","iopub.execute_input":"2024-06-05T11:38:37.093590Z","iopub.status.idle":"2024-06-05T11:38:37.341110Z","shell.execute_reply.started":"2024-06-05T11:38:37.093563Z","shell.execute_reply":"2024-06-05T11:38:37.339983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ndf_test=torch.tensor(df_test.values,dtype=torch.float32)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:39:07.820591Z","iopub.execute_input":"2024-06-05T11:39:07.821264Z","iopub.status.idle":"2024-06-05T11:39:09.697307Z","shell.execute_reply.started":"2024-06-05T11:39:07.821231Z","shell.execute_reply":"2024-06-05T11:39:09.696407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    for data in test_l:\n        output=net(df_test)\nxyz,predicted=torch.max(output.data,1)\nans = pd.DataFrame(predicted.numpy())","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:41:08.239900Z","iopub.execute_input":"2024-06-05T11:41:08.240704Z","iopub.status.idle":"2024-06-05T11:41:58.631916Z","shell.execute_reply.started":"2024-06-05T11:41:08.240672Z","shell.execute_reply":"2024-06-05T11:41:58.631047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans['ImageId'] = range(1, len(ans) + 1)\nans.rename(columns={0: 'Label'}, inplace=True)\nans=ans[['ImageId','Label']]\nans.to_csv('ans.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T11:47:40.363257Z","iopub.execute_input":"2024-06-05T11:47:40.364102Z","iopub.status.idle":"2024-06-05T11:47:40.398832Z","shell.execute_reply.started":"2024-06-05T11:47:40.364069Z","shell.execute_reply":"2024-06-05T11:47:40.397724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"jupyter":{"source_hidden":true}},"execution_count":null,"outputs":[]}]}