Giter Site home page Giter Site logo

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. about sam HOT 3 CLOSED

davda54 avatar davda54 commented on August 21, 2024
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.

from sam.

Comments (3)

davda54 avatar davda54 commented on August 21, 2024 3

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

from sam.

XieZixiUSTC avatar XieZixiUSTC commented on August 21, 2024

@davda54 thanks,it work

from sam.

1215481871 avatar 1215481871 commented on August 21, 2024

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?

from sam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.