Use TopK as a Selection But Fail to Autograd

Stone_Yu: <p>Hi there,</p>
<p>My framework contains two parts: 1). a selection mechanism 2). a predictor. The input data is a sequence but not all items in the sequence are useful. So I just use TopK items in the sequence to predict something.</p>
<p>But I failed to calculate the gradients since using top K indices block gradients flow to the selection model. (second part of the code)</p>
<p>A toy code as following:</p>
<pre><code>seq = torch.randn(10, 15, 8)                  # batch * seq_length * feature
sel_model = nn.Linear(8, 1)                   # the selection mechanism
scores = sel_model(seq).squeeze() 

# using topK indices bring the autograd problem
val, ind = torch.topk(scores, 3, dim=1, largest=True)
ind_exp = ind.unsqueeze(-1).expand(10, 3, 8)
selected_seq = torch.gather(seq, 1, ind_exp) 

feature = selected_seq.mean(-1)
pred_model = nn.Linear(3, 1)
pred = pred_model(feature).squeeze()
loss = pred.sum()  # assume there is a loss
loss.backward()

print('sel_model grad：', sel_model.weight.grad)  # grad is None
print('select scores grad：', scores.grad)        # grad is None
print('pred_model grad：', pred_model.weight.grad)
</code></pre>
<p>So how to allow the gradients back to the selection model?</p>

tom: <p>Yeah, well, the core of the problem is that you are not using scores or val in the further calculation. If you then do the calculation based on the input sequence, you cannot expect gradients (which factor through <code>dloss/dval</code> and <code>dloss/dscores = dval/dscores dloss/dval</code>) to flow back. (You need to use <code>scores.retain_grad()</code> to keep intermediate gradients, btw.)</p>
<p>Now, if you consider only the indices as the output of your selection model, you could look towards learning techniques in reinforcement learning for discrete decisions.</p>
<p>Best regards</p>
<p>Thomas</p>

Stone_Yu: <p>Hey thanks, it is helpful.</p>
<p>However, I’m still wondering what’s the correct way to use top-k values, as <code>top-k</code> will break the chain rule (not sure just in my opinion)? Could you provide some examples?</p>
<p>Below is a modified version of the previous code, using top-k values.</p>
<pre><code>data = torch.randn(10, 15)  # batch * features
select_model = nn.Linear(15, 15)  # each feature has a score
scores = select_model(data)

# use top-3 features and mask the rest
val, ind = torch.topk(scores, 3, dim=1, largest=True)
masked_scores = torch.zeros_like(scores)
masked_scores.scatter_(1, ind, val)
masked_data = data * masked_scores  # mask some sequences

# I can use retain_grad to keep grad, but which one I should use?
# scores.retain_grad()
# masked_scores.retain_grad()
# masked_data.retain_grad()

# do the prediction task using the masked data
pred_model = nn.Linear(15, 1)
pred = pred_model(masked_data).squeeze()
loss = pred.sum()  # assume there is a loss
loss.backward()

print('select_model grad：', select_model.weight.grad.size())
print('select scores grad：', scores.grad)  # grad is None
print('masked scores grad：', masked_scores.grad)  # grad is None
print('pred_model grad：', pred_model.weight.grad.size())</code></pre>

tom: <aside class="quote no-group" data-username="Stone_Yu" data-post="3" data-topic="58004">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stone_yu/48/16600_2.png" class="avatar"> Stone_Yu:</div>
<blockquote>
<p>However, I’m still wondering what’s the correct way to use top-k values, as <code>top-k</code> will break the chain rule (not sure just in my opinion)? Could you provide some examples?</p>
</blockquote>
</aside>
<p>topk will propagate gradients from the grad of output values to the grad of inputs all right (in fact, the backward is a scatter to the indices similar to the ones you have), so the topk part you show should work now.</p>
<p>Best regards</p>
<p>Thomas</p>

Stone_Yu: <p>Yes. Topk part works well even without <code>retain_grad()</code>. There might be some other problems.</p>
<p>Thanks!</p>