Skip to content

Instantly share code, notes, and snippets.

@dotchen
dotchen / receptive_field.py
Last active October 13, 2018 21:03
Calculating size of receptive field numerically using the NaN trick
import torch
from torch import nn
import numpy as np
def receptive_field(m, input_size=(1,3,64,64)):
i = torch.rand(input_size, requires_grad=True)
o = m(i)
o_grad = torch.zeros(o.size())
o_grad[:,:,int(o.size()[2]/2),int(o.size()[3]/2)] = torch.tensor(float('nan'))
o.backward(gradient=o_grad)