{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30747,"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","execution":{"iopub.status.busy":"2024-08-06T14:43:31.082919Z","iopub.execute_input":"2024-08-06T14:43:31.083744Z","iopub.status.idle":"2024-08-06T14:43:31.091648Z","shell.execute_reply.started":"2024-08-06T14:43:31.083714Z","shell.execute_reply":"2024-08-06T14:43:31.090720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchmetrics\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:43:32.686762Z","iopub.execute_input":"2024-08-06T14:43:32.687159Z","iopub.status.idle":"2024-08-06T14:43:32.691846Z","shell.execute_reply.started":"2024-08-06T14:43:32.687129Z","shell.execute_reply":"2024-08-06T14:43:32.690888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = torch.from_numpy(train.drop(['label'],axis = 1).to_numpy()).to(device = 'cuda',dtype = torch.float)\ny = torch.from_numpy(train['label'].to_numpy()).to(device = 'cuda',dtype = torch.long)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np.reshape(X.cpu().detach().numpy()[8],(28,28)))","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:20:30.625652Z","iopub.execute_input":"2024-08-06T14:20:30.626013Z","iopub.status.idle":"2024-08-06T14:20:30.998714Z","shell.execute_reply.started":"2024-08-06T14:20:30.625984Z","shell.execute_reply":"2024-08-06T14:20:30.997785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = torch.nn.Sequential(\n    torch.nn.Linear(784,512),\n    torch.nn.ReLU(),\n    torch.nn.Linear(512,512),\n    torch.nn.ReLU(),\n    torch.nn.Linear(512,512),\n    torch.nn.ReLU(),\n    torch.nn.Linear(512,10),\n).to(device='cuda')\nloss_fn = torch.nn.CrossEntropyLoss().to(device='cuda')\nacc = torchmetrics.Accuracy(task='multiclass',num_classes = 10).to(device='cuda')\noptim = torch.optim.SGD(params = model1.parameters(),lr = 0.01)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:11:11.235483Z","iopub.execute_input":"2024-08-06T14:11:11.235852Z","iopub.status.idle":"2024-08-06T14:11:11.255044Z","shell.execute_reply.started":"2024-08-06T14:11:11.235811Z","shell.execute_reply":"2024-08-06T14:11:11.254244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nfor epoch in range(epochs):\n    l, a = 0 ,0\n    for batch in range(104):\n        logits = model1(X[(batch*400):((batch*400)+400)])\n        preds = torch.softmax(logits,1).argmax(1)\n        loss = loss_fn(logits,y[(batch*400):((batch*400)+400)])\n        l+=loss.item()\n        accuracy = acc(preds,y[(batch*400):((batch*400)+400)])\n        a+=accuracy.item()\n        optim.zero_grad()\n        loss.backward()\n        optim.step()\n        if batch %20==0:\n            print(f'data {(batch+1)*400}/42000')\n    l/=105\n    a/=105\n    print(f'\\nepoch {epoch} | loss {l} accuracy {a}\\n')\n        ","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:11:11.456692Z","iopub.execute_input":"2024-08-06T14:11:11.457196Z","iopub.status.idle":"2024-08-06T14:11:13.997717Z","shell.execute_reply.started":"2024-08-06T14:11:11.457170Z","shell.execute_reply":"2024-08-06T14:11:13.996844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X = torch.from_numpy(test.to_numpy()).to(device='cuda',dtype = torch.float)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:42:15.486746Z","iopub.execute_input":"2024-08-06T14:42:15.487625Z","iopub.status.idle":"2024-08-06T14:42:15.678668Z","shell.execute_reply.started":"2024-08-06T14:42:15.487591Z","shell.execute_reply":"2024-08-06T14:42:15.677659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = torch.softmax(model1(test_X),1).argmax(1)\nres","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:42:38.315792Z","iopub.execute_input":"2024-08-06T14:42:38.316170Z","iopub.status.idle":"2024-08-06T14:42:38.341762Z","shell.execute_reply.started":"2024-08-06T14:42:38.316140Z","shell.execute_reply":"2024-08-06T14:42:38.340973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = 369\nplt.imshow(np.reshape(test_X[img].unsqueeze(0).cpu().detach().numpy(),(28,28)))\nplt.title(torch.softmax(model1(test_X[img].unsqueeze(0)),1).argmax(1).item())","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:43:05.740587Z","iopub.execute_input":"2024-08-06T14:43:05.741439Z","iopub.status.idle":"2024-08-06T14:43:05.966455Z","shell.execute_reply.started":"2024-08-06T14:43:05.741407Z","shell.execute_reply":"2024-08-06T14:43:05.965481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\noutput['Label'] = res.cpu()\noutput","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:44:36.040203Z","iopub.execute_input":"2024-08-06T14:44:36.040847Z","iopub.status.idle":"2024-08-06T14:44:36.062681Z","shell.execute_reply.started":"2024-08-06T14:44:36.040806Z","shell.execute_reply":"2024-08-06T14:44:36.061734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('/kaggle/working/submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T14:45:16.265632Z","iopub.execute_input":"2024-08-06T14:45:16.266081Z","iopub.status.idle":"2024-08-06T14:45:16.306843Z","shell.execute_reply.started":"2024-08-06T14:45:16.266051Z","shell.execute_reply":"2024-08-06T14:45:16.306135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}