{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Aditya Patel","metadata":{}},{"cell_type":"code","source":"# !pip install ndlinear\n# !pip install tensorboard","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T21:38:52.026003Z","iopub.execute_input":"2025-04-12T21:38:52.026791Z","iopub.status.idle":"2025-04-12T21:38:52.030712Z","shell.execute_reply.started":"2025-04-12T21:38:52.026757Z","shell.execute_reply":"2025-04-12T21:38:52.029840Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import datasets\nfrom torchvision.transforms import ToTensor\nfrom torch import nn\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom ndlinear import NdLinear\nfrom torch.utils.tensorboard import SummaryWriter\nfrom tensorboard import notebook","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-04-12T20:03:53.455588Z","iopub.execute_input":"2025-04-12T20:03:53.455908Z","iopub.status.idle":"2025-04-12T20:03:53.461071Z","shell.execute_reply.started":"2025-04-12T20:03:53.455882Z","shell.execute_reply":"2025-04-12T20:03:53.460240Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = ( \"cuda\" if torch.cuda.is_available() else 'cpu')\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:32.355728Z","iopub.execute_input":"2025-04-12T18:51:32.356092Z","iopub.status.idle":"2025-04-12T18:51:32.361183Z","shell.execute_reply.started":"2025-04-12T18:51:32.356063Z","shell.execute_reply":"2025-04-12T18:51:32.360159Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CustomTrain(Dataset):\n    def __init__(self,path,trans):\n        super().__init__()\n        self.data=pd.read_csv(path,header='infer').iloc[:-2000].values\n        self.transforms=trans\n        self.length=len(self.data)\n        \n        \n    def __len__(self):\n        return self.length\n\n    def __getitem__(self,idx):\n        img=self.data[idx,1:]\n        img=img.reshape((28,28,-1)).astype('uint8')\n        img=self.transforms(img)\n        target=self.data[idx,0]\n        return img,target\n        \nclass CustomTest(Dataset):\n    def __init__(self,path,trans):\n        super().__init__()\n        self.data=pd.read_csv(path,header='infer').iloc[-2000:].values\n        self.transforms=trans\n        self.length=len(self.data)\n        \n        \n    def __len__(self):\n        return self.length\n\n    def __getitem__(self,idx):\n        img=self.data[idx,1:]\n        img=img.reshape((28,28,-1)).astype('uint8')\n        img=self.transforms(img)\n        target=self.data[idx,0]\n        return img,target","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:35.918731Z","iopub.execute_input":"2025-04-12T18:51:35.919062Z","iopub.status.idle":"2025-04-12T18:51:35.927215Z","shell.execute_reply.started":"2025-04-12T18:51:35.919035Z","shell.execute_reply":"2025-04-12T18:51:35.926367Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_data=CustomTrain('/kaggle/input/digit-recognizer/train.csv',ToTensor())\ntesting_data=CustomTest('/kaggle/input/digit-recognizer/train.csv',ToTensor())","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:36.090317Z","iopub.execute_input":"2025-04-12T18:51:36.090620Z","iopub.status.idle":"2025-04-12T18:51:40.704418Z","shell.execute_reply.started":"2025-04-12T18:51:36.090596Z","shell.execute_reply":"2025-04-12T18:51:40.703633Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x,x_t=training_data[5]\nprint(x.shape)\nprint(x.dtype)\nprint(x_t)\nplt.imshow(x.permute(1,2,0))\nplt.show()\n\nx,x_t=testing_data[5]\nprint(x.shape)\nprint(x.dtype)\nprint(x_t)\nplt.imshow(x.permute(1,2,0))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:40.705772Z","iopub.execute_input":"2025-04-12T18:51:40.706027Z","iopub.status.idle":"2025-04-12T18:51:40.995457Z","shell.execute_reply.started":"2025-04-12T18:51:40.706005Z","shell.execute_reply":"2025-04-12T18:51:40.994301Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train=DataLoader(training_data,batch_size=16,shuffle=True)\ntest=DataLoader(testing_data,batch_size=16)","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:40.996601Z","iopub.execute_input":"2025-04-12T18:51:40.996907Z","iopub.status.idle":"2025-04-12T18:51:41.001615Z","shell.execute_reply.started":"2025-04-12T18:51:40.996883Z","shell.execute_reply":"2025-04-12T18:51:41.000721Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Task 1: Comparison of Convolution-linear head with Convolution-NdLinear head for Digit Recognizer","metadata":{}},{"cell_type":"code","source":"class dcnn_model(nn.Module):\n\n    def __init__(self):\n        super().__init__()\n        self.r=nn.ReLU()\n        self.c1=nn.Conv2d(1,8,(3,3))\n        self.b1=nn.BatchNorm2d(8)\n        self.mp1=nn.MaxPool2d((3,3),stride=1)\n        self.c2=nn.Conv2d(8,64,(3,3))\n        self.b2=nn.BatchNorm2d(64)\n        self.c3=nn.Conv2d(64,128,(3,3))\n        self.b3=nn.BatchNorm2d(128)\n        self.c4=nn.Conv2d(128,64,(3,3))\n        self.b4=nn.BatchNorm2d(64)\n        self.c5=nn.Conv2d(64,32,(3,3))\n        self.b5=nn.BatchNorm2d(32)\n        self.l1=nn.Linear(8192,1024)\n        self.l2=nn.Linear(1024,10)\n        self.f=nn.Flatten()\n        return\n\n    def forward(self,x):\n        x=self.c1(x)\n        x=self.b1(x)\n        x=self.r(x)\n        x=self.mp1(x)\n        x=self.c2(x)\n        x=self.b2(x)\n        x=self.r(x)\n        x=self.c3(x)\n        x=self.b3(x)\n        x=self.r(x)\n        x=self.c4(x)\n        x=self.b4(x)\n        x=self.r(x)\n        x=self.c5(x)\n        x=self.b5(x)\n        x=self.r(x)\n        x=self.f(x)\n        x=self.l1(x)\n        x=self.r(x)\n        x=self.l2(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:46.130950Z","iopub.execute_input":"2025-04-12T18:51:46.131748Z","iopub.status.idle":"2025-04-12T18:51:46.140163Z","shell.execute_reply.started":"2025-04-12T18:51:46.131717Z","shell.execute_reply":"2025-04-12T18:51:46.139176Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_fn(model,loss,optim,train,writer,epoch):\n    model.train()\n    final_acc=0\n    for i,(data,label) in enumerate(train):\n        data=data.to(device)\n        label=label.to(device)\n        optim.zero_grad()\n        pred=model(data)\n        loss=loss_fn(pred,label)\n        loss.backward()\n        optim.step()\n        pred=torch.argmax(pred,dim=1)\n        acc=(pred==label).sum().item()\n        final_acc+=acc\n        # print(\"Train Batch\",i,\"Accuracy :\",acc/16)\n    print(\"Epoch Accuracy :\",final_acc/(16*len(train)))\n    writer.add_scalar(\"Accuracy Training\", final_acc/(16*len(train)), epoch)","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:46.485698Z","iopub.execute_input":"2025-04-12T18:51:46.486320Z","iopub.status.idle":"2025-04-12T18:51:46.491708Z","shell.execute_reply.started":"2025-04-12T18:51:46.486295Z","shell.execute_reply":"2025-04-12T18:51:46.490824Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def eval_fn(model,test):\n    model.eval()\n    final_acc=0\n    # df=pd.read_csv(path,header='infer')\n    with torch.no_grad():\n        for i,(data,label) in enumerate(test):\n            data=data.to(device)\n            pred=model(data)\n            pred=torch.argmax(pred,dim=1).detach().cpu()\n            acc=(pred==label).sum().item()\n            final_acc+=acc\n    print(\"Test Accuracy :\",final_acc/(16*len(test)))","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:48.584813Z","iopub.execute_input":"2025-04-12T18:51:48.585120Z","iopub.status.idle":"2025-04-12T18:51:48.590441Z","shell.execute_reply.started":"2025-04-12T18:51:48.585094Z","shell.execute_reply":"2025-04-12T18:51:48.589504Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"writer = SummaryWriter('linear')\nmodel=dcnn_model().to(device)\nloss_fn=nn.CrossEntropyLoss()\noptim=torch.optim.Adam(model.parameters(),lr=0.001)\nepochs=10\nfor epoch in range(epochs):\n    train_fn(model,loss_fn,optim,train,writer,epoch)\neval_fn(model,test)\nwriter.flush()\nwriter.close()","metadata":{"execution":{"iopub.status.busy":"2025-04-12T18:51:53.662371Z","iopub.execute_input":"2025-04-12T18:51:53.662688Z","iopub.status.idle":"2025-04-12T18:54:21.383351Z","shell.execute_reply.started":"2025-04-12T18:51:53.662664Z","shell.execute_reply":"2025-04-12T18:54:21.382497Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorboard.backend.event_processing import event_accumulator\n\nea = event_accumulator.EventAccumulator('linear')\nea.Reload()\n\nscalars = ea.Scalars('Accuracy Training')\nsteps = [s.step for s in scalars]\nvalues = [s.value for s in scalars]\n\nimport matplotlib.pyplot as plt\nplt.plot(steps, values)\nplt.xlabel(\"Step\")\nplt.ylabel(\"Loss\")\nplt.title(\"Train Accuracy\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T18:56:28.685103Z","iopub.execute_input":"2025-04-12T18:56:28.685477Z","iopub.status.idle":"2025-04-12T18:56:28.858671Z","shell.execute_reply.started":"2025-04-12T18:56:28.685449Z","shell.execute_reply":"2025-04-12T18:56:28.857797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class dcnn_Ld_model(nn.Module):\n\n    def __init__(self):\n        super().__init__()\n        self.r=nn.ReLU()\n        self.c1=nn.Conv2d(1,8,(3,3))\n        self.b1=nn.BatchNorm2d(8)\n        self.mp1=nn.MaxPool2d((3,3),stride=1)\n        self.c2=nn.Conv2d(8,64,(3,3))\n        self.b2=nn.BatchNorm2d(64)\n        self.c3=nn.Conv2d(64,128,(3,3))\n        self.b3=nn.BatchNorm2d(128)\n        self.c4=nn.Conv2d(128,64,(3,3))\n        self.b4=nn.BatchNorm2d(64)\n        self.c5=nn.Conv2d(64,32,(3,3))\n        self.b5=nn.BatchNorm2d(32) #B, 32, 16, 16\n        # self.l1=nn.Linear(8192,1024)\n        self.l1=NdLinear(input_dims=(32,16,16),hidden_size=(16,8,8))\n        self.l2=nn.Linear(1024,10)\n        self.f=nn.Flatten()\n        return\n\n    def forward(self,x):\n        x=self.c1(x)\n        x=self.b1(x)\n        x=self.r(x)\n        x=self.mp1(x)\n        x=self.c2(x)\n        x=self.b2(x)\n        x=self.r(x)\n        x=self.c3(x)\n        x=self.b3(x)\n        x=self.r(x)\n        x=self.c4(x)\n        x=self.b4(x)\n        x=self.r(x)\n        x=self.c5(x)\n        x=self.b5(x)\n        x=self.r(x)\n        x=self.l1(x)\n        x=self.r(x)\n        x=self.f(x)\n        x=self.l2(x)\n        return x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T18:57:14.350495Z","iopub.execute_input":"2025-04-12T18:57:14.351080Z","iopub.status.idle":"2025-04-12T18:57:14.359576Z","shell.execute_reply.started":"2025-04-12T18:57:14.351050Z","shell.execute_reply":"2025-04-12T18:57:14.358694Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"writer = SummaryWriter('NdLinear')\nmodel_Ld=dcnn_Ld_model().to(device)\nloss_fn=nn.CrossEntropyLoss()\noptim=torch.optim.Adam(model_Ld.parameters(),lr=0.001)\nepochs=10\nfor epoch in range(epochs):\n    train_fn(model_Ld,loss_fn,optim,train,writer,epoch)\neval_fn(model_Ld,test)\nwriter.flush()\nwriter.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T18:57:18.103168Z","iopub.execute_input":"2025-04-12T18:57:18.103505Z","iopub.status.idle":"2025-04-12T18:59:27.580422Z","shell.execute_reply.started":"2025-04-12T18:57:18.103479Z","shell.execute_reply":"2025-04-12T18:59:27.579531Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorboard.backend.event_processing import event_accumulator\n\nea = event_accumulator.EventAccumulator('NdLinear')\nea.Reload()\n\nscalars = ea.Scalars('Accuracy Training')\nsteps = [s.step for s in scalars]\nvalues = [s.value for s in scalars]\n\nimport matplotlib.pyplot as plt\nplt.plot(steps, values)\nplt.xlabel(\"Step\")\nplt.ylabel(\"Loss\")\nplt.title(\"Train Accuracy\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T18:59:27.581798Z","iopub.execute_input":"2025-04-12T18:59:27.582050Z","iopub.status.idle":"2025-04-12T18:59:27.734975Z","shell.execute_reply.started":"2025-04-12T18:59:27.582028Z","shell.execute_reply":"2025-04-12T18:59:27.734154Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##### It shows that similar performance can be achieved with lesser parameters\n##### But it is important to understand if the model is actually able to distinguish based on feature structure, for this in the following section I have employed a auto-encoder for visulaizing the embeddings and distiction between classes","metadata":{}},{"cell_type":"code","source":"# notebook.list()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:50:11.089880Z","iopub.execute_input":"2025-04-12T20:50:11.090641Z","iopub.status.idle":"2025-04-12T20:50:11.094096Z","shell.execute_reply.started":"2025-04-12T20:50:11.090611Z","shell.execute_reply":"2025-04-12T20:50:11.093200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# notebook.display(port=6007, height=500)  # or just notebook.display()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T18:59:27.742047Z","iopub.execute_input":"2025-04-12T18:59:27.742346Z","iopub.status.idle":"2025-04-12T18:59:27.750072Z","shell.execute_reply.started":"2025-04-12T18:59:27.742310Z","shell.execute_reply":"2025-04-12T18:59:27.749445Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Auto Encoder Architecture for understading the embedding space of each class","metadata":{}},{"cell_type":"code","source":"class down_Ld_model(nn.Module):\n\n    def __init__(self):\n        super().__init__()\n        self.r=nn.ReLU()\n        self.c1=nn.Conv2d(1,4,(3,3)) #26\n        self.c2=nn.Conv2d(4,8,(3,3)) #24\n        self.c3=nn.Conv2d(8,16,(5,5)) #20\n        self.c4=nn.Conv2d(16,32,(5,5)) #16\n        self.l1=NdLinear(input_dims=(32,16,16),hidden_size=(16,8,8))\n        return\n\n    def forward(self,x,skip):\n        x=self.c1(x)\n        skip.append(x)\n        x=self.r(x)\n        x=self.c2(x)\n        skip.append(x)\n        x=self.r(x)\n        x=self.c3(x)\n        skip.append(x)\n        x=self.r(x)\n        x=self.c4(x)\n        skip.append(x)\n        x=self.r(x)\n        x=self.l1(x)\n        return x\n\nclass up_Ld_model(nn.Module):\n    \n    def __init__(self):\n        super().__init__()\n        self.r=nn.ReLU()\n        # self.l1=NdLinear(input_dims=(16,8,8),hidden_size=(32,16,16))\n        self.up1_1=nn.ConvTranspose2d(16,32,kernel_size=3)\n        self.up1_2=nn.ConvTranspose2d(32,64,kernel_size=3)\n        self.up1_3=nn.ConvTranspose2d(64,32,kernel_size=5)\n        self.c1_1=nn.Conv2d(64,32,kernel_size=3)\n        self.up2=nn.ConvTranspose2d(32,16,kernel_size=7)\n        self.c2_1=nn.Conv2d(32,16,kernel_size=3)\n        self.up3=nn.ConvTranspose2d(16,8,kernel_size=7)\n        self.c3_1=nn.Conv2d(16,8,kernel_size=3)\n        self.up4=nn.ConvTranspose2d(8,4,kernel_size=5)\n        self.c4_1=nn.Conv2d(8,4,kernel_size=3)\n        self.up5=nn.ConvTranspose2d(4,2,kernel_size=5)\n        self.c5_1=nn.Conv2d(2,2,kernel_size=1)\n        \n    def forward(self,x,skip):\n        # x=self.l1(x)\n        x=self.up1_1(x)\n        x=self.up1_2(x)\n        x=self.up1_3(x)\n        x=torch.cat([x,skip[-1]],dim=1)\n        x=self.c1_1(x)\n        x=self.r(x)\n        x=self.up2(x)\n        x=torch.cat([x,skip[-2]],dim=1)\n        x=self.c2_1(x)\n        x=self.r(x)\n        x=self.up3(x)\n        x=torch.cat([x,skip[-3]],dim=1)\n        # print(x.shape)\n        x=self.c3_1(x)\n        # print(x.shape)\n        x=self.r(x)\n        x=self.up4(x)\n        # print(x.shape)\n        x=torch.cat([x,skip[-4]],dim=1)\n        x=self.c4_1(x)\n        x=self.r(x)\n        x=self.up5(x)\n        x=self.c5_1(x)\n        return x\n\nclass Unet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.down=down_Ld_model()\n        self.up=up_Ld_model()\n        return\n\n    def forward(self,x):\n        skip=[]\n        emb=self.down(x,skip)\n        x=self.up(emb,skip)\n        return emb,x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T19:35:56.042549Z","iopub.execute_input":"2025-04-12T19:35:56.042885Z","iopub.status.idle":"2025-04-12T19:35:56.056482Z","shell.execute_reply.started":"2025-04-12T19:35:56.042859Z","shell.execute_reply":"2025-04-12T19:35:56.055644Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_fn(model,loss,optim,train,writer,epoch):\n    model.train()\n    dice_acc=0\n    l=0\n    for i,(data,_) in enumerate(train):\n        data=data.to(device)\n        data[data>0]=1\n        optim.zero_grad()\n        _,pred=model(data)\n        data=data.squeeze(1).to(torch.long)\n        loss=loss_fn(pred,data)\n        loss.backward()\n        optim.step()\n        pred=torch.argmax(pred,dim=1)\n        # pred[pred>=0.5]=1\n        # pred[pred<0.5]=0\n        x=(pred==1).sum().item()\n        y=(data==1).sum().item()\n        pred[pred==0]=2\n        xandy=(pred==data).sum().item()\n        dice_acc+=2*xandy/(x+y)\n        pred[pred==2]=0\n        l+=loss.item()\n        del loss,data,pred\n        torch.cuda.empty_cache()\n    print(\"Epoch Accuracy :\",dice_acc/(len(train)))\n    print(\"Epoch Loss :\",l/(len(train)))\n    writer.add_scalar(\"Accuracy Training\", dice_acc/(len(train)), epoch)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:44:48.554194Z","iopub.execute_input":"2025-04-12T20:44:48.555022Z","iopub.status.idle":"2025-04-12T20:44:48.562048Z","shell.execute_reply.started":"2025-04-12T20:44:48.554994Z","shell.execute_reply":"2025-04-12T20:44:48.561035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def eval_fn(model,test):\n    model.eval()\n    dice_acc=0\n    with torch.no_grad():\n        for i,(data,_) in enumerate(test):\n            data=data.to(device)\n            data[data>0]=1\n            _,pred=model(data)\n            data=data.squeeze(1).to(torch.long)\n            pred=torch.argmax(pred,dim=1)\n            # pred[pred>=0.5]=1\n            # pred[pred<0.5]=0\n            x=(pred==1).sum().item()\n            y=(data==1).sum().item()\n            pred[pred==0]=2\n            xandy=(pred==data).sum().item()\n            dice_acc+=2*xandy/(x+y)\n            pred[pred==2]=0\n            del data,pred\n            torch.cuda.empty_cache()\n    print(\"Test Accuracy :\",dice_acc/(len(test)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:44:48.744671Z","iopub.execute_input":"2025-04-12T20:44:48.745222Z","iopub.status.idle":"2025-04-12T20:44:48.750821Z","shell.execute_reply.started":"2025-04-12T20:44:48.745192Z","shell.execute_reply":"2025-04-12T20:44:48.749923Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"writer = SummaryWriter('Unet')\nmodel_un=Unet().to(device)\nloss_fn=nn.CrossEntropyLoss()\noptim=torch.optim.Adam(model_un.parameters(),lr=0.001)\nepochs=10\nfor epoch in range(epochs):\n    train_fn(model_un,loss_fn,optim,train,writer,epoch)\neval_fn(model_un,test)\nwriter.flush()\nwriter.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:51:32.198557Z","iopub.execute_input":"2025-04-12T20:51:32.199060Z","iopub.status.idle":"2025-04-12T20:54:39.536468Z","shell.execute_reply.started":"2025-04-12T20:51:32.199013Z","shell.execute_reply":"2025-04-12T20:54:39.535596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorboard.backend.event_processing import event_accumulator\n\nea = event_accumulator.EventAccumulator('Unet')\nea.Reload()\n\nscalars = ea.Scalars('Accuracy Training')\nsteps = [s.step for s in scalars]\nvalues = [s.value for s in scalars]\n\nimport matplotlib.pyplot as plt\nplt.plot(steps, values)\nplt.xlabel(\"Step\")\nplt.ylabel(\"Loss\")\nplt.title(\"Train Accuracy\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:54:39.538105Z","iopub.execute_input":"2025-04-12T20:54:39.538395Z","iopub.status.idle":"2025-04-12T20:54:39.678480Z","shell.execute_reply.started":"2025-04-12T20:54:39.538373Z","shell.execute_reply":"2025-04-12T20:54:39.677707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def gen_emb(model,train,test):\n    model.eval()\n    e=np.empty([0,1025])\n    with torch.no_grad():\n        for i,(data,label) in enumerate(train):\n            data=data.to(device)\n            data[data>0]=1\n            emb,_=model(data)\n            emb=emb.flatten(1)\n            emb=np.append(emb.cpu().numpy(),label.unsqueeze(-1).numpy(),axis=1)\n            e=np.append(e,emb,axis=0)\n        \n        for i,(data,label) in enumerate(test):\n            data=data.to(device)\n            data[data>0]=1\n            emb,_=model(data)\n            emb=emb.flatten(1)\n            emb=np.append(emb.cpu().numpy(),label.unsqueeze(-1).numpy(),axis=1)\n            e=np.append(e,emb,axis=0)\n    return e","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:31:10.298039Z","iopub.execute_input":"2025-04-12T20:31:10.298364Z","iopub.status.idle":"2025-04-12T20:31:10.305066Z","shell.execute_reply.started":"2025-04-12T20:31:10.298339Z","shell.execute_reply":"2025-04-12T20:31:10.304047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"embedding=gen_emb(model_un,train,test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:31:11.089286Z","iopub.execute_input":"2025-04-12T20:31:11.089574Z","iopub.status.idle":"2025-04-12T20:34:01.023838Z","shell.execute_reply.started":"2025-04-12T20:31:11.089552Z","shell.execute_reply":"2025-04-12T20:34:01.023062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.manifold import TSNE\n\n# The default of 1,000 iterations gives fine results, but I'm training for longer just to eke\n# out some marginal improvements. NB: This takes almost an hour!\ndf=pd.DataFrame()\ntsne = TSNE(random_state=1, n_iter=1000, metric=\"cosine\")\n\nembs = tsne.fit_transform(embedding[:,:-1])\n# Add to dataframe for convenience\ndf['x'] = embs[:, 0]\ndf['y'] = embs[:, 1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:36:56.607905Z","iopub.execute_input":"2025-04-12T20:36:56.608243Z","iopub.status.idle":"2025-04-12T20:42:11.149103Z","shell.execute_reply.started":"2025-04-12T20:36:56.608218Z","shell.execute_reply":"2025-04-12T20:42:11.148094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"FS = (8, 6)\nfig, ax = plt.subplots(figsize=FS)\n# Make points translucent so we can visually identify regions with a high density of overlapping points\nc=['orange','red','green','yellow','blue','black','purple','cyan','grey','pink']\nfor i in range(0,10):\n    t=df[embedding[:,-1]==i]\n    ax.scatter(t.x, t.y, alpha=.1,c=c[i])\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T20:43:37.239098Z","iopub.execute_input":"2025-04-12T20:43:37.239467Z","iopub.status.idle":"2025-04-12T20:43:37.642512Z","shell.execute_reply.started":"2025-04-12T20:43:37.239440Z","shell.execute_reply":"2025-04-12T20:43:37.641590Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##### Although the model has overfitted very early in the training cycle, but we can clearly observe that it has created a separation of embeddings between different classes, thus NdLinear has understood the structural features of the input","metadata":{}}]}