Giter Club home page Giter Club logo

Comments (3)

tshead2 avatar tshead2 commented on September 16, 2024

I've noticed this, too.

from lucent.

TomasWilson avatar TomasWilson commented on September 16, 2024

This issue stems from the fact that the registered forward_hooks (they are used to grab feature maps from the hidden layers) are not properly cleaned up before render_vis returns.

This is quite a serious bug, although it is not harmful to the correctness of the visualizations. After many iterations, the sub modules accumulate hundreds of these hooks, that get called after every forward() call on the model. This even impacts any other code that uses the model after calling render_vis.

A quick and dirty fix is to clear all forward_hooks from the model, after each call to render_vis, like so:

from collections import OrderedDict
def remove_all_forward_hooks(model):
    for _, child in model._modules.items():
        if child is not None:
            if hasattr(child, "_forward_hooks"):
                child._forward_hooks = OrderedDict()
            remove_all_forward_hooks(child)

# call this after render_vis
remove_all_forward_hooks(your_torch_model)

(I took this code from here)

Of course, a proper fix to this should be patched directly into the library, cleaning up the model by removing only the hooks actually created by the render_vis function (the above code removes ALL hooks, even those unrelated to Lucent / render_vis).

from lucent.

greentfrapp avatar greentfrapp commented on September 16, 2024

Thanks @antoninogreco and @tshead2 for reporting this! And thanks to @TomasWilson for the suggested fix!
This should be fixed with #52 where the created module hooks are cleared after each render_vis run.

from lucent.

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.