• 教程 >
  • 使用 numpy 和 scipy 创建扩展
Shortcuts

Creating Extensions Using numpy and scipy

Author: Adam Paszke

Updated by: Adam Dziedzic

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 its implementation

  2. Create a neural network layer that has learnable weights

    • This calls into SciPy as part of its implementation

import torch
from torch.autograd import Function

Parameter-less example

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

It is aptly named BadFFTFunction

Layer Implementation

from numpy.fft import rfft2, irfft2


class BadFFTFunction(Function):
    @staticmethod
    def forward(ctx, input):
        numpy_input = input.detach().numpy()
        result = abs(rfft2(numpy_input))
        return input.new(result)

    @staticmethod
    def backward(ctx, grad_output):
        numpy_go = grad_output.numpy()
        result = irfft2(numpy_go)
        return grad_output.new(result)

# 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):
    return BadFFTFunction.apply(input)

Example usage of the created layer:

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

输出:

tensor([[ 3.3248,  8.6934, 10.1613,  2.2307, 12.3608],
        [ 6.8796,  5.2975,  4.9111,  9.5349,  7.9492],
        [ 1.7146, 17.0204,  8.5861,  2.9516,  4.4361],
        [ 2.3543,  9.4432,  5.0801,  7.9173,  3.4937],
        [ 6.1790,  2.9700,  5.5966,  7.9316,  1.0364],
        [ 2.3543,  9.1855,  5.1234,  0.1324,  3.4937],
        [ 1.7146,  2.5182,  1.5804,  3.7015,  4.4361],
        [ 6.8796,  7.2606,  9.7128,  7.6067,  7.9492]],
       grad_fn=<BadFFTFunctionBackward>)
tensor([[ 1.8589,  0.8455,  0.5278, -1.9478, -1.0154, -0.7578,  0.1231,  0.8001],
        [ 1.6320,  0.1240, -0.8338,  0.1578,  0.4579,  1.0815,  0.8947, -0.2489],
        [-0.4466, -0.2201,  0.0795, -0.3085,  0.0553,  1.2725, -0.4200,  0.3399],
        [ 0.2328,  1.9768,  0.5205, -0.3027,  0.0961, -0.7689, -0.0494,  0.1171],
        [-0.5295,  1.3302,  1.8524, -1.9706,  0.1228, -0.1062, -0.1439, -0.9080],
        [-0.4090,  0.8340,  0.5221, -1.3914, -0.2900, -0.4998,  0.2201, -0.1956],
        [ 0.1647, -0.2741, -0.0418, -0.4245,  0.7484, -0.2011, -0.2998, -1.5324],
        [ 0.4673, -1.0877,  1.4180,  1.7547,  0.1727, -0.7631,  0.1552, -1.2429]],
       requires_grad=True)

Parametrized example

In deep learning literature, this layer is confusingly referred to as convolution while the actual operation is cross-correlation (the only difference is that filter is flipped for convolution, which is not the case for cross-correlation).

Implementation of a layer with learnable weights, where cross-correlation has a filter (kernel) that represents weights.

The backward pass computes the gradient wrt the input and the gradient wrt the filter.

from numpy import flip
import numpy as np
from scipy.signal import convolve2d, correlate2d
from torch.nn.modules.module import Module
from torch.nn.parameter import Parameter


class ScipyConv2dFunction(Function):
    @staticmethod
    def forward(ctx, input, filter, bias):
        # detach so we can cast to NumPy
        input, filter, bias = input.detach(), filter.detach(), bias.detach()
        result = correlate2d(input.numpy(), filter.numpy(), mode='valid')
        result += bias.numpy()
        ctx.save_for_backward(input, filter, bias)
        return torch.as_tensor(result, dtype=input.dtype)

    @staticmethod
    def backward(ctx, grad_output):
        grad_output = grad_output.detach()
        input, filter, bias = ctx.saved_tensors
        grad_output = grad_output.numpy()
        grad_bias = np.sum(grad_output, keepdims=True)
        grad_input = convolve2d(grad_output, filter.numpy(), mode='full')
        # the previous line can be expressed equivalently as:
        # grad_input = correlate2d(grad_output, flip(flip(filter.numpy(), axis=0), axis=1), mode='full')
        grad_filter = correlate2d(input.numpy(), grad_output, mode='valid')
        return torch.from_numpy(grad_input), torch.from_numpy(grad_filter).to(torch.float), torch.from_numpy(grad_bias).to(torch.float)


class ScipyConv2d(Module):
    def __init__(self, filter_width, filter_height):
        super(ScipyConv2d, self).__init__()
        self.filter = Parameter(torch.randn(filter_width, filter_height))
        self.bias = Parameter(torch.randn(1, 1))

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

Example usage:

module = ScipyConv2d(3, 3)
print("Filter and bias: ", list(module.parameters()))
input = torch.randn(10, 10, requires_grad=True)
output = module(input)
print("Output from the convolution: ", output)
output.backward(torch.randn(8, 8))
print("Gradient for the input map: ", input.grad)

输出:

Filter and bias:  [Parameter containing:
tensor([[-0.0127,  0.6094,  0.7437],
        [ 0.0034, -2.0211,  1.2026],
        [ 0.2839, -1.2465,  2.4501]], requires_grad=True), Parameter containing:
tensor([[0.1754]], requires_grad=True)]
Output from the convolution:  tensor([[ 3.7715, -3.3933,  3.7742, -0.4405,  1.8428, -2.6763,  7.1234,  1.7851],
        [ 3.9066,  2.4116,  1.2015, -2.7389, -3.4771, -6.0740,  6.7892,  0.3956],
        [ 0.9773,  3.6122, -4.7350,  1.8631,  5.8414, -2.5559,  6.2548,  3.2117],
        [ 3.3199,  0.8009, -2.2397,  0.3575, -1.4996, -5.4387,  1.0262,  6.6938],
        [ 1.1739,  2.4859,  2.9899, -3.7422, -0.7200, -1.0648,  6.6347, -0.1760],
        [-1.0964,  4.4039,  3.6015, -4.9088, -2.3931,  2.6643,  4.6775, -2.2962],
        [-3.8718,  0.4117,  3.6370,  0.2743, -0.3120, -3.2564, -1.9917,  4.5054],
        [-0.8772, -1.5775,  2.9736,  1.4305, -1.9539,  3.0286, -0.2207,  6.7862]],
       grad_fn=<ScipyConv2dFunctionBackward>)
Gradient for the input map:  tensor([[ 1.0798e-02, -5.1339e-01, -9.5126e-01,  2.3995e-01,  1.7568e+00,
          1.4583e+00, -1.0408e+00, -2.7085e+00, -1.1490e+00,  2.0302e-01],
        [ 1.2722e-02,  9.8464e-01, -1.6087e+00, -3.3372e+00, -2.4636e+00,
          7.5287e-01,  5.3042e+00,  3.4130e-01, -3.9520e+00, -1.6781e-01],
        [-2.4795e-01,  3.4859e+00, -7.0468e-02, -3.7899e+00,  1.7569e+00,
         -4.6621e-01,  3.0854e+00, -4.0084e-01, -4.3139e+00, -4.4742e-01],
        [-3.5165e-01,  1.0503e+00, -1.7441e+00, -1.6710e+00,  4.4271e+00,
         -5.8303e+00,  1.3973e+00, -1.7359e+00, -2.1967e-01, -1.4826e+00],
        [ 3.5946e-02, -2.7309e-01, -7.6754e-01,  1.0063e+00,  3.8445e+00,
         -3.1658e+00,  2.3488e+00, -1.2406e+00, -3.5167e-04,  1.0574e-01],
        [ 6.4901e-02, -1.2321e+00, -1.3731e+00,  3.4139e+00, -3.7268e+00,
         -3.1196e+00,  1.8897e+00, -5.2468e-02, -2.6932e+00,  2.8736e+00],
        [ 6.6747e-02,  1.8437e+00, -3.6303e+00,  4.4750e+00, -5.6375e+00,
         -4.8604e-01,  5.1752e+00, -9.0405e-01, -1.1082e+00,  8.6575e-01],
        [-3.3658e-01,  2.7781e+00, -5.6290e+00,  4.8723e+00,  1.5226e+00,
         -7.6519e+00,  2.2954e+00, -5.7324e-01,  1.1070e+00,  9.9571e-01],
        [-2.1812e-01,  2.5107e+00, -3.7996e+00,  2.3511e+00,  1.2881e+00,
         -8.1735e+00,  2.7228e+00, -1.0100e+00,  3.1309e+00, -1.7033e+00],
        [-2.1412e-01,  1.0145e+00, -2.2977e+00,  9.5607e-01,  1.5400e-02,
         -2.3620e+00,  1.0516e+00, -1.2254e+00,  2.2289e+00, -1.3369e+00]])

Check the gradients:

from torch.autograd.gradcheck import gradcheck

moduleConv = ScipyConv2d(3, 3)

input = [torch.randn(20, 20, dtype=torch.double, requires_grad=True)]
test = gradcheck(moduleConv, input, eps=1e-6, atol=1e-4)
print("Are the gradients correct: ", test)

输出:

Are the gradients correct:  True

脚本总运行时间: ( 0 minutes 0.491 seconds)

Gallery generated by Sphinx-Gallery