Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot compute gradients when STN is called multiple times #17

Open
emited opened this issue Jul 7, 2017 · 2 comments
Open

cannot compute gradients when STN is called multiple times #17

emited opened this issue Jul 7, 2017 · 2 comments

Comments

@emited
Copy link

emited commented Jul 7, 2017

Hello, I get the following error when the code below gets executed:

<ipython-input-4-7d15b88aed95> in <module>()
     19     loss += crit(input1, input1.detach()*0)
     20 
---> 21 loss.backward()

~/anaconda2/envs/python36/lib/python3.6/site-packages/torch/autograd/variable.py in backward(self, gradient, retain_variables)
    144                     'or with gradient w.r.t. the variable')
    145             gradient = self.data.new().resize_as_(self.data).fill_(1)
--> 146         self._execution_engine.run_backward((self,), (gradient,), retain_variables)
    147 
    148     def register_hook(self, hook):

RuntimeError: could not compute gradients for some functions

This happens whenever the STN module is called more then once per backward (the same bug seems to appear when using cuda). Does this code work for somebody else? Does somebody have an idea where it comes from?

import torch
import numpy as np
from modules.stn import STN
from modules.gridgen import AffineGridGenV2

s = STN()
g = AffineGridGenV2(64, 64)
crit = torch.nn.MSELoss()

inputImages = torch.rand(1, 64, 64, 3)
input = torch.autograd.Variable(torch.from_numpy(np.array([[[1, 0.5, 0], [0.5, 1, 0]]], dtype=np.float32)), requires_grad = True)
input1 = torch.autograd.Variable(inputImages)

loss = 0
for i in range(2):
    out = g(input)
    input1 = s(input1, out)
    loss += crit(input1, input1.detach()*0)
    
loss.backward()
@xuejingl
Copy link

I encounter similar problem. Do you solve it? @emited

@emited
Copy link
Author

emited commented Oct 28, 2017

I now use pytorch's official STN implementation!
https://github.com/pytorch/pytorch/blob/master/torch/nn/_functions/vision.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants