{"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":"none","dataSources":[{"sourceType":"competition","sourceId":10418,"databundleVersionId":862236},{"sourceType":"competition","sourceId":27233,"databundleVersionId":2344753}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\nimport numpy as np\nimport pandas as pd\n\nimport einops\n\nimport cv2\nimport albumentations as A\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:36:58.421132Z","iopub.execute_input":"2026-04-17T10:36:58.421404Z","iopub.status.idle":"2026-04-17T10:37:04.507865Z","shell.execute_reply.started":"2026-04-17T10:36:58.421363Z","shell.execute_reply":"2026-04-17T10:37:04.506708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"p = \"/kaggle/input/competitions/human-protein-atlas-image-classification/train/\"\nimages = []\n\nfor n in [\n    \"00070df0-bbc3-11e8-b2bc-ac1f6b6435d0\", \n    \"000a6c98-bb9b-11e8-b2b9-ac1f6b6435d0\", \n    \"000a9596-bbc4-11e8-b2bc-ac1f6b6435d0\", \n    \"000c99ba-bba4-11e8-b2b9-ac1f6b6435d0\"\n]:\n    image = [\n        cv2.imread(f\"{p}{n}_{c}.png\", cv2.IMREAD_GRAYSCALE).astype(np.float32)/255 for c in ['red','green','blue','yellow']\n    ]\n    images += [np.stack(image, axis=-1)]\n\nimages[0].shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:19.773892Z","iopub.execute_input":"2026-04-06T06:46:19.774727Z","iopub.status.idle":"2026-04-06T06:46:19.882634Z","shell.execute_reply.started":"2026-04-06T06:46:19.774695Z","shell.execute_reply":"2026-04-06T06:46:19.881814Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in [254, 255, 256, 257, 258, 259]:\n    print (images[0][i][i])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:19.884171Z","iopub.execute_input":"2026-04-06T06:46:19.884628Z","iopub.status.idle":"2026-04-06T06:46:19.890204Z","shell.execute_reply.started":"2026-04-06T06:46:19.884580Z","shell.execute_reply":"2026-04-06T06:46:19.889525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def visualization(images):\n    \n    fig, axes = plt.subplots(1, 4, figsize=(20, 5))\n    \n    for i, image in enumerate(images):\n        \n        composite = np.zeros_like(image[:,:,:3])\n        \n        composite[:,:,0] = image[:,:,0] + image[:,:,3] * .5\n        composite[:,:,1] = image[:,:,1] + image[:,:,3] * .5\n        composite[:,:,2] = image[:,:,2]\n        \n        composite = np.clip(composite, 0, 1)\n        \n        axes[i].imshow(composite)\n        axes[i].axis('off')\n    \n    plt.tight_layout()\n    plt.show()\n\nvisualization(images)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:19.891375Z","iopub.execute_input":"2026-04-06T06:46:19.891876Z","iopub.status.idle":"2026-04-06T06:46:20.641110Z","shell.execute_reply.started":"2026-04-06T06:46:19.891848Z","shell.execute_reply":"2026-04-06T06:46:20.639968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = A.Compose([\n    A.HorizontalFlip(p=.5),\n    A.VerticalFlip(p=.3),\n    A.ShiftScaleRotate(\n        shift_limit=.1, \n        scale_limit=.2, \n        rotate_limit=30, \n        p=.5\n    )\n])\n\nvisualization([transform(image=image)['image'] for image in images])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:20.643595Z","iopub.execute_input":"2026-04-06T06:46:20.643970Z","iopub.status.idle":"2026-04-06T06:46:21.362615Z","shell.execute_reply.started":"2026-04-06T06:46:20.643934Z","shell.execute_reply":"2026-04-06T06:46:21.361612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = torch.randn(2, 3, 224, 224)\n\nfor e in x:\n    print (e[:, 0, 0])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.363993Z","iopub.execute_input":"2026-04-06T06:46:21.364392Z","iopub.status.idle":"2026-04-06T06:46:21.373507Z","shell.execute_reply.started":"2026-04-06T06:46:21.364350Z","shell.execute_reply":"2026-04-06T06:46:21.372620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lr = nn.Conv2d(3, 8, 3, \n               stride=1, \n               padding=0)\n\nfor e in lr(x):\n    print (e[:, 0, 0])\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.374612Z","iopub.execute_input":"2026-04-06T06:46:21.374886Z","iopub.status.idle":"2026-04-06T06:46:21.391802Z","shell.execute_reply.started":"2026-04-06T06:46:21.374861Z","shell.execute_reply":"2026-04-06T06:46:21.391053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lr(x).shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.392781Z","iopub.execute_input":"2026-04-06T06:46:21.393039Z","iopub.status.idle":"2026-04-06T06:46:21.406567Z","shell.execute_reply.started":"2026-04-06T06:46:21.393015Z","shell.execute_reply":"2026-04-06T06:46:21.405872Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lr = nn.Conv2d(3, 8, 3, \n               stride=1, \n               padding='same')\n\nfor e in lr(x):\n    print (e[:, -1, -1])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.407520Z","iopub.execute_input":"2026-04-06T06:46:21.407841Z","iopub.status.idle":"2026-04-06T06:46:21.425232Z","shell.execute_reply.started":"2026-04-06T06:46:21.407813Z","shell.execute_reply":"2026-04-06T06:46:21.423957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class BasicBlock(nn.Module):\n    \n    def __init__(self, in_channels, out_channels, stride=1, projection=None):\n        super().__init__()\n        \n        self.conv1 = nn.Conv2d(in_channels, out_channels, 3, stride=stride, padding=1, bias=False)\n        self.bn1 = nn.BatchNorm2d(out_channels)\n        self.relu = nn.ReLU(inplace=True)\n        \n        self.conv2 = nn.Conv2d(out_channels, out_channels, 3, stride=1, padding=1, bias=False)\n        self.bn2 = nn.BatchNorm2d(out_channels)\n        \n        self.projection = projection \n    \n    def forward(self, x):\n        \n        m = self.relu(self.bn1(self.conv1(x)))\n        m = self.bn2(self.conv2(m))\n        \n        if self.projection is not None: \n            x = self.projection(x)\n        \n        m = self.relu(m + x) \n        \n        return m\n\nclass ResNet34(nn.Module):\n    \n    def __init__(self, num_classes=1000):\n        super().__init__()\n        \n        self.conv1 = nn.Conv2d(3, 64, 7, stride=2, padding=3, bias=False)\n        self.bn1 = nn.BatchNorm2d(64)\n        self.relu = nn.ReLU(inplace=True)\n        \n        self.maxpool = nn.MaxPool2d(3, stride=2, padding=1)\n        \n        self.layer1 = self._make_layer(64, 64, 3, stride=1)\n        self.layer2 = self._make_layer(64, 128, 4, stride=2)\n        self.layer3 = self._make_layer(128, 256, 6, stride=2)\n        self.layer4 = self._make_layer(256, 512, 3, stride=2)\n        \n        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))\n        self.fc = nn.Linear(512, num_classes)\n    \n    def _make_layer(self, in_channels, out_channels, blocks, stride):\n        \n        projection = None\n        if stride != 1 or in_channels != out_channels:\n            projection = nn.Sequential(\n                nn.Conv2d(in_channels, out_channels, 1, stride=stride, bias=False),\n                nn.BatchNorm2d(out_channels)\n            )\n        \n        layers = [BasicBlock(in_channels, out_channels, stride, projection)]\n        for _ in range(1, blocks):\n            layers.append(BasicBlock(out_channels, out_channels))\n        \n        return nn.Sequential(*layers)\n    \n    def forward(self, x):\n        \n        x = self.relu(self.bn1(self.conv1(x)))  ## (N, 64, H/2, W/2)  \n        x = self.maxpool(x)                     ## (N, 64, H/4, W/4)  \n        \n        x = self.layer1(x)                      ## (N, 64, H/4, W/4)  \n        x = self.layer2(x)                      ## (N, 128, H/8, W/8)  \n        x = self.layer3(x)                      ## (N, 256, H/16, W/16)  \n        x = self.layer4(x)                      ## (N, 512, H/32, W/32)  \n        \n        x = self.avgpool(x)                     ## (N, 512, 1, 1)  \n        \n        x = torch.flatten(x, 1)\n        x = self.fc(x)\n        \n        return x\n\nml = ResNet34(1000)\n\nml(x).shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.426496Z","iopub.execute_input":"2026-04-06T06:46:21.426874Z","iopub.status.idle":"2026-04-06T06:46:21.887849Z","shell.execute_reply.started":"2026-04-06T06:46:21.426802Z","shell.execute_reply":"2026-04-06T06:46:21.886970Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class ResNeXtBlock(nn.Module):\n    \n    \"\"\"\n    BottleneckBlock ResNet50\n    ResNeXt50_32x4d \n    \"\"\"\n    \n    def __init__(self, in_channels, out_channels, stride=1, cardinality=32, width=4, projection=None):\n        super().__init__()\n        \n        group_width = cardinality * width\n        \n        self.conv1 = nn.Conv2d(in_channels, group_width, 1, bias=False)\n        self.bn1 = nn.BatchNorm2d(group_width)\n        \n        self.conv2 = nn.Conv2d(\n            group_width, \n            group_width, \n            3, \n            stride=stride, \n            padding=1, \n            groups=cardinality, \n            bias=False\n        )\n        self.bn2 = nn.BatchNorm2d(group_width)\n        \n        self.conv3 = nn.Conv2d(group_width, out_channels, 1, bias=False)\n        self.bn3 = nn.BatchNorm2d(out_channels)\n        \n        self.relu = nn.ReLU(inplace=True)\n        self.projection = projection\n    \n    def forward(self, x):\n        \n        m = self.relu(self.bn1(self.conv1(x)))\n        m = self.relu(self.bn2(self.conv2(m)))\n        m = self.bn3(self.conv3(m))\n        \n        if self.projection is not None:\n            x = self.projection(x)\n        \n        m += x\n        m = self.relu(m)\n        \n        return m\n\nx = torch.randn(2, 128, 32, 32)\nlr = ResNeXtBlock(128, 128)\n\nlr(x).shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.890320Z","iopub.execute_input":"2026-04-06T06:46:21.890748Z","iopub.status.idle":"2026-04-06T06:46:21.911621Z","shell.execute_reply.started":"2026-04-06T06:46:21.890722Z","shell.execute_reply":"2026-04-06T06:46:21.910803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lr = nn.Conv2d(128, 128, 3)\nprint (lr.weight.shape)\n\nlr = nn.Conv2d(128, 128, 3, \n               groups=16)\nprint (lr.weight.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.913114Z","iopub.execute_input":"2026-04-06T06:46:21.913439Z","iopub.status.idle":"2026-04-06T06:46:21.920186Z","shell.execute_reply.started":"2026-04-06T06:46:21.913412Z","shell.execute_reply":"2026-04-06T06:46:21.919364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class SeparableConvolution(nn.Module):\n\n    \"\"\"\n    GroupedConvolution -> SeparableConvolution \n    XceptionBlock \n    \"\"\"\n    \n    def __init__(self, in_channels, out_channels, kernel_size, stride=1):\n        super().__init__()\n        \n        ## depthwise convolution\n        self.depthwise = nn.Conv2d(\n            in_channels, in_channels, kernel_size,\n            stride=stride, padding=kernel_size//2, groups=in_channels\n        )\n        \n        ## pointwise convolution  \n        self.pointwise = nn.Conv2d(in_channels, out_channels, 1)\n        \n        self.bn = nn.BatchNorm2d(out_channels)\n    \n    def forward(self, x):\n        x = self.depthwise(x)\n        x = self.pointwise(x)\n        x = self.bn(x)\n        return x\n\nlr = SeparableConvolution(128, 128, 3) \n\nlr(x).shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.921172Z","iopub.execute_input":"2026-04-06T06:46:21.921522Z","iopub.status.idle":"2026-04-06T06:46:21.941068Z","shell.execute_reply.started":"2026-04-06T06:46:21.921494Z","shell.execute_reply":"2026-04-06T06:46:21.940177Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class InceptionBlock(nn.Module):\n    \n    def __init__(self, in_channels, ch1x1, ch3x3reduction, ch3x3, ch5x5reduction, ch5x5, pool_proj):\n        super().__init__()\n        \n        self.branch0 = nn.Sequential(\n            nn.Conv2d(in_channels, ch1x1, 1, bias=False),\n            nn.BatchNorm2d(ch1x1),\n            nn.ReLU(inplace=True)\n        )\n        \n        self.branch1 = nn.Sequential(\n            nn.Conv2d(in_channels, ch3x3reduction, 1, bias=False),\n            nn.BatchNorm2d(ch3x3reduction),\n            nn.ReLU(inplace=True),\n            \n            nn.Conv2d(ch3x3reduction, ch3x3, 3, padding=1, bias=False),\n            nn.BatchNorm2d(ch3x3),\n            nn.ReLU(inplace=True)\n        )\n        \n        self.branch2 = nn.Sequential(\n            nn.Conv2d(in_channels, ch5x5reduction, 1, bias=False),\n            nn.BatchNorm2d(ch5x5reduction),\n            nn.ReLU(inplace=True),\n            \n            nn.Conv2d(ch5x5reduction, ch5x5, 3, padding=1, bias=False),\n            nn.BatchNorm2d(ch5x5),\n            nn.ReLU(inplace=True),\n            \n            nn.Conv2d(ch5x5, ch5x5, 3, padding=1, bias=False),\n            nn.BatchNorm2d(ch5x5),\n            nn.ReLU(inplace=True)\n        )\n        \n        self.branch3 = nn.Sequential(\n            nn.MaxPool2d(3, stride=1, padding=1),\n            \n            nn.Conv2d(in_channels, pool_proj, 1, bias=False),\n            nn.BatchNorm2d(pool_proj),\n            nn.ReLU(inplace=True)\n        )\n    \n    def forward(self, x):\n        \n        b0 = self.branch0(x)\n        b1 = self.branch1(x)\n        b2 = self.branch2(x)\n        b3 = self.branch3(x)\n        \n        return torch.cat([b0, b1, b2, b3], dim=1)\n\nlr = InceptionBlock(128, 64, 96, 128, 16, 32, 32)\n\nlr(x).shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.942585Z","iopub.execute_input":"2026-04-06T06:46:21.942937Z","iopub.status.idle":"2026-04-06T06:46:21.979824Z","shell.execute_reply.started":"2026-04-06T06:46:21.942900Z","shell.execute_reply":"2026-04-06T06:46:21.979006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class SEBlock(nn.Module):\n    \n    def __init__(self, channels, reduction=4):\n        super().__init__()\n        \n        self.pool = nn.AdaptiveAvgPool2d(1)\n        \n        self.fc1 = nn.Conv2d(channels, channels // reduction, 1)\n        self.fc2 = nn.Conv2d(channels // reduction, channels, 1)\n        \n        self.swish = nn.SiLU()\n        self.sigmoid = nn.Sigmoid()\n    \n    def forward(self, x):\n        \n        y = self.pool(x)\n        y = self.swish(self.fc1(y))\n        y = self.sigmoid(self.fc2(y))\n        \n        return x * y\n\nclass MBConv(nn.Module):\n\n    \"\"\"\n    Mobile Inverted Bottleneck Convolution \n    \"\"\"\n    \n    def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, expand_ratio=6):\n        super().__init__()\n\n        self.residual = (stride == 1 and in_channels == out_channels)\n        self.stride = stride\n        \n        expanded_channels = in_channels * expand_ratio\n        self.expand_conv = nn.Conv2d(in_channels, expanded_channels, 1, bias=False)\n        self.expand_bn = nn.BatchNorm2d(expanded_channels)\n        \n        self.depthwise_conv = nn.Conv2d(\n            expanded_channels, expanded_channels, 3, \n            stride=stride, padding=1, \n            groups=expanded_channels, \n            bias=False\n        )\n        self.depthwise_bn = nn.BatchNorm2d(expanded_channels)\n        \n        self.se = SEBlock(expanded_channels)\n        \n        self.project_conv = nn.Conv2d(expanded_channels, out_channels, 1, bias=False)\n        self.project_bn = nn.BatchNorm2d(out_channels)\n        \n        self.swish = nn.SiLU()\n    \n    def forward(self, x):\n\n        ## inverted bottleneck: narrow → wide → narrow \n        ## resnet block: narrow -> wide  \n        \n        ## depthwise convolution\n        m = self.swish(self.expand_bn(self.expand_conv(x)))\n        m = self.swish(self.depthwise_bn(self.depthwise_conv(m)))\n        \n        ## squeeze-and-excitation \n        ## channel-wise attention \n        m = self.se(m)\n        \n        m = self.project_bn(self.project_conv(m))\n        if self.residual:\n            m += x\n            \n        return m\n\nlr = MBConv(128, 128) \n\nlr(x).shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:21.980920Z","iopub.execute_input":"2026-04-06T06:46:21.981364Z","iopub.status.idle":"2026-04-06T06:46:22.037374Z","shell.execute_reply.started":"2026-04-06T06:46:21.981328Z","shell.execute_reply":"2026-04-06T06:46:22.036656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def imitations(rate):\n    \n    labels = np.random.binomial(1, rate, 1000)\n    strategies = {\n        'optimal': rate, \n        'half': .5, \n        'maxi': .9, \n        'mini': rate/10 \n    }\n    \n    for name, prob in strategies.items():\n        \n        losses = []\n        for label in labels:\n            if label == 1:\n                loss = -np.log(prob)\n            else:\n                loss = -np.log(1 - prob)\n            losses.append(loss)\n    \n        print (name, np.mean(losses))\n\nfor rate in [.003, .03, .3]:\n    imitations(.03)\n    print ('\\n')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:22.038629Z","iopub.execute_input":"2026-04-06T06:46:22.038961Z","iopub.status.idle":"2026-04-06T06:46:22.063319Z","shell.execute_reply.started":"2026-04-06T06:46:22.038926Z","shell.execute_reply":"2026-04-06T06:46:22.062569Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class FocalLoss(nn.Module):\n\n    \"\"\"\n    Args:\n        inputs: predictions (N, C) - logits\n        targets: ground truth (N,) - class indices\n    \"\"\"\n    \n    def __init__(self, alpha=1.0, gamma=2.0, reduction='mean'):\n        super(FocalLoss, self).__init__()\n        \n        self.alpha = alpha\n        self.gamma = gamma\n        self.reduction = reduction\n    \n    def forward(self, inputs, targets):\n        \n        ce_loss = F.cross_entropy(inputs, targets, reduction='none')\n        \n        p = torch.exp(-ce_loss)\n        w = (1 - p) ** self.gamma\n        focal_loss = self.alpha * w * ce_loss\n        \n        if self.reduction == 'mean':\n            return focal_loss.mean()\n        elif self.reduction == 'sum':\n            return focal_loss.sum()\n        else:\n            return focal_loss\n\nxs = torch.randn(3, 4) \nys = torch.tensor([0, 2, 1])\n\ncriterion = FocalLoss()\ncriterion(xs, ys)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:22.064393Z","iopub.execute_input":"2026-04-06T06:46:22.064755Z","iopub.status.idle":"2026-04-06T06:46:22.106950Z","shell.execute_reply.started":"2026-04-06T06:46:22.064718Z","shell.execute_reply":"2026-04-06T06:46:22.106133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print (\n    1 * (1-np.exp(-.0954)) ** 2 * .0954, \n    1 * (1-np.exp(-.9054)) ** 2 * .9054, \n    1 * (1-np.exp(-17)) ** 2 * 17 \n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:22.107971Z","iopub.execute_input":"2026-04-06T06:46:22.108212Z","iopub.status.idle":"2026-04-06T06:46:22.113516Z","shell.execute_reply.started":"2026-04-06T06:46:22.108182Z","shell.execute_reply":"2026-04-06T06:46:22.112657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print (\n    (1 * (1-np.exp(-.1054)) ** 2 * .1054) - (1 * (1-np.exp(-.0954)) ** 2 * .0954), \n    (1 * (1-np.exp(-.9154)) ** 2 * .9154) - (1 * (1-np.exp(-.9054)) ** 2 * .9054), \n    (1 * (1-np.exp(-17.1)) ** 2 * 17.1) - (1 * (1-np.exp(-17)) ** 2 * 17) \n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-06T06:46:22.115346Z","iopub.execute_input":"2026-04-06T06:46:22.116059Z","iopub.status.idle":"2026-04-06T06:46:22.129489Z","shell.execute_reply.started":"2026-04-06T06:46:22.116005Z","shell.execute_reply":"2026-04-06T06:46:22.128647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_parquet(\"/kaggle/input/competitions/optiver-realized-volatility-prediction/book_train.parquet\")\n\ndf = df[df[\"time_id\"] == 5].copy()\ndf.reset_index(drop=True, \n               inplace=True)\n\ndf.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:37:15.858775Z","iopub.execute_input":"2026-04-17T10:37:15.859180Z","iopub.status.idle":"2026-04-17T10:37:26.221233Z","shell.execute_reply.started":"2026-04-17T10:37:15.859159Z","shell.execute_reply":"2026-04-17T10:37:26.220683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stocks = df[\"stock_id\"].unique()\nseconds = sorted(df[\"seconds_in_bucket\"].unique())\nindex = pd.MultiIndex.from_product([stocks, seconds], \n                                   names=['stock_id', 'seconds_in_bucket'])\ndf = df.set_index(['stock_id', 'seconds_in_bucket']).reindex(index).reset_index()\n\nprint (df.shape[0], len(stocks) * len(seconds))\ndf.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:37:26.222286Z","iopub.execute_input":"2026-04-17T10:37:26.222492Z","iopub.status.idle":"2026-04-17T10:37:26.264744Z","shell.execute_reply.started":"2026-04-17T10:37:26.222474Z","shell.execute_reply":"2026-04-17T10:37:26.264015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sc = df[['stock_id']].copy()\n\ndf = df.groupby('stock_id', group_keys=False, observed=True).ffill()\ndf['stock_id'] = sc['stock_id'].copy()\n\ndf.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:37:26.265619Z","iopub.execute_input":"2026-04-17T10:37:26.265871Z","iopub.status.idle":"2026-04-17T10:37:26.287775Z","shell.execute_reply.started":"2026-04-17T10:37:26.265847Z","shell.execute_reply":"2026-04-17T10:37:26.287130Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## (len(stocks), len(seconds) * len(features_space))  \n\nfeatures_space = [\n    'bid_price1', 'ask_price1', 'bid_price2', 'ask_price2', \n    'bid_size1', 'ask_size1', 'bid_size2', 'ask_size2'\n]\nconv112 = df.pivot_table(  \n    index='stock_id', \n    columns='seconds_in_bucket', \n    values=features_space, \n    observed=True\n) \n\nconv112.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:40:47.093858Z","iopub.execute_input":"2026-04-17T10:40:47.095132Z","iopub.status.idle":"2026-04-17T10:40:47.139941Z","shell.execute_reply.started":"2026-04-17T10:40:47.095091Z","shell.execute_reply":"2026-04-17T10:40:47.139287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## (len(stocks), len(seconds) * len(features_space))  \n## (len(features_space), len(stocks), len(seconds))  \n\nconv112 = conv112.values.reshape(  \n    len(stocks), \n    len(features_space), \n    len(seconds)\n).transpose(1, 0, 2)     \n\nconv112.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:40:48.136093Z","iopub.execute_input":"2026-04-17T10:40:48.136358Z","iopub.status.idle":"2026-04-17T10:40:48.143319Z","shell.execute_reply.started":"2026-04-17T10:40:48.136339Z","shell.execute_reply":"2026-04-17T10:40:48.142315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conv112 = torch.tensor(conv112, dtype=torch.float32).unsqueeze(0)\n\nconv112.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:40:57.550002Z","iopub.execute_input":"2026-04-17T10:40:57.550261Z","iopub.status.idle":"2026-04-17T10:40:57.557165Z","shell.execute_reply.started":"2026-04-17T10:40:57.550243Z","shell.execute_reply":"2026-04-17T10:40:57.556413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Einstein-Inspired Notation  \n\nconv112 = einops.rearrange(conv112, 'b f s t -> (b s) f t')\n\nconv112.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:40:58.924949Z","iopub.execute_input":"2026-04-17T10:40:58.925283Z","iopub.status.idle":"2026-04-17T10:40:58.931105Z","shell.execute_reply.started":"2026-04-17T10:40:58.925249Z","shell.execute_reply":"2026-04-17T10:40:58.930149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Einstein-Inspired Notation  \n\nconv112 = nn.Conv1d(len(features_space), 32, 3, stride=3)(conv112)\n\nconv112.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:41:04.823616Z","iopub.execute_input":"2026-04-17T10:41:04.823925Z","iopub.status.idle":"2026-04-17T10:41:04.833272Z","shell.execute_reply.started":"2026-04-17T10:41:04.823906Z","shell.execute_reply":"2026-04-17T10:41:04.832458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conv112 = einops.rearrange(conv112, '(b s) f t -> b f s t', s=len(stocks))\n\nconv112.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:41:12.503707Z","iopub.execute_input":"2026-04-17T10:41:12.504039Z","iopub.status.idle":"2026-04-17T10:41:12.510291Z","shell.execute_reply.started":"2026-04-17T10:41:12.504020Z","shell.execute_reply":"2026-04-17T10:41:12.509062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evs112 = torch.tensor(range(len(stocks))).unsqueeze(0)\nevs112 = nn.Embedding(len(stocks), 32)(evs112)\n\nevs112.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:41:14.664666Z","iopub.execute_input":"2026-04-17T10:41:14.664931Z","iopub.status.idle":"2026-04-17T10:41:14.670717Z","shell.execute_reply.started":"2026-04-17T10:41:14.664912Z","shell.execute_reply":"2026-04-17T10:41:14.670097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conv_and_evs = conv112 + evs112.transpose(1, 2).unsqueeze(-1)\n\nconv_and_evs.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:41:36.020319Z","iopub.execute_input":"2026-04-17T10:41:36.020593Z","iopub.status.idle":"2026-04-17T10:41:36.027171Z","shell.execute_reply.started":"2026-04-17T10:41:36.020575Z","shell.execute_reply":"2026-04-17T10:41:36.026449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"maxi, _ = torch.max(conv_and_evs, dim=2)\n\nmaxi.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:41:38.195989Z","iopub.execute_input":"2026-04-17T10:41:38.196259Z","iopub.status.idle":"2026-04-17T10:41:38.202304Z","shell.execute_reply.started":"2026-04-17T10:41:38.196238Z","shell.execute_reply":"2026-04-17T10:41:38.201575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evs1 = torch.tensor([0]).unsqueeze(0)\nevs1 = nn.Embedding(len(stocks), 32)(evs1)\n\nevs1.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:41:58.753811Z","iopub.execute_input":"2026-04-17T10:41:58.754098Z","iopub.status.idle":"2026-04-17T10:41:58.759832Z","shell.execute_reply.started":"2026-04-17T10:41:58.754075Z","shell.execute_reply":"2026-04-17T10:41:58.759133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"one = maxi + evs1.transpose(1, 2)\n\none.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-17T10:42:01.817479Z","iopub.execute_input":"2026-04-17T10:42:01.817745Z","iopub.status.idle":"2026-04-17T10:42:01.825789Z","shell.execute_reply.started":"2026-04-17T10:42:01.817718Z","shell.execute_reply":"2026-04-17T10:42:01.825049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}