Sparse Convolution layer

dr.lauren: <p>Hi All,</p>
<p>Wanting to make a net along the lines of this paper: <a href="https://openreview.net/attachment?id=S1ln1TNKvH&amp;name=original_pdf" rel="nofollow noopener">A HARMONIC STRUCTURE-BASED NEURAL<br>
NETWORK MODEL FOR MUSICAL PITCH DETECTION</a> I needed a sparse convolutional layer. I couldn’t find an implementation of it, so I made it myself. However, I’m still pretty new at pytorch, and so I’m looking for wisdom on whether I’ve done it properly, and how it could be improved.</p>
<p>The motivation for the sparse kernel: convolution works great for image recognition in part because the identity of an object in an image comes from the relationship between adjacent pixels. Ordinary convolution learns about those relationships. However, with a musical sound on a spectrogram, the identity of a musical note comes from a fundamental frequency and its harmonics. So a new kind of convolution is needed that uses a non-contiguous set of pixels for the kernel, chosen so that they can learn about harmonically related frequencies.</p>
<p>I’ll paste the code for the sparse convolutional layer below, but heres a link to the repo: github / jseales/ sparse_kernel, which also contains a README describing all the data and processes that happen within it.</p>
<p>Also, here’s the code for the rest of the net, and for loading data, etc. github / jseales/ harmonic_net/ Currently it runs, but doesn’t seem to learn very well. I don’t currently know if that’s due to problems with the sparse kernel, or hyperparameters, or what!</p>
<p>Any perspective on the sparse convolution implementation or any other aspect of the product will be most welcome!</p>
<p>class SparseConv1D(nn.Module):</p>
<p>def <strong>init</strong>(self, sk_ind, in_channels=1, out_channels=1, device=‘cpu’):<br>
super(SparseConv1D, self).<strong>init</strong>()<br>
self.out_channels = out_channels<br>
self.in_channels = in_channels<br>
self.sk_ind = np.array(sk_ind, dtype=int)<br>
self.sk_len = len(sk_ind)<br>
self.sk_weights = torch.randn(out_channels, in_channels, self.sk_len,<br>
dtype=torch.float, requires_grad=True, device=device)<br>
self.device = device<br>
<span class="hashtag">#print</span>(‘self.sk_weights\n’, self.sk_weights)</p>
<p>def unfold_sparse_1D(self, input_tensor):<br>
# Find the amount of zero padding needed to make the output the same<br>
# size as the input.<br>
# print(‘input_tensor.shape’, input_tensor.shape)<br>
low_pad = int(max(0 - min(self.sk_ind), 0))<br>
high_pad = int(max(0, max(self.sk_ind)))<br>
input_array = input_tensor.cpu().detach().numpy()<br>
padded_array = np.hstack((input_array,<br>
np.zeros((self.in_channels, high_pad)),<br>
np.zeros((self.in_channels, low_pad))))<br>
# print(‘padded array\n’, padded_array)</p>
<pre><code># Construct an array of indices that will be used to make the 
# unfolded array via numpy fancy indexing. 
# Broadcast to make an array of shape(sk_len, input_len)
indices = self.sk_ind[:, np.newaxis] + np.arange(self.input_len)
# print('indices\n', indices)
# output of array has shape(in_channels, sk_len, input_len)
return torch.tensor(padded_array[np.arange(self.in_channels)[:, np.newaxis, np.newaxis], 
                                 indices[np.newaxis, :, :]], 
                                 dtype=torch.float, device=self.device)
</code></pre>
<p>def forward(self, input_tensor):<br>
batch_size = input_tensor.shape[0]<br>
self.input_len = input_tensor.shape[2]<br>
output_batch = torch.empty(batch_size, self.out_channels, self.input_len,<br>
dtype=torch.float, device=self.device)</p>
<pre><code>for i in range(batch_size):
# Input_array will come in shape (in_channels, input_len)
  unfolded = self.unfold_sparse_1D(input_tensor[i])
  # print('unfolded\n', unfolded)
  #print(self.sk_weights)
  output_batch[i] = torch.mm(self.sk_weights.reshape(self.out_channels, self.in_channels * self.sk_len), 
                  unfolded.reshape(self.in_channels * self.sk_len, self.input_len))
return output_batch</code></pre>