MCPcopy
hub / github.com/ddbourgin/numpy-ml / calc_pad_dims_1D

Function calc_pad_dims_1D

numpy_ml/neural_nets/utils/utils.py:123–192  ·  view source on GitHub ↗

Compute the padding necessary to ensure that convolving `X` with a 1D kernel of shape `kernel_shape` and stride `stride` produces outputs with length `l_out`. Parameters ---------- X_shape : tuple of `(n_ex, l_in, in_ch)` Dimensions of the input volume. Padding is a

(X_shape, l_out, kernel_width, stride, dilation=0, causal=False)

Source from the content-addressed store, hash-verified

121
122
123def calc_pad_dims_1D(X_shape, l_out, kernel_width, stride, dilation=0, causal=False):
124 """
125 Compute the padding necessary to ensure that convolving `X` with a 1D kernel
126 of shape `kernel_shape` and stride `stride` produces outputs with length
127 `l_out`.
128
129 Parameters
130 ----------
131 X_shape : tuple of `(n_ex, l_in, in_ch)`
132 Dimensions of the input volume. Padding is applied on either side of
133 `l_in`.
134 l_out : int
135 The desired length an output example after applying the convolution.
136 kernel_width : int
137 The width of the 1D convolution kernel.
138 stride : int
139 The stride for the convolution kernel.
140 dilation : int
141 Number of pixels inserted between kernel elements. Default is 0.
142 causal : bool
143 Whether to compute the padding dims for a regular or causal
144 convolution. If causal, padding is added only to the left side of the
145 sequence. Default is False.
146
147 Returns
148 -------
149 padding_dims : 2-tuple
150 Padding dims for X. Organized as (left, right)
151 """
152 if not isinstance(X_shape, tuple):
153 raise ValueError("`X_shape` must be of type tuple")
154
155 if not isinstance(l_out, int):
156 raise ValueError("`l_out` must be of type int")
157
158 if not isinstance(kernel_width, int):
159 raise ValueError("`kernel_width` must be of type int")
160
161 if not isinstance(stride, int):
162 raise ValueError("`stride` must be of type int")
163
164 d = dilation
165 fw = kernel_width
166 n_ex, l_in, in_ch = X_shape
167
168 # update effective filter shape based on dilation factor
169 _fw = fw * (d + 1) - d
170 total_pad = int((stride * (l_out - 1) + _fw - l_in))
171
172 if not causal:
173 pw = total_pad // 2
174 l_out1 = int(1 + (l_in + 2 * pw - _fw) / stride)
175
176 # add asymmetric padding pixels to right / bottom
177 pw1, pw2 = pw, pw
178 if l_out1 == l_out - 1:
179 pw1, pw2 = pw, pw + 1
180 elif l_out1 != l_out:

Callers 1

pad1DFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected