{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":3649,"databundleVersionId":46718}],"dockerImageVersionId":31287,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Convulation operation : practice ","metadata":{}},{"cell_type":"code","source":"import torch \nimport torchvision \nfrom torchvision.transforms import transforms\nfrom torch.optim import Adam \nfrom torch.utils.data import Dataset, DataLoader","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:56:58.567969Z","iopub.execute_input":"2026-02-21T06:56:58.568236Z","iopub.status.idle":"2026-02-21T06:57:10.233248Z","shell.execute_reply.started":"2026-02-21T06:56:58.568203Z","shell.execute_reply":"2026-02-21T06:57:10.232622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize(256),\n    transforms.ToTensor()\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:10.234576Z","iopub.execute_input":"2026-02-21T06:57:10.235039Z","iopub.status.idle":"2026-02-21T06:57:10.238865Z","shell.execute_reply.started":"2026-02-21T06:57:10.235014Z","shell.execute_reply":"2026-02-21T06:57:10.238206Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_set = torchvision.datasets.CIFAR10(root=\"./data\",train=True,download=True,transform=transform)\ntest_set = torchvision.datasets.CIFAR10(root=\"./data\",train=False,download=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:10.239710Z","iopub.execute_input":"2026-02-21T06:57:10.239993Z","iopub.status.idle":"2026-02-21T06:57:14.689175Z","shell.execute_reply.started":"2026-02-21T06:57:10.239963Z","shell.execute_reply":"2026-02-21T06:57:14.688591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_set","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:14.690589Z","iopub.execute_input":"2026-02-21T06:57:14.690846Z","iopub.status.idle":"2026-02-21T06:57:14.696016Z","shell.execute_reply.started":"2026-02-21T06:57:14.690823Z","shell.execute_reply":"2026-02-21T06:57:14.695310Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loader = DataLoader(train_set,batch_size=32,shuffle=True)\ntest_loader = DataLoader(test_set,batch_size=32,shuffle=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:14.696889Z","iopub.execute_input":"2026-02-21T06:57:14.697129Z","iopub.status.idle":"2026-02-21T06:57:14.710114Z","shell.execute_reply.started":"2026-02-21T06:57:14.697107Z","shell.execute_reply":"2026-02-21T06:57:14.709592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for image,label in train_loader:\n    break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:14.710791Z","iopub.execute_input":"2026-02-21T06:57:14.711092Z","iopub.status.idle":"2026-02-21T06:57:14.916157Z","shell.execute_reply.started":"2026-02-21T06:57:14.711057Z","shell.execute_reply":"2026-02-21T06:57:14.915499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image.shape , label.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:29:06.849481Z","iopub.execute_input":"2026-02-21T06:29:06.849791Z","iopub.status.idle":"2026-02-21T06:29:06.855384Z","shell.execute_reply.started":"2026-02-21T06:29:06.849752Z","shell.execute_reply":"2026-02-21T06:29:06.854564Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### now lets train the CNN model ","metadata":{}},{"cell_type":"code","source":"## (3,256,256)  [32,32] -> (6,225,225) => in channels = 3 , outchannels = 6 , strides = 1 , padding = 0\n## (2x2) => (6,225,255) => (6,112,112)\n## ---\n## (6,112,112) [32,32] => in channels = 6 , outchannels = 12 , strides = 1 , padding = 0 => (12,81,81)\n## (2x2) => (12,40,40)\n# ----\n## (12,40,40) [ 32,32] => in channels = 6 , outchannels = 24 , strides = 1 , padding = 0 => (24,9,9)\n## (2x2) => (24,4,4)\n# ------------ flatten---------------------\n## (24x4x4) => linear layer 384 ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn as nn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:14.917113Z","iopub.execute_input":"2026-02-21T06:57:14.917406Z","iopub.status.idle":"2026-02-21T06:57:14.921220Z","shell.execute_reply.started":"2026-02-21T06:57:14.917376Z","shell.execute_reply":"2026-02-21T06:57:14.920524Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class MyCNN(nn.Module):\n    def __init__(self,num_class=10):\n        super().__init__()\n        self.num_class = num_class\n        self.conv1 = nn.Conv2d(in_channels=3,out_channels=6,kernel_size=32,stride=1,padding=0)\n        self.conv2 = nn.Conv2d(in_channels=6,out_channels=12,kernel_size=32,stride=1,padding=0)\n        self.conv3 = nn.Conv2d(in_channels=12,out_channels=24,kernel_size=32,stride=1,padding=0)\n        self.fc1 = nn.Linear(24*4*4,128)\n        self.pool = nn.MaxPool2d(2,2)\n        self.fc2 = nn.Linear(128,num_class)\n        self.relu = nn.ReLU() # applicable for all class \n    def forward(self,x):\n        x = self.pool(self.relu(self.conv1(x)))\n        x = self.pool(self.relu(self.conv2(x)))\n        x = self.pool(self.relu(self.conv3(x)))\n        x = torch.flatten(x, 1)\n        return (self.fc2(self.relu(self.fc1(x)))) # default :  binary  cross entropy ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:14.924217Z","iopub.execute_input":"2026-02-21T06:57:14.924560Z","iopub.status.idle":"2026-02-21T06:57:14.939734Z","shell.execute_reply.started":"2026-02-21T06:57:14.924535Z","shell.execute_reply":"2026-02-21T06:57:14.939224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.cuda \ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Device:{device}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:14.941411Z","iopub.execute_input":"2026-02-21T06:57:14.941720Z","iopub.status.idle":"2026-02-21T06:57:15.205725Z","shell.execute_reply.started":"2026-02-21T06:57:14.941697Z","shell.execute_reply":"2026-02-21T06:57:15.204895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = MyCNN(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:37:55.333461Z","iopub.execute_input":"2026-02-21T06:37:55.334096Z","iopub.status.idle":"2026-02-21T06:37:55.341841Z","shell.execute_reply.started":"2026-02-21T06:37:55.334050Z","shell.execute_reply":"2026-02-21T06:37:55.341228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:37:56.268527Z","iopub.execute_input":"2026-02-21T06:37:56.269249Z","iopub.status.idle":"2026-02-21T06:37:56.273770Z","shell.execute_reply.started":"2026-02-21T06:37:56.269217Z","shell.execute_reply":"2026-02-21T06:37:56.273036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model(image).shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:38:01.764476Z","iopub.execute_input":"2026-02-21T06:38:01.764782Z","iopub.status.idle":"2026-02-21T06:38:02.741779Z","shell.execute_reply.started":"2026-02-21T06:38:01.764747Z","shell.execute_reply":"2026-02-21T06:38:02.741067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = Adam(model.parameters(),lr=0.01)\ncriterion = nn.CrossEntropyLoss()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:42:24.564205Z","iopub.execute_input":"2026-02-21T06:42:24.565014Z","iopub.status.idle":"2026-02-21T06:42:24.568982Z","shell.execute_reply.started":"2026-02-21T06:42:24.564984Z","shell.execute_reply":"2026-02-21T06:42:24.568124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = MyCNN(10).to(device)\noptimizer = Adam(model.parameters(),lr=0.01)\ncriterion = nn.CrossEntropyLoss()\nepochs = 1\nfor epoch in range(epochs):\n    model.train()\n    total_loss = 0\n    for batch,label in train_loader:\n        batch,label = batch.to(device),label.to(device)\n        optimizer.zero_grad()\n        preds = model(batch)\n        loss = criterion(preds,label)\n        loss.backward()\n        optimizer.step()\n        total_loss += loss.item()\n    print(f\"Epoch:{epoch+1} | Loss :{total_loss/len(train_loader)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:57:15.206849Z","iopub.execute_input":"2026-02-21T06:57:15.207191Z","iopub.status.idle":"2026-02-21T07:03:36.752259Z","shell.execute_reply.started":"2026-02-21T06:57:15.207153Z","shell.execute_reply":"2026-02-21T07:03:36.751356Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## test part","metadata":{}},{"cell_type":"code","source":"import numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:28:25.308527Z","iopub.execute_input":"2026-02-21T06:28:25.308866Z","iopub.status.idle":"2026-02-21T06:28:25.316710Z","shell.execute_reply.started":"2026-02-21T06:28:25.308832Z","shell.execute_reply":"2026-02-21T06:28:25.315964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image = torch.zeros(32,24,4,4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:30:05.627305Z","iopub.execute_input":"2026-02-21T06:30:05.627602Z","iopub.status.idle":"2026-02-21T06:30:05.631237Z","shell.execute_reply.started":"2026-02-21T06:30:05.627577Z","shell.execute_reply":"2026-02-21T06:30:05.630637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:30:06.673022Z","iopub.execute_input":"2026-02-21T06:30:06.673320Z","iopub.status.idle":"2026-02-21T06:30:06.677915Z","shell.execute_reply.started":"2026-02-21T06:30:06.673293Z","shell.execute_reply":"2026-02-21T06:30:06.677266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image.view(-1,24*4*4).shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:30:14.956214Z","iopub.execute_input":"2026-02-21T06:30:14.957111Z","iopub.status.idle":"2026-02-21T06:30:14.962506Z","shell.execute_reply.started":"2026-02-21T06:30:14.957068Z","shell.execute_reply":"2026-02-21T06:30:14.961796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"b,c,x,y = image.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:30:55.737174Z","iopub.execute_input":"2026-02-21T06:30:55.737495Z","iopub.status.idle":"2026-02-21T06:30:55.741323Z","shell.execute_reply.started":"2026-02-21T06:30:55.737468Z","shell.execute_reply":"2026-02-21T06:30:55.740591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"b","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-21T06:30:57.558670Z","iopub.execute_input":"2026-02-21T06:30:57.559089Z","iopub.status.idle":"2026-02-21T06:30:57.563426Z","shell.execute_reply.started":"2026-02-21T06:30:57.559058Z","shell.execute_reply":"2026-02-21T06:30:57.562874Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}