# Creating extensions using numpy and scipy¶

In this tutorial, we shall go through two tasks:

1. Create a neural network layer with no parameters.

• This calls into numpy as part of it’s implementation
2. Create a neural network layer that has learnable weights

• This calls into SciPy as part of it’s implementation
import torch


## Parameter-less example¶

This layer doesn’t particularly do anything useful or mathematically correct.

Layer Implementation

from numpy.fft import rfft2, irfft2

def forward(self, input):
numpy_input = input.numpy()
result = abs(rfft2(numpy_input))

result = irfft2(numpy_go)

# since this layer does not have any parameters, we can
# simply declare this as a function, rather than as an nn.Module class

def incorrect_fft(input):


Example usage of the created layer:

input = Variable(torch.randn(8, 8), requires_grad=True)
result = incorrect_fft(input)
print(result.data)
result.backward(torch.randn(result.size()))


## Parametrized example¶

This implements a layer with learnable weights.

It implements the Cross-correlation with a learnable kernel.

In deep learning literature, it’s confusingly referred to as Convolution.

The backward computes the gradients wrt the input and gradients wrt the filter.

Implementation:

Please Note that the implementation serves as an illustration, and we did not verify it’s correctness

from scipy.signal import convolve2d, correlate2d
from torch.nn.modules.module import Module
from torch.nn.parameter import Parameter

class ScipyConv2dFunction(Function):

def forward(self, input, filter):
result = correlate2d(input.numpy(), filter.numpy(), mode='valid')
self.save_for_backward(input, filter)

input, filter = self.saved_tensors

class ScipyConv2d(Module):

def __init__(self, kh, kw):
super(ScipyConv2d, self).__init__()
self.filter = Parameter(torch.randn(kh, kw))

def forward(self, input):
return ScipyConv2dFunction()(input, self.filter)


Example usage:

module = ScipyConv2d(3, 3)
print(list(module.parameters()))