Why are the grads None?

username: otatopeht
cooked: <p>Hello everyone,<br>
I’m trying to finetune a pretrained sentence transformer on indomaine data with Pytorch.<br>
The loss is being calculated but the grads are None.</p>
<p>The model looks like this:</p>
<pre><code class="lang-auto">SentenceTransformer(
  (0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: XLMRobertaModel 
  (1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
)
</code></pre>
<p>And when trying to use the forward hooks as follow:</p>
<pre><code class="lang-auto">activation = {}                                                                                             
def get_activation(name):                                                                                   
    def hook(model, input, output):                                                                         
        activation[name] = output[0].detach()                                                               
    return hook                                                                                             
model = SentenceTransformer(xxx)                
for name, layer in model.named_modules():                                                               
        layer.register_forward_hook(get_activation(name))                                                                                                                                               
output = model.encode("test sentence")                                                                             
for key in activation:                                                                    
     print(activation[key])
</code></pre>
<p>I get this error</p>
<pre><code class="lang-auto">in hook
    activation[name] = output.detach()
AttributeError: 'tuple' object has no attribute 'detach'
</code></pre>
<p>Also when going through the <a href="https://github.com/UKPLab/sentence-transformers/blob/master/sentence_transformers/SentenceTransformer.py" rel="noopener nofollow ugc">sentence transformer</a> code, within the encode method the forward seems to be calculated with no grad, might that be the problem?</p>

username: AlphaBetaGamma96
cooked: <p>With hooks, the returned object is of type <code>tuple</code>. So you need to get the 0-th element of that tuple in order to get the <code>Tensor</code> out of the <code>tuple</code>, as you did here</p>
<aside class="quote no-group" data-username="otatopeht" data-post="1" data-topic="130829">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/o/e19b73/48.png" class="avatar"> otatopeht:</div>
<blockquote>
<pre><code class="lang-auto">def get_activation(name):                                                                                   
    def hook(model, input, output):                                                                         
        activation[name] = output[0].detach()                                                               
    return hook  
</code></pre>
</blockquote>
</aside>
<p>I think with hooks you shouldn’t embed them within other functions, the hook should be a standalone function like this,</p>
<pre><code class="lang-auto">def hook(module, input, output):                                                                         
  name = module.__class__.__name__
  activation[name] = output[0].detach()
</code></pre>
<p>but you’d need to change the <code>name</code> variable, if the <code>name</code> is derived from the the particular layer. This can be done via <code>module.__class__.__name__</code> (check if these two statements give the same results!)</p>

username: otatopeht
cooked: <p>Thank you for replying!</p>
<p>I changed the code as you mentioned and I get this:</p>
<blockquote>
<p>TypeError: hook() missing 2 required positional arguments: ‘input’ and ‘output’</p>
</blockquote>

username: AlphaBetaGamma96
cooked: <p>Can you share exactly what you changed? So, it’s easier for me to debug! A reproducible example would be great!</p>

username: otatopeht
cooked: <p>Thank you for replying!<br>
The error is fixed<br>
Here is the code:</p>
<pre><code class="lang-auto">from sentence_transformers import SentenceTransformer

def hook(name, output):
    #name = module.__class__.__name__
    activation[name] = output[0].detach()
activation = {}
model = SentenceTransformer('T-Systems-onsite/cross-en-de-roberta-sentence-transformer')
output = model.encode("test sentence", convert_to_tensor=True)
for name, layer in model.named_modules():
    layer.register_forward_hook(hook(name,output))
print(activation)
</code></pre>
<p>the output of the intermediate layers is the same.</p>

username: AlphaBetaGamma96
cooked: <p>That’s great it works!</p>
<p>One comment, <code>hooks</code> have predefined arguments which should take the <code>module</code> first. You can read about it in the docs, but just a comment in case future hooks don’t work!</p>

username: Ryan06
cooked: <aside class="quote no-group quote-modified" data-username="AlphaBetaGamma96" data-post="2" data-topic="130829" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p>With hooks, the returned object is of type <code>tuple</code>. So you need to get the 0-th element of that tuple in order to get the <code>Tensor</code> out of the <code>tuple</code>, as you did here</p>
<aside class="quote no-group" data-username="otatopeht" data-post="1" data-topic="130829">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/o/e19b73/48.png" class="avatar"> otatopeht:</div>
<blockquote>
<pre><code class="lang-auto">def get_activation(name):                                                                                   
    def hook(model, input, output):                                                                         
        activation[name] = output[0].detach()                                                               
    return hook  
</code></pre>
</blockquote>
</aside>
<p>I think with hooks you shouldn’t embed them within other functions, the hook should be a standalone function like this,</p>
<pre><code class="lang-auto">def hook(module, input, output):                                                                         
  name = module.__class__.__name__
  activation[name] = output[0].detach()
</code></pre>
<p>but you’d need to change the <code>name</code> variable, if the <code>name</code> is derived from the the particular layer. This can be done via <code>module.__class__.__name__</code> (check if these two statements give the same results!) <a href="https://www.myloweslife.biz/" rel="noopener nofollow ugc">Myloweslife</a></p>
</blockquote>
</aside>
<p>Hello,</p>
<p>That’s great, It’s work for me, Really appreciate for help.</p>