What does the scatter_ function do in layman terms?

username: Bassel
cooked: <p>I read the examples in the documentation and the explanation but i still can’t understand what this function does.</p>

username: albanD
cooked: <p>Hi,</p>
<p>It is setting particular values of a tensor at the provided indices. The value that you set can either be always the same or provided by another tensor wich is the same size as the indices tensor.<br>
For example, if you want to do a one hot encoding of a 1D tensor of labels, you can start with a 2D tensor filled with zeros and then scatter 1s according to the labels of each entry.</p>

username: jon
cooked: <p>In case anyone is looking at this after the documentation, here is an explanation for how they arrived at the first result:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; x = torch.rand(2, 5)
&gt;&gt;&gt; x
tensor([[ 0.3992,  0.2908,  0.9044,  0.4850,  0.6004],
        [ 0.5735,  0.9006,  0.6797,  0.4152,  0.1732]])
&gt;&gt;&gt; torch.zeros(3, 5).scatter_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)
tensor([[ 0.3992,  0.9006,  0.6797,  0.4850,  0.6004],
        [ 0.0000,  0.2908,  0.0000,  0.4152,  0.0000],
        [ 0.5735,  0.0000,  0.9044,  0.0000,  0.1732]])

</code></pre>
<p>The scatter says “send the elements of x to the following indices in torch.zeros, according to ROW-WISE (dim 0)”. In layman’s terms, this is saying, for each element in the original<br>
<code>[ 0.3992, 0.2908, 0.9044, 0.4850, 0.6004], [ 0.5735, 0.9006, 0.6797, 0.4152, 0.1732]</code> tensor, we specify a row index (0,1 or 2) to send it to in the tensor we are scattering into.</p>
<p><a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.scatter_" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/tensors.html#torch.Tensor.scatter_</a></p>

username: Shisho_Sama
cooked: <p>Sorry for asking but could you elaborate more on this?<br>
I don’t understand the index part!?</p>
<p>Update :<br>
I guess I understood it thanks to God!<br>
0, 1, 2, 0 , 0 are the row indexes of our destination tensor .<br>
this simply means, put the first element from source into the first row  of  the destination tensor (at the same exact index )<br>
so 0th element in x which is <code>0.3992</code> goes to row 0. and in index 0.<br>
1tst element in x which is <code>0.2908</code> goes to row 1 and in index 1<br>
2nd element in x which is <code>0.9044</code> will go to row 2 and in index 2<br>
3rd element in x which is <code>0.4850</code> will go to row 0 and in index 3<br>
4th element in x which is ’ <code>0.6004</code> will go to row 0 and in index 4<br>
and this goes on.<br>
Basically, all elements in source will have their respective index, but they only are redirected to a specific row in the destination tensor!<br>
Is this right?</p>

username: hypnagogic
cooked: <p><a class="mention" href="/u/shisho_sama">@Shisho_Sama</a>: almost. You got it right, <strong>given dim=0</strong>  (i.e. row in this case). But if <strong>dim=1</strong> (i.e. column), the source row position would be maintained, and the indices would be used to specify the column position in destination.</p>
<p>Essentially, think of ‘dim’ with ‘index’, and keep everything else in output the same as in ‘src’.</p>

username: devstorm
cooked: <p>The ‘_’ in <code>scatter_</code> signifies that the operation has to happen in-place.</p>