Coder Social home page Coder Social logo

Comments (2)

filipradenovic avatar filipradenovic commented on May 23, 2024
  1. As far as I know it sums all the gradients, take a look at this discussion for more insights about PyTorch and gradients. A really important thing is to do optimizer.zero_grad() before the loop in which you do loss.backward(), because the gradients need to be cleared before accumulating them for a new batch.
  2. First of all, both implementations would yield the same result. But yes, there is a reason why we do it the way we do. It is a trade-off between performance and memory usage. As it is implemented right now, it is the most memory efficient (so that you can do training with larger images as well, if necessary). Doing only one loss.backward() per optimizer.step() would be the most memory hungry, but also the fastest. Code can be simply adjusted if you want to do that. You can also implement it to split each batch into smaller sub-batches, so that they nearly fill up the memory.
  3. During validation there is no backward pass or gradient computation, it is a simple evaluation of the network so it does not require a lot of memory. Because of that, we do not need to split the loss computations, we can simply compute it for the whole batch.

Hope this helps!

from cnnimageretrieval-pytorch.

itzsid avatar itzsid commented on May 23, 2024

Thanks for the quick replies @filipradenovic. This is helpful.

from cnnimageretrieval-pytorch.

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.