Scattering of data

Matthias_Moller: <p>I require a functionality that comes close to TensorFlows <code>scatter_nd</code> function, I guess.</p>
<p>Let’s assume that I have an <code>m-x-n</code> matrix <code>A</code> and four row vectors <code>N</code>, <code>S</code> (of size <code>n</code>) and <code>E</code>, <code>E</code> (of size <code>m</code>). I need to create a new matrix <code>B</code> with the four vectors replacing the first/last row/column, e.g.</p>
<pre><code class="lang-auto">    | N1    N2     ...  Nn-1     Nn   |
    | W2    A2,2   ...  A2,n-1   E2   |
B = | W3    A3,2   ...  A3,n-1   E3   |
    | ...   ...    ...  ...      ...  |
    | Wm-1  Am-1,2 ...  Am-1,n-1 Em-1 |
    | S1    S2     ...  Sn-1     Sn   |
</code></pre>
<p>The vectors are such that N1=W1, Wm=S1, … so it does not matter whether N1 or W1 it put at B1,1, etc.</p>
<p>The above ist just an example, I need the same for 3 and 4 dimensional tensors, then replacing the “boundaries” of the tensors by matrices and 3-d tensors, respectively.</p>
<p>Any help is appreciated.</p>

KFrank: <p>Hi Matthias!</p>
<aside class="quote no-group" data-username="Matthias_Moller" data-post="1" data-topic="182800" 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/matthias_moller/48/43979_2.png" class="avatar"> Matthias_Moller:</div>
<blockquote>
<pre><code class="lang-auto">    | N1    N2     ...  Nn-1     Nn   |
    | W2    A2,2   ...  A2,n-1   E2   |
B = | W3    A3,2   ...  A3,n-1   E3   |
    | ...   ...    ...  ...      ...  |
    | Wm-1  Am-1,2 ...  Am-1,n-1 Em-1 |
    | S1    S2     ...  Sn-1     Sn   |
</code></pre>
</blockquote>
</aside>
<p>You may modify <code>A</code> (or a copy of it) by assigning into it using slices:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; print (torch.__version__)
2.0.0
&gt;&gt;&gt;
&gt;&gt;&gt; m = 5
&gt;&gt;&gt; n = 6
&gt;&gt;&gt;
&gt;&gt;&gt; A = 5 * torch.ones (m, n)
&gt;&gt;&gt;
&gt;&gt;&gt; N = 1 * torch.ones (n)
&gt;&gt;&gt; S = 2 * torch.ones (n)
&gt;&gt;&gt; E = 3 * torch.ones (m)
&gt;&gt;&gt; W = 4 * torch.ones (m)
&gt;&gt;&gt;
&gt;&gt;&gt; A[ 0, :] = N
&gt;&gt;&gt; A[-1, :] = S
&gt;&gt;&gt; A[:, -1] = E
&gt;&gt;&gt; A[:,  0] = W
&gt;&gt;&gt;
&gt;&gt;&gt; A
tensor([[4., 1., 1., 1., 1., 3.],
        [4., 5., 5., 5., 5., 3.],
        [4., 5., 5., 5., 5., 3.],
        [4., 5., 5., 5., 5., 3.],
        [4., 2., 2., 2., 2., 3.]])
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

Matthias_Moller: <p>Hi Frank,</p>
<p>thanks. Two questions on this: How do I realise this in C++ (<a href="https://pytorch.org/cppdocs/notes/tensor_indexing.html" class="inline-onebox" rel="noopener nofollow ugc">Tensor Indexing API — PyTorch main documentation</a>) and does the above work when I need to compute gradients of the resulting tensor?</p>
<p>Best,<br>
Matthias</p>

KFrank: <p>Hi Matthias!</p>
<aside class="quote no-group" data-username="Matthias_Moller" data-post="3" data-topic="182800" 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/matthias_moller/48/43979_2.png" class="avatar"> Matthias_Moller:</div>
<blockquote>
<p>does the above work when I need to compute gradients of the resulting tensor?</p>
</blockquote>
</aside>
<p>Yes, this will work fine for computing gradients both with respect to <code>A</code> and the<br>
<code>N</code>, <code>S</code>, <code>E</code>, <code>W</code> vectors.</p>
<p>Note that, as written, my suggestion modifies <code>A</code> inplace.  This can complicate,<br>
but does not invalidate, the use of gradients.</p>
<p>(I don’t know anything about the C++ api.)</p>
<p>Best.</p>
<p>K. Frank</p>

Matthias_Moller: <p>Thanks, Frank.</p>
<p>I tried your suggestion and translated it to C++ according to <a href="https://pytorch.org/cppdocs/notes/tensor_indexing.html" class="inline-onebox" rel="noopener nofollow ugc">Tensor Indexing API — PyTorch main documentation</a>. The following does indeed work:</p>
<pre><code class="lang-cpp">A=.index_put_({ 0, "..."}, N);
A=.index_put_({-1, "..."}, S);
A=.index_put_({"...", -1}, E);
A=.index_put_({"...",  0}, W);
</code></pre>