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

Code broken in backward #1

Open
FrancescoMandru opened this issue May 14, 2021 · 4 comments
Open

Code broken in backward #1

FrancescoMandru opened this issue May 14, 2021 · 4 comments

Comments

@FrancescoMandru
Copy link

/home/francesco/Desktop/test/VAE-GAN/network.py:187: UserWarning: nn.init.uniform is now deprecated in favor of nn.init.uniform_.
  nn.init.uniform(m.weight,-scale,scale)
/home/francesco/Desktop/test/VAE-GAN/network.py:189: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_.
  nn.init.constant(m.bias, 0.0)
Epoch:0
/home/francesco/Desktop/test/test/lib/python3.8/site-packages/torch/nn/functional.py:1709: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.
  warnings.warn("nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.")
[W python_anomaly_mode.cpp:104] Warning: Error detected in AddmmBackward. Traceback of forward call that caused the error:
  File "main.py", line 97, in <module>
    x_tilde, disc_class, disc_layer, mus, log_variances = net(x)
  File "/home/francesco/Desktop/test/VAE-GAN/network.py", line 224, in __call__
    return super(VaeGan, self).__call__(*args, **kwargs)
  File "/home/francesco/Desktop/test/test/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/francesco/Desktop/test/VAE-GAN/network.py", line 199, in forward
    mus, log_variances = self.encoder(x)
  File "/home/francesco/Desktop/test/VAE-GAN/network.py", line 79, in __call__
    return super(Encoder, self).__call__(*args, **kwargs)
  File "/home/francesco/Desktop/test/test/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/francesco/Desktop/test/VAE-GAN/network.py", line 75, in forward
    logvar = self.l_var(ten)
  File "/home/francesco/Desktop/test/test/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/francesco/Desktop/test/test/lib/python3.8/site-packages/torch/nn/modules/linear.py", line 94, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/francesco/Desktop/test/test/lib/python3.8/site-packages/torch/nn/functional.py", line 1753, in linear
    return torch._C._nn.linear(input, weight, bias)
 (function _print_stack)
Traceback (most recent call last):
  File "main.py", line 140, in <module>
    loss_decoder.backward(retain_graph=True)  #[p.grad.data.clamp_(-1,1) for p in net.decoder.parameters()]
  File "/home/francesco/Desktop/test/test/lib/python3.8/site-packages/torch/tensor.py", line 245, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  File "/home/francesco/Desktop/test/test/lib/python3.8/site-packages/torch/autograd/__init__.py", line 145, in backward
    Variable._execution_engine.run_backward(
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1024, 128]], which is output 0 of TBackward, is at version 3; expected version 2 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!
@ry85
Copy link
Owner

ry85 commented May 26, 2021

Hi,

The code should work fine. There might some difference with respect to the python version or similar to that. The error you are getting is not an uncommon error, did you tried to google it. I think modifying the version should work.

@ILYSHI
Copy link

ILYSHI commented Jun 13, 2021

It's rather common error, i found it in several VAE-GAN models.

@GloryyrolG
Copy link

Hi,

The code should work fine. There might some difference with respect to the python version or similar to that. The error you are getting is not an uncommon error, did you tried to google it. I think modifying the version should work.

Could you please provide your environment, i.e., the requirements of this repo code?

@wqzdhrdhr
Copy link

wqzdhrdhr commented Jul 13, 2021

Hi,

The code should work fine. There might some difference with respect to the python version or similar to that. The error you are getting is not an uncommon error, did you tried to google it. I think modifying the version should work.

I encountered the same problem, my environment is pytorch1.7+python3.9. I guess this is because in the newer version of pytorch, .step() directly changes the parameters in the graph, instead of creating a clone like in the old version. So I tried to put all optimizer.step() after all loss.backward() was completed.

        `net.zero_grad()
        # encoder
        loss_encoder.backward(retain_graph=True)  #someone likes to clamp the grad here: [p.grad.data.clamp_(-1,1) for p in net.encoder.parameters()]
        net.decoder.zero_grad()
        net.discriminator.zero_grad()

        if (train_dec and train_dis == True):
            loss_decoder.backward(retain_graph=True)
            net.discriminator.zero_grad()
            loss_discriminator.backward()
            optimizer_encoder.step()
            optimizer_decoder.step()
            optimizer_discriminator.step()
        #decoder
        if (train_dec == True and train_dis == False):
            loss_decoder.backward()  #[p.grad.data.clamp_(-1,1) for p in net.decoder.parameters()]
            optimizer_decoder.step()
            optimizer_encoder.step()
            optimizer_discriminator.step()


        #discriminator
        if (train_dec == False and train_dis == True):
            loss_discriminator.backward() 
            optimizer_encoder.step()
            optimizer_decoder.step()
            optimizer_discriminator.step()

        if (train_dec == False and train_dis == False):
            optimizer_encoder.step()`

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

5 participants