Skip to content

Instantly share code, notes, and snippets.

@haoxiangsnr
Created April 29, 2019 07:05
Show Gist options
  • Select an option

  • Save haoxiangsnr/fd08f828c0964da7b8b2453c724c2b15 to your computer and use it in GitHub Desktop.

Select an option

Save haoxiangsnr/fd08f828c0964da7b8b2453c724c2b15 to your computer and use it in GitHub Desktop.

Revisions

  1. haoxiangsnr created this gist Apr 29, 2019.
    129 changes: 129 additions & 0 deletions conv_caculator.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,129 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {},
    "outputs": [],
    "source": [
    "import math"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 10,
    "metadata": {},
    "outputs": [],
    "source": [
    "l_in = 16384\n",
    "padding = 7\n",
    "dilation = 1\n",
    "kernel_size = 15\n",
    "stride = 1\n",
    "l_out = 16384"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 17,
    "metadata": {},
    "outputs": [],
    "source": [
    "def calulate_l_out(l_in, kernel_size, stride, dilation=1, padding=0):\n",
    " # https://pytorch.org/docs/stable/nn.html#conv1d\n",
    " return math.floor(((l_in + 2 * padding - dilation * (kernel_size - 1) - 1) / stride) + 1)\n",
    "\n",
    "def calulate_same_padding(l_in, kernel_size, stride, dilation=1):\n",
    " # https://pytorch.org/docs/stable/nn.html#conv1d\n",
    " return math.ceil(((l_in - 1) * stride + 1 + dilation * (kernel_size - 1) - l_in) / 2)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 18,
    "metadata": {},
    "outputs": [],
    "source": [
    "l_in = 16384\n",
    "kernel_size = 15\n",
    "stride = 1\n",
    "dilation = 1\n",
    "padding = 7\n",
    "l_out = 16384"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 23,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "16384"
    ]
    },
    "execution_count": 23,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "calulate_l_out(\n",
    " l_in=16384,\n",
    " kernel_size = 15,\n",
    " stride=1,\n",
    " dilation=1,\n",
    " padding=7,\n",
    ")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 24,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "7"
    ]
    },
    "execution_count": 24,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "# Calulate same padding\n",
    "calulate_same_padding(\n",
    " l_in=16384, \n",
    " kernel_size=15,\n",
    " stride=1,\n",
    " dilation = 1\n",
    ")"
    ]
    }
    ],
    "metadata": {
    "kernelspec": {
    "display_name": "Python 3",
    "language": "python",
    "name": "python3"
    },
    "language_info": {
    "codemirror_mode": {
    "name": "ipython",
    "version": 3
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
    "version": "3.6.5"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 2
    }