Batch proc instead of loop

username: p_may
cooked: <p>Hi all, is there any loop-free way of doing this?</p>
<pre><code class="lang-auto">all = torch.zeros(batch, n, dim)

for B in range(batch):
    wpe = torch.zeros(n,dim)
    for i in range(n):
        wpe[w][:] = torch.sum(atten_matrix[B][:][i] * ee.permute(0,2,1),dim=2)
       # ee.permute(0,2,1).size() =&gt; batch x dim x L
       # atten_matrix.size() batch x L x L
    all[B] = wpe
</code></pre>

username: KFrank
cooked: <p>Hi Pi!</p>
<aside class="quote no-group" data-username="p_may" data-post="1" data-topic="79186" data-full="true">
<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/p_may/48/23526_2.png" class="avatar"> p_may:</div>
<blockquote>
<p>Hi all, is there any loop-free way of doing this?</p>
</blockquote>
</aside>
<p>There are several issues with the code you posted that make it<br>
unclear what you are trying to do, so we can’t really say whether<br>
you can do it without loops.</p>
<p>We don’t know what <code>w</code> is, and <code>wpe[w][:]</code> refers to the same<br>
data for all iterations of the inner loop, so you might as well<br>
just write:</p>
<pre data-code-wrap="python"><code class="lang-python">    wpe[w][:] = torch.sum(atten_matrix[B][:][n - 1] * ee.permute(0,2,1),dim=2)
</code></pre>
<p>Did you mean something like <code>wpe[w][:] += ...</code> or<br>
<code>wpe[i][:] = ...</code>?</p>
<p>It would be best if you could post a complete, runnable script<br>
(including small sample data) that does what you want, and<br>
also post the results.  Then we could see for sure what you<br>
are trying to do, and see if we can reproduce those results<br>
with a loop-free approach.</p>
<p>Best.</p>
<p>K. Frank</p>

username: p_may
cooked: <aside class="quote no-group" data-username="KFrank" data-post="2" data-topic="79186">
<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/k/ecb155/48.png" class="avatar"> KFrank:</div>
<blockquote>
<p>permute(0,2,1),dim=2)</p>
</blockquote>
</aside>
<p>Thanks Frank for the reply.<br>
Sorry that was my bad, in <code>wpe[w][:] = torch.sum(atten_matrix[B][:][i] * ee.permute(0,2,1),dim=2)</code> w should be <code>i</code>. About <code>wpe</code>, <code>atten_matrix[B][:][i] * ee.permute(0,2,1)</code> gives me <code>L</code> vectors (L x dim) and I sum them up along the second (actually third) dimension (dim=2), which gives me one vector (1 x dim).</p>

username: KFrank
cooked: <p>Hi Pi!</p>
<aside class="quote no-group" data-username="p_may" data-post="3" data-topic="79186" data-full="true">
<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/p_may/48/23526_2.png" class="avatar"> p_may:</div>
<blockquote>
<p>About <code>wpe</code>, <code>atten_matrix[B][:][i] * ee.permute(0,2,1)</code> gives me <code>L</code> vectors (L x dim) and I sum them up along the second (actually third) dimension (dim=2), which gives me one vector (1 x dim).</p>
</blockquote>
</aside>
<p>Please post complete, working code – with loops – that does<br>
what you want (and the results you get when you run it).  That<br>
will be the most satisfactory description of what you are trying<br>
to do.</p>
<p>Best.</p>
<p>K. Frank</p>