Skip to content

Instantly share code, notes, and snippets.

@zou3519
Created November 20, 2017 21:17
Show Gist options
  • Save zou3519/18f4606971aa2267b455a93124f17370 to your computer and use it in GitHub Desktop.
Save zou3519/18f4606971aa2267b455a93124f17370 to your computer and use it in GitHub Desktop.
# add this to TestAutograd in test/test_autograd.py
def test_convbackwardbackward(self):
v_in = Variable(torch.Tensor([0.1, 0.1]).view(1, 2, 1, 1), requires_grad=True)
def forward(v_in):
f1 = lambda x: x * 0.0001
f2 = torch.nn.Conv2d(2, 1, 1, 1)
grad_out = Variable(torch.ones(1, 1, 1, 1))
gradient = torch.autograd.grad(outputs=f2(f1(v_in)), inputs=v_in,
grad_outputs=grad_out,
create_graph=True, retain_graph=True,
only_inputs=True)[0]
out = gradient.sum()
return out
self.assertTrue(gradcheck(forward, [v_in]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment