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

RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time. #45

Closed
XieZixiUSTC opened this issue Sep 10, 2021 · 3 comments

Comments

@XieZixiUSTC
Copy link

i encounter this problem,please help me ,thanks a lot

the code is :

compute output

    output = model(images)
    # loss = criterion(output, target)
    # SAM
    # first forward-backward pass
    loss = criterion(output, target)  # use this loss for any training statistics
    loss.backward()
    optimizer.first_step(zero_grad=True)

    # second forward-backward pass
    criterion(output, target).backward()  # make sure to do a full forward pass
    optimizer.second_step(zero_grad=True)

    # measure accuracy and record loss
    acc1, acc5 = accuracy(output, target, topk=(1, 5))
    losses.update(loss.item(), images.size(0))
    top1.update(acc1[0], images.size(0))
    top5.update(acc5[0], images.size(0))

the problen is :
Traceback (most recent call last):
File "C:/softWare/SAM/main.py", line 557, in
main()
File "C:/softWare/SAM/main.py", line 156, in main
main_worker(args.gpu, ngpus_per_node, args)
File "C:/softWare/SAM/main.py", line 340, in main_worker
acc2, loss2=train(train_loader, model, criterion, optimizer, epoch, args, f)
File "C:/softWare/SAM/main.py", line 413, in train
criterion(output, target).backward() # make sure to do a full forward pass
File "C:\softWare\Python37\lib\site-packages\torch\tensor.py", line 221, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "C:\softWare\Python37\lib\site-packages\torch\autograd_init_.py", line 132, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.

@davda54
Copy link
Owner

davda54 commented Sep 10, 2021

Hi, you should call the forward pass output = model(images) again before criterion(output, target).backward()

@XieZixiUSTC
Copy link
Author

@davda54 thanks,it work

@davda54 davda54 closed this as completed Sep 15, 2021
@1215481871
Copy link

Sorry, I got the similar question. I actually did the full second forward pass but also reported "Trying to backward through the graph a second time (or directly access saved variables after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved variables after calling backward."

Here is my code:
output = backbone(imgs)
loss = self.criterion(output,labs)
backbone_opt.zero_grad()
loss.backward()
backbone_opt.first_step(zero_grad=True)

        output2 = backbone(imgs)
        loss2 = self.criterion(output2,labs)
        loss2.backward()
        backbone_opt.second_step(zero_grad=True)

But if I substitute "output2 = backbone(imgs)" with "output2 = backbone(imgs.detach())", it works.
Does this implementation make sense or I also made some mistakes here?

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

3 participants