1D convolution operator in NCW layout Parameters ---------- a_np : numpy.ndarray 3-D with shape [batch, in_channel, in_width] w_np : numpy.ndarray 3-D with shape [num_filter, in_channel, filter_width] stride : int Stride size padding : int, tuple,
(a_np, w_np, stride, padding, dilation)
| 57 | |
| 58 | |
| 59 | def conv1d_ncw_python(a_np, w_np, stride, padding, dilation): |
| 60 | """1D convolution operator in NCW layout |
| 61 | |
| 62 | Parameters |
| 63 | ---------- |
| 64 | a_np : numpy.ndarray |
| 65 | 3-D with shape [batch, in_channel, in_width] |
| 66 | |
| 67 | w_np : numpy.ndarray |
| 68 | 3-D with shape [num_filter, in_channel, filter_width] |
| 69 | |
| 70 | stride : int |
| 71 | Stride size |
| 72 | |
| 73 | padding : int, tuple, or str |
| 74 | Single int for padding size or tuple of (left, right) padding |
| 75 | or a string in ['VALID', 'SAME'] |
| 76 | |
| 77 | dilation : int |
| 78 | Dilation rate of the kernel |
| 79 | |
| 80 | groups : int |
| 81 | Number of groups in the convolution |
| 82 | |
| 83 | Returns |
| 84 | ------- |
| 85 | b_np : numpy.ndarray |
| 86 | 3-D with shape [batch, out_channel, out_width] |
| 87 | """ |
| 88 | batch, in_c, in_w = a_np.shape |
| 89 | out_c, _, filter_w = w_np.shape |
| 90 | if isinstance(stride, tuple | list): |
| 91 | stride = stride[0] |
| 92 | if isinstance(dilation, tuple | list): |
| 93 | dilation = dilation[0] |
| 94 | |
| 95 | dilated_filter_w = (filter_w - 1) * dilation + 1 |
| 96 | pad_left, pad_right = get_pad_tuple1d(padding, (dilated_filter_w,)) |
| 97 | out_w = ((in_w - dilated_filter_w + pad_left + pad_right) // stride) + 1 |
| 98 | |
| 99 | padded_a_np = np.zeros((batch, in_c, in_w + pad_left + pad_right)) |
| 100 | padded_a_np[:, :, pad_left : (in_w + pad_left)] = a_np |
| 101 | |
| 102 | b_np = np.zeros((batch, out_c, out_w)) |
| 103 | for n in range(batch): |
| 104 | for f in range(out_c): |
| 105 | for c in range(in_c): |
| 106 | out = np.convolve( |
| 107 | padded_a_np[n, c], np.flip(dilate_np(w_np[f, c], dilation)), mode="valid" |
| 108 | ) |
| 109 | b_np[n, f] += out[::stride] |
| 110 | return b_np |
no test coverage detected
searching dependent graphs…