Invalid index at scatter?

username: farazk86
cooked: <p>I’m trying to implement <code>scatter_</code> in my custom loss function:</p>
<pre><code class="lang-auto">encoded_target = input.data.clone().zero_()
encoded_target.scatter_(1, target.unsqueeze(1), 1)
</code></pre>
<p><code>input</code> is the output from my CNN network and is of the shape <code>[1,1,32,64,64]</code> and <code>target</code> is the ground truth used to calculate the loss, it has the shape <code>[1,32,64,64]</code>, hence the <code>unsqueeze(1)</code> to bring it to the same shape as input.</p>
<p><code>input</code> is of type <code>float32 </code> and <code>target</code> is of type <code>long</code>.</p>
<p>I get the following error at <code>scatter_</code>:</p>
<blockquote>
<p>RuntimeError: Invalid index in scatter at c:\new-builder_2\win-wheel\pytorch\aten\src\th\generic/THTensorMath.cpp:703</p>
</blockquote>
<p>Ive checked the documentation on this and my implementation is correct, Is there any reason this is not working?</p>
<p>P.S. I noticed that if I change the dimension in <code>scatter_</code> it works:</p>
<p><code>encoded_target.scatter_(2, target.unsqueeze(1), 1)</code></p>
<p>But this is not what i want, why is working for dimension <code>2</code> and failing for <code>1</code>?</p>
<p>Thanks</p>

username: farazk86
cooked: <p>I couldnt get the <code>scatter_</code> to work. As a work around I managed to create my <code>target</code> using a thesholding method:</p>
<pre><code class="lang-auto">where_ones = torch.ones(target.shape, dtype=torch.long) 
where_zeros = torch.zeros(target.shape, dtype=torch.long)
target_ = torch.where(target &gt; 0.1, where_ones, where_zeros)
target = torch.unsqueeze(target_, 0).float()
</code></pre>
<p>This works and gives the loss output but I’m not sure if this is the preferred method.<br>
Is what what I am doing equivalent to the output produced by <code>scatter_</code>?<br>
I read in one of the docs that for <code>target</code> is is advised to use the <code>scatter_</code> method as this was the tensor is differentiable and <code>autograd</code>  can be utilized. Or was this the case for previous versions of pytorch?</p>

username: ptrblck
cooked: <p>I’m not sure, how you would like to apply scatter in this case, as dim1 only is of size 1.<br>
You would get this operation:</p>
<blockquote>
<p>self[i][index[i][j][k]][k] = src[i][j][k]  # if dim == 1</p>
</blockquote>
<p>, which means that your scatter index (<code>target</code> in your case` can only hold zeros.<br>
Other values will yield an invalid index.</p>
<p>Could you post a small sample with values and how the result should look like?</p>

username: farazk86
cooked: <p>Sorry, I found this code online for DiceLoss and wanted to use it in my code, below is the full code for DiceLoss:</p>
<pre><code class="lang-auto">    def forward(self, input, target):
        weights = 1
        encoded_target = input.data.clone().zero_()
        encoded_target.scatter_(1, target.unsqueeze(1), 1)
        # encoded_target = Variable(encoded_target)
        assert input.size() == encoded_target.size(), "Input sizes must be equal."
        assert input.dim() == 5, "Input must be a 4D Tensor."
        num = (input * encoded_target).sum(dim=4).sum(dim=3).sum(dim=2)
        den1 = input.pow(2).sum(dim=4).sum(dim=3).sum(dim=2)
        den2 = encoded_target.pow(2).sum(dim=4).sum(dim=3).sum(dim=2)
        dice = (2 * num / (den1 + den2)) * weights
        return ((1-dice.sum()) / dice.size(1)) / 5
</code></pre>
<p>I want to create a target tensor to calculate the loss against. Thats why I thought if I create it in the same was as in my second post will it be the same thing as scatter?</p>
<p>I assume scatter here is trying to do the same thing…</p>

username: yaghoubi
cooked: <p><a class="mention" href="/u/farazk86">@farazk86</a><br>
Hi, I am working on a code and I have run to the same error derivating form the loss function. This link explains my error <a href="https://github.com/michuanhaohao/reid-strong-baseline/issues/120" rel="nofollow noopener">https://github.com/michuanhaohao/reid-strong-baseline/issues/120</a>. Did you find a solution for fixing the error?</p>