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

Function conv1D

numpy_ml/neural_nets/utils/utils.py:668–717  ·  view source on GitHub ↗

A faster (but more memory intensive) implementation of a 1D "convolution" (technically, cross-correlation) of input `X` with a collection of kernels in `W`. Notes ----- Relies on the :func:`im2col` function to perform the convolution as a single matrix multiplication.

(X, W, stride, pad, dilation=0)

Source from the content-addressed store, hash-verified

666
667
668def conv1D(X, W, stride, pad, dilation=0):
669 """
670 A faster (but more memory intensive) implementation of a 1D "convolution"
671 (technically, cross-correlation) of input `X` with a collection of kernels in
672 `W`.
673
674 Notes
675 -----
676 Relies on the :func:`im2col` function to perform the convolution as a single
677 matrix multiplication.
678
679 For a helpful diagram, see Pete Warden's 2015 blogpost [1].
680
681 References
682 ----------
683 .. [1] Warden (2015). "Why GEMM is at the heart of deep learning,"
684 https://petewarden.com/2015/04/20/why-gemm-is-at-the-heart-of-deep-learning/
685
686 Parameters
687 ----------
688 X : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, l_in, in_ch)`
689 Input volume (unpadded)
690 W: :py:class:`ndarray <numpy.ndarray>` of shape `(kernel_width, in_ch, out_ch)`
691 A volume of convolution weights/kernels for a given layer
692 stride : int
693 The stride of each convolution kernel
694 pad : tuple, int, or 'same'
695 The padding amount. If 'same', add padding to ensure that the output of
696 a 1D convolution with a kernel of `kernel_shape` and stride `stride`
697 produces an output volume of the same dimensions as the input. If
698 2-tuple, specifies the number of padding colums to add *on both sides*
699 of the columns in X.
700 dilation : int
701 Number of pixels inserted between kernel elements. Default is 0.
702
703 Returns
704 -------
705 Z : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, l_out, out_ch)`
706 The convolution of X with W.
707 """
708 _, p = pad1D(X, pad, W.shape[0], stride, dilation=dilation)
709
710 # add a row dimension to X to permit us to use im2col/col2im
711 X2D = np.expand_dims(X, axis=1)
712 W2D = np.expand_dims(W, axis=0)
713 p2D = (0, 0, p[0], p[1])
714 Z2D = conv2D(X2D, W2D, stride, p2D, dilation)
715
716 # drop the row dimension
717 return np.squeeze(Z2D, axis=1)
718
719
720def deconv2D_naive(X, W, stride, pad, dilation=0):

Callers 1

forwardMethod · 0.85

Calls 2

pad1DFunction · 0.85
conv2DFunction · 0.85

Tested by

no test coverage detected