Replace part of the elements in the tensor, how to handle？

Yongjie_Shi: <p>Hello everyone. I have recently come across a problem.<br>
I have a tensor A and a tensor B. I want to paste B to A with some known position information.<br>
The whole process can be described as follows.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/8/b8ee0b64fb4d18866856ec6ec1b674a65fd55e11.png" data-download-href="https://discuss.pytorch.org/uploads/default/b8ee0b64fb4d18866856ec6ec1b674a65fd55e11" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/8/b8ee0b64fb4d18866856ec6ec1b674a65fd55e11.png" alt="image" data-base62-sha1="qnXRtJ8xZ1HzP8tQn8kMdrAR625" width="690" height="256" data-dominant-color="EDEDED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1511×561 18 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/7/076d49ca92ed8e7ed9419f0ae7a08fef76b00575.png" data-download-href="https://discuss.pytorch.org/uploads/default/076d49ca92ed8e7ed9419f0ae7a08fef76b00575" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/7/076d49ca92ed8e7ed9419f0ae7a08fef76b00575.png" alt="image" data-base62-sha1="13HusuvCmWlV93SvohrRApdx3SJ" width="690" height="217" data-dominant-color="F2F2F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1527×482 18.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ecf1698012284438f75f0a988f83b1637897d0e9.png" data-download-href="https://discuss.pytorch.org/uploads/default/ecf1698012284438f75f0a988f83b1637897d0e9" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ecf1698012284438f75f0a988f83b1637897d0e9.png" alt="image" data-base62-sha1="xO5UaZXgythD3amwn2JtrjREPG1" width="690" height="430" data-dominant-color="EAEAEA"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1369×855 18.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I read some documentation about scatter_ or index_put, but I don’t know how to deal with this problem, because the position corresponding to each patch is irregular…</p>

ptrblck: <p>Based on your image you could either use direct indexing or e.g. <code>masked_scatter_</code>:</p>
<pre><code class="lang-python">A = torch.zeros(3, 5, 5)
B = torch.randn(3, 2, 2)

A[0, 1:3, 1:3] = B[0]
A[1, 0:2, 3:5] = B[1]
A[2, 3:5, 0:2] = B[2]
print(A)

A = torch.zeros(3, 5, 5)
mask = torch.zeros(3, 5, 5).bool()
mask[0, 1:3, 1:3] = True
mask[1, 0:2, 3:5] = True
mask[2, 3:5, 0:2] = True
A.masked_scatter_(mask, B)
print(A)
</code></pre>
<p>I don’t know how the indices are created and which approach might be preferred.</p>