Equivalence of slicing and assign in Aten C++

eusouoandre: <p>If I dont something like this:</p>
<blockquote>
<p>auto Xbt = torch::zeros({ batch_size, 10, 5, (3 + p) });<br>
Xbt.narrow(0, 0, 1) = Xm.repeat({ 10, 1 }).view({ 10, 5, (3 + p) });</p>
</blockquote>
<p>Will be the same as ‘like’:</p>
<blockquote>
<p>Xbt[0] = Xm.repeat({ 10, 1 }).view({ 10, 5, (3 + p) });</p>
</blockquote>
<p>I just wanted to do something like this from Python:</p>
<blockquote>
<p>Xbt[0, :, : , : ] = Xm.repeat(10, 1).view(10, 5, (3 + p))</p>
</blockquote>

tom: <p>If you can use narrow/slice/slect for getting the subtensor, using <code>copy_</code> for assignment works. For advanced indexing assignments use <code>index_put_</code>.</p>
<p>Best regards</p>
<p>Thomas</p>

eusouoandre: <p><a class="mention" href="/u/tom">@tom</a> thanks for the answer! But why do I need to use <code>copy_</code>? After your suggestions I saw something like this <a href="https://stackoverflow.com/a/57737809/1207193" rel="nofollow noopener">here</a> in stackoverflow.</p>
<p><code>Xbt.slice(0, 0, 1) = Xm.repeat({ 10, 1 }).view({ 10, 5, (3 + p) });</code></p>
<p>Would this not work?</p>
<p>Also found my way through discuss search and the <strong>golden tip about translate python to C++</strong>. And discovered that this will work:</p>
<p><code>Xbt.select(0, 0).copy_(Xm.repeat(10, 1).view(10, 5, (3+p)))</code></p>
<p><strong>Golden tip about translate from python to C++</strong></p>
<ul>
<li>Translate your python script using C++ like functions like <code>put_</code>, <code>narrow</code> testing it (to making sure it works) than just go to C++ replicating everything.</li>
</ul>

tom: <p>I must admit that I’m not using stack overflow as a source of PyTorch advice much but would venture that the average person answering here (heavily skewed towards ptrblck and alband) is more involved in PyTorch than the average stackoverflower - point in case: as noticed by in the comments to that answer, the code piece does have at least one important typo (Tensor vs. tensor) leading to non-working code.<br>
Back in the day when I re-implemented torch::bilinear in C++, one could not assign to the result of a function call but had to assign it to a temporary variable first. That is probably more than 2 years now, so it might have changed, but this is why I would use copy. In Python it’s impossible to assign to a function call result, too, so <code>copy_</code> does work better with the golden tip, too.</p>
<p>Best regards</p>
<p>Thomas</p>

yf225: <p>Starting from the current nightly build (and PyTorch 1.5 soon), for</p>
<pre><code class="lang-auto">Xbt[0, :, : , : ] = Xm.repeat(10, 1).view(10, 5, (3 + p))
</code></pre>
<p>we can write</p>
<pre><code class="lang-auto">using namespace torch::indexing;
Xbt.index_put_({ 0, Slice(), Slice(), Slice() }, Xm.repeat({ 10, 1 }).view({ 10, 5, (3 + p) }));
</code></pre>
<p>Here is the general translation for <code>Tensor::index</code> and <code>Tensor::index_put_</code> functions:</p>
<pre><code class="lang-auto">Python             C++ (assuming `using namespace at::indexing`)
-------------------------------------------------------------------
0                  0
None               None
...                "..." or Ellipsis
:                  Slice()
start:stop:step    Slice(start, stop, step)
True / False       true / false
[[1, 2]]           torch::tensor({{1, 2}})
</code></pre>