{
  "id": 587558,
  "title": "About 1D convolutions",
  "url": "/competitions/cmi-detect-behavior-with-sensor-data/discussion/587558",
  "author_name": "",
  "post_date": "2025-07-01T14:57:46.038867800Z",
  "votes": 44,
  "comment_count": 2,
  "views": 0,
  "content": "<p>I wanted to share a little diagram I made which I found useful when designing 1D-CNN architectures as sometimes input and output shapes can get a bit confusing.</p>\n<p>If we have:</p>\n<ul>\n<li>An input tensor of shape <code>(B, C_in, L_in)</code> (batch size, input channels, input sequence length).</li>\n<li>The output will have shape <code>(B, C_out, L_out)</code> (batch size, output channels, output sequence length).</li>\n<li>Then the 1D-CNN kernel will have weights of shape <code>(C_out, C_in, kernel_size)</code></li>\n</ul>\n<p><em>Disclaimer: the diagram numerical values are for illustration purposes only</em></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197853%2F74819e0ff09d05a84e22d13b58cc88c5%2F1d-cnn-shape.png?generation=1751381120080184&amp;alt=media\" alt=\"\"></p>\n<p>When determining a 1D convolution you are in charge of designing the <code>C_out</code> and <code>kernel_size</code>. This in turn will determine the <code>L_out</code> size by the formula:<br>\n$$<br>\nL_{out} = \\left\\lfloor \\frac{L_{in} + 2\\cdot \\text{pad} - \\text{dilation} \\cdot (\\text{kernel size} - 1) - 1}{\\text{stride}} + 1 \\right\\rfloor<br>\n$$</p>\n<p>So, essentially, I try to think about the kernel shape as having <code>C_out</code> number of kernels of shape <code>(C_in, kernel_size)</code> where each channel in the kernel \"takes care\" of each channel in the input and I need <code>C_out</code> number of these kernels because I want the output to have <code>C_out</code> channels.</p>\n<p>You can check tensor shapes with this simple code:</p>\n<pre><code>input_tensor = torch.randn(, , )\nB, C_in, L = input_tensor.shape\n\nC_out = \nkernel_size = \n\nconv1d = nn.Conv1d(\n    in_channels=C_in,\n    out_channels=C_out,\n    kernel_size=kernel_size,\n    padding=,\n    stride=,\n    dilation=\n)\noutput_tensor = conv1d(input_tensor)\n\n()\n()\n()\n</code></pre>\n<p>Also, I am leaving <a href=\"https://jinglescode.github.io/2020/11/01/how-convolutional-layers-work-deep-learning-neural-networks/\" target=\"_blank\">this blog</a> which has some great animations.</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197853%2F283aac0afb7dd4067da393c16fe104b8%2F1d-cnn-animation-1.gif?generation=1751381663094261&amp;alt=media\" alt=\"\"></p>\n<p>Happy Kaggling!</p>",
  "messages": [
    {
      "id": "3238108",
      "postDate": "07/01/2025 14:57:46",
      "content": "<p>I wanted to share a little diagram I made which I found useful when designing 1D-CNN architectures as sometimes input and output shapes can get a bit confusing.</p>\n<p>If we have:</p>\n<ul>\n<li>An input tensor of shape <code>(B, C_in, L_in)</code> (batch size, input channels, input sequence length).</li>\n<li>The output will have shape <code>(B, C_out, L_out)</code> (batch size, output channels, output sequence length).</li>\n<li>Then the 1D-CNN kernel will have weights of shape <code>(C_out, C_in, kernel_size)</code></li>\n</ul>\n<p><em>Disclaimer: the diagram numerical values are for illustration purposes only</em></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197853%2F74819e0ff09d05a84e22d13b58cc88c5%2F1d-cnn-shape.png?generation=1751381120080184&amp;alt=media\" alt=\"\"></p>\n<p>When determining a 1D convolution you are in charge of designing the <code>C_out</code> and <code>kernel_size</code>. This in turn will determine the <code>L_out</code> size by the formula:<br>\n$$<br>\nL_{out} = \\left\\lfloor \\frac{L_{in} + 2\\cdot \\text{pad} - \\text{dilation} \\cdot (\\text{kernel size} - 1) - 1}{\\text{stride}} + 1 \\right\\rfloor<br>\n$$</p>\n<p>So, essentially, I try to think about the kernel shape as having <code>C_out</code> number of kernels of shape <code>(C_in, kernel_size)</code> where each channel in the kernel \"takes care\" of each channel in the input and I need <code>C_out</code> number of these kernels because I want the output to have <code>C_out</code> channels.</p>\n<p>You can check tensor shapes with this simple code:</p>\n<pre><code>input_tensor = torch.randn(, , )\nB, C_in, L = input_tensor.shape\n\nC_out = \nkernel_size = \n\nconv1d = nn.Conv1d(\n    in_channels=C_in,\n    out_channels=C_out,\n    kernel_size=kernel_size,\n    padding=,\n    stride=,\n    dilation=\n)\noutput_tensor = conv1d(input_tensor)\n\n()\n()\n()\n</code></pre>\n<p>Also, I am leaving <a href=\"https://jinglescode.github.io/2020/11/01/how-convolutional-layers-work-deep-learning-neural-networks/\" target=\"_blank\">this blog</a> which has some great animations.</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197853%2F283aac0afb7dd4067da393c16fe104b8%2F1d-cnn-animation-1.gif?generation=1751381663094261&amp;alt=media\" alt=\"\"></p>\n<p>Happy Kaggling!</p>",
      "rawMarkdown": "I wanted to share a little diagram I made which I found useful when designing 1D-CNN architectures as sometimes input and output shapes can get a bit confusing.\n\nIf we have:\n- An input tensor of shape `(B, C_in, L_in)` (batch size, input channels, input sequence length).\n- The output will have shape `(B, C_out, L_out)` (batch size, output channels, output sequence length).\n- Then the 1D-CNN kernel will have weights of shape `(C_out, C_in, kernel_size)`\n\n*Disclaimer: the diagram numerical values are for illustration purposes only*\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197853%2F74819e0ff09d05a84e22d13b58cc88c5%2F1d-cnn-shape.png?generation=1751381120080184&alt=media)\n\nWhen determining a 1D convolution you are in charge of designing the `C_out` and `kernel_size`. This in turn will determine the `L_out` size by the formula:\n$$\nL_{out} = \\left\\lfloor \\frac{L_{in} + 2\\cdot \\text{pad} - \\text{dilation} \\cdot (\\text{kernel size} - 1) - 1}{\\text{stride}} + 1 \\right\\rfloor\n$$\n\nSo, essentially, I try to think about the kernel shape as having `C_out` number of kernels of shape `(C_in, kernel_size)` where each channel in the kernel \"takes care\" of each channel in the input and I need `C_out` number of these kernels because I want the output to have `C_out` channels.\n\nYou can check tensor shapes with this simple code:\n```python\ninput_tensor = torch.randn(32, 8, 100)\nB, C_in, L = input_tensor.shape\n\nC_out = 12\nkernel_size = 3\n\nconv1d = nn.Conv1d(\n    in_channels=C_in,\n    out_channels=C_out,\n    kernel_size=kernel_size,\n    padding=4,\n    stride=2,\n    dilation=2\n)\noutput_tensor = conv1d(input_tensor)\n\nprint(f\"Input tensor shape: {input_tensor.shape}\")\nprint(f\"Output tensor shape: {output_tensor.shape}\")\nprint(f\"Kernel shape: {conv1d.weight.shape}\")\n```\n\nAlso, I am leaving [this blog](https://jinglescode.github.io/2020/11/01/how-convolutional-layers-work-deep-learning-neural-networks/) which has some great animations.\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197853%2F283aac0afb7dd4067da393c16fe104b8%2F1d-cnn-animation-1.gif?generation=1751381663094261&alt=media)\n\nHappy Kaggling!",
      "votes": null
    },
    {
      "id": "3238144",
      "postDate": "07/01/2025 15:40:30",
      "content": "<p>Let me add the link to my favorite explanation of convolutions: <a href=\"https://stackoverflow.com/a/44628011/5108365\" target=\"_blank\">https://stackoverflow.com/a/44628011/5108365</a></p>",
      "rawMarkdown": "Let me add the link to my favorite explanation of convolutions: https://stackoverflow.com/a/44628011/5108365",
      "votes": null
    },
    {
      "id": "3238208",
      "postDate": "07/01/2025 16:32:42",
      "content": "<p>Thanks for sharing</p>",
      "rawMarkdown": "Thanks for sharing",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 3238144,
      "author_name": "ogurtsov",
      "author_url": "",
      "post_date": "07/01/2025 15:40:30",
      "content": "<p>Let me add the link to my favorite explanation of convolutions: <a href=\"https://stackoverflow.com/a/44628011/5108365\" target=\"_blank\">https://stackoverflow.com/a/44628011/5108365</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 3238208,
          "author_name": "alejopaullier",
          "author_url": "",
          "post_date": "07/01/2025 16:32:42",
          "content": "<p>Thanks for sharing</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "3238108": "I wanted to share a little diagram I made which I found useful when designing 1D-CNN architectures as sometimes input and output shapes can get a bit confusing.\n\nIf we have:\n- An input tensor of shape `(B, C_in, L_in)` (batch size, input channels, input sequence length).\n- The output will have shape `(B, C_out, L_out)` (batch size, output channels, output sequence length).\n- Then the 1D-CNN kernel will have weights of shape `(C_out, C_in, kernel_size)`\n\n*Disclaimer: the diagram numerical values are for illustration purposes only*\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197853%2F74819e0ff09d05a84e22d13b58cc88c5%2F1d-cnn-shape.png?generation=1751381120080184&alt=media)\n\nWhen determining a 1D convolution you are in charge of designing the `C_out` and `kernel_size`. This in turn will determine the `L_out` size by the formula:\n$$\nL_{out} = \\left\\lfloor \\frac{L_{in} + 2\\cdot \\text{pad} - \\text{dilation} \\cdot (\\text{kernel size} - 1) - 1}{\\text{stride}} + 1 \\right\\rfloor\n$$\n\nSo, essentially, I try to think about the kernel shape as having `C_out` number of kernels of shape `(C_in, kernel_size)` where each channel in the kernel \"takes care\" of each channel in the input and I need `C_out` number of these kernels because I want the output to have `C_out` channels.\n\nYou can check tensor shapes with this simple code:\n```python\ninput_tensor = torch.randn(32, 8, 100)\nB, C_in, L = input_tensor.shape\n\nC_out = 12\nkernel_size = 3\n\nconv1d = nn.Conv1d(\n    in_channels=C_in,\n    out_channels=C_out,\n    kernel_size=kernel_size,\n    padding=4,\n    stride=2,\n    dilation=2\n)\noutput_tensor = conv1d(input_tensor)\n\nprint(f\"Input tensor shape: {input_tensor.shape}\")\nprint(f\"Output tensor shape: {output_tensor.shape}\")\nprint(f\"Kernel shape: {conv1d.weight.shape}\")\n```\n\nAlso, I am leaving [this blog](https://jinglescode.github.io/2020/11/01/how-convolutional-layers-work-deep-learning-neural-networks/) which has some great animations.\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197853%2F283aac0afb7dd4067da393c16fe104b8%2F1d-cnn-animation-1.gif?generation=1751381663094261&alt=media)\n\nHappy Kaggling!",
    "3238144": "Let me add the link to my favorite explanation of convolutions: https://stackoverflow.com/a/44628011/5108365",
    "3238208": "Thanks for sharing"
  },
  "source": "meta"
}