Zero-pad a 3D input volume `X` along the second dimension. Parameters ---------- X : :py:class:`ndarray ` of shape `(n_ex, l_in, in_ch)` Input volume. Padding is applied to `l_in`. pad : tuple, int, or {'same', 'causal'} The padding amount. If 'sa
(X, pad, kernel_width=None, stride=None, dilation=0)
| 193 | |
| 194 | |
| 195 | def pad1D(X, pad, kernel_width=None, stride=None, dilation=0): |
| 196 | """ |
| 197 | Zero-pad a 3D input volume `X` along the second dimension. |
| 198 | |
| 199 | Parameters |
| 200 | ---------- |
| 201 | X : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, l_in, in_ch)` |
| 202 | Input volume. Padding is applied to `l_in`. |
| 203 | pad : tuple, int, or {'same', 'causal'} |
| 204 | The padding amount. If 'same', add padding to ensure that the output |
| 205 | length of a 1D convolution with a kernel of `kernel_shape` and stride |
| 206 | `stride` is the same as the input length. If 'causal' compute padding |
| 207 | such that the output both has the same length as the input AND |
| 208 | ``output[t]`` does not depend on ``input[t + 1:]``. If 2-tuple, |
| 209 | specifies the number of padding columns to add on each side of the |
| 210 | sequence. |
| 211 | kernel_width : int |
| 212 | The dimension of the 2D convolution kernel. Only relevant if p='same' |
| 213 | or 'causal'. Default is None. |
| 214 | stride : int |
| 215 | The stride for the convolution kernel. Only relevant if p='same' or |
| 216 | 'causal'. Default is None. |
| 217 | dilation : int |
| 218 | The dilation of the convolution kernel. Only relevant if p='same' or |
| 219 | 'causal'. Default is None. |
| 220 | |
| 221 | Returns |
| 222 | ------- |
| 223 | X_pad : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, padded_seq, in_channels)` |
| 224 | The padded output volume |
| 225 | p : 2-tuple |
| 226 | The number of 0-padded columns added to the (left, right) of the sequences |
| 227 | in `X`. |
| 228 | """ |
| 229 | p = pad |
| 230 | if isinstance(p, int): |
| 231 | p = (p, p) |
| 232 | |
| 233 | if isinstance(p, tuple): |
| 234 | X_pad = np.pad( |
| 235 | X, |
| 236 | pad_width=((0, 0), (p[0], p[1]), (0, 0)), |
| 237 | mode="constant", |
| 238 | constant_values=0, |
| 239 | ) |
| 240 | |
| 241 | # compute the correct padding dims for a 'same' or 'causal' convolution |
| 242 | if p in ["same", "causal"] and kernel_width and stride: |
| 243 | causal = p == "causal" |
| 244 | p = calc_pad_dims_1D( |
| 245 | X.shape, X.shape[1], kernel_width, stride, causal=causal, dilation=dilation |
| 246 | ) |
| 247 | X_pad, p = pad1D(X, p) |
| 248 | |
| 249 | return X_pad, p |
| 250 | |
| 251 | |
| 252 | def pad2D(X, pad, kernel_shape=None, stride=None, dilation=0): |