Register forward hook is not working

username: Ragnor
cooked: <pre><code>def extract_features(self, t_model, s_model, t_model_cfg, s_model_cfg):
    global t_FM
    t_FM = []
    global s_FM
    s_FM = []


    def hook1(module1, input, output):
        print("hooker Working")
        t_FM.append(output)

    handle = t_model.tbackbone.layer2[1].conv1.register_forward_hook(hook1)
    handle.remove()

    def hook2(module2, input, output):
        print("hooker Working")
        s_FM.append(output)

    handle = s_model.sbackbone.layer2[1].conv1.register_forward_hook(hook2)
    handle.remove()

    return t_FM, s_FM</code></pre>

username: sio277
cooked: <p>Remove the hook handle after fire it (after forwarding).</p>

username: Ragnor
cooked: <p>Hello <a class="mention" href="/u/sio277">@sio277</a> ,<br>
can you explain me more.<br>
thank you.</p>

username: sio277
cooked: <p>The registered hooks were removed before they are called. Try something like this:</p>
<pre><code class="lang-auto">    t_FM = []
    s_FM = []
    hooks = []

    def hook1(module1, input, output):
        print("hooker Working")
        t_FM.append(output)

    hooks.append(t_model.tbackbone.layer2[1].conv1.register_forward_hook(hook1))

    def hook2(module2, input, output):
        print("hooker Working")
        s_FM.append(output)

    hooks.append(s_model.sbackbone.layer2[1].conv1.register_forward_hook(hook2))

    your_model(input_tensors)  # forward with firing all the hooks registered
    for hook_handle in hooks:
        hook_handle.remove()
</code></pre>
<p>Also, you don’t need to set <code>t_FM</code> and <code>s_FM</code> as global (nonlocal) variables .</p>

username: sio277
cooked: <aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/ffdecc1ac48baf6aea3848e9f530256342a7046b/torch/nn/modules/module.py#L1009" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/ffdecc1ac48baf6aea3848e9f530256342a7046b/torch/nn/modules/module.py#L1009" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/ffdecc1ac48baf6aea3848e9f530256342a7046b/torch/nn/modules/module.py#L1009</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="999" style="counter-reset: li-counter 998 ;">
<li>            tracing_state.push_scope(name)</li>
<li>        else:</li>
<li>            recording_scopes = False</li>
<li>    try:</li>
<li>        result = self.forward(*input, **kwargs)</li>
<li>    finally:</li>
<li>        if recording_scopes:</li>
<li>            tracing_state.pop_scope()</li>
<li>    return result</li>
<li>
<li class="selected">def _call_impl(self, *input, **kwargs):</li>
<li>    forward_call = (self._slow_forward if torch._C._get_tracing_state() else self.forward)</li>
<li>    # If we don't have any hooks, we want to skip the rest of the logic in</li>
<li>    # this function, and just call forward.</li>
<li>    if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks</li>
<li>            or _global_forward_hooks or _global_forward_pre_hooks):</li>
<li>        return forward_call(*input, **kwargs)</li>
<li>    # Do not call functions when jit is used</li>
<li>    full_backward_hooks, non_full_backward_hooks = [], []</li>
<li>    if self._backward_hooks or _global_backward_hooks:</li>
<li>        full_backward_hooks, non_full_backward_hooks = self._get_backward_hooks()</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<p>
Check the source code of <code>nn.Module.__call__()</code>, where all the hooks are fired with <code>forward()</code>.</p>

username: Ragnor
cooked: <p>Thank you so much <a class="mention" href="/u/sio277">@sio277</a><br>
I global them because of i want to use them in another def</p>