Threshold/Filter input data

username: Rakuen
cooked: <p>My dataset consists of arrays whose elements are between -1.5 and 2.0. But in these arrays there are some elements which are -10. I want to train my model ( image-to-image prediction) without considering these -10 elements.<br>
One of my arrays is looking like bottom one( as a representation).</p>
<blockquote>
<p>Blockquote<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/e/9eeae25f60ff51191745a803aa535d1f0b894a15.png" alt="image" data-base62-sha1="mFQG0w4ObxC0kROrNLIDPqJvgJn" width="597" height="119"></p>
</blockquote>
<p>Is there any way to filter this data by giving threshold or any other way so that in the output array from my model preserves these -10s and predicts other data only.</p>

username: Balamurali_M
cooked: <p>Can’t you make it as zero ? Like x(x==-10) = 0</p>

username: anantguptadbl
cooked: <p><a class="mention" href="/u/rakuen">@Rakuen</a><br>
Applying filters within your model would be difficult. Instead you can apply filters on the input data like</p>
<p><code>x[x!=-10]</code></p>
<p>while sending data to the model to be trained</p>
<p>The problem is foresee is that during prediction(inference) you would have to apply the same rule, so that your model does not get to predict the rows for -10</p>

username: Rakuen
cooked: <p>Thank you. I will try but I didn’t understand it all. Ok, I can send data to model without-10s with your suggestion but I am not sure whether my model gives an error about its size. Anyway I will try and see tomorrow. I also thought that I can use something like torch.clamp(output, min=-9) but didn’t try it neither.</p>

username: anantguptadbl
cooked: <p><a class="mention" href="/u/rakuen">@Rakuen</a>  Sure, share some data.  We might be able to help out</p>

username: Rakuen
cooked: <p>As I expected, It didn’t work. Size is the problem. Data has size<br>
<code>torch.Size([1, 1, 256, 256])</code><br>
but after <code>x[x!=-10]</code><br>
<code>torch.Size([62779])</code><br>
So my model didn’t accept data. But I also tried <code>torch.clamp</code>. I applied this to my data before sending to model and for the ground truth before calculating loss function and so backpropagation. It is working but in a wrong way. I want to estimate weights of my model without taking account of this -10 data. Because I think my model predicts near this -10 region by considering it.</p>

username: beaupreda
cooked: <p>If I understand correctly, you want your input to keep the same size (256 x 256)? What does your ground-truth looks like? (not super familiar with image-to-image prediction)</p>

username: Rakuen
cooked: <p>Yes, I want to keep it the same (256x256) and my model will be trained for just non-10 data:)</p>

username: beaupreda
cooked: <p>Ok, what loss function do you use? And are your targets (ground-truth) also images?</p>

username: Rakuen
cooked: <p>MSE. Inputs and targets are numpy arrays (256x256, float).</p>

username: beaupreda
cooked: <p>Ok, here what I think you could try. Basically, your model will learn to not predict the -10 values and hopefully learn to ignore them from the input. It may not be exactly as you want, but it is the only way I can see it done without modifying the size of the input. It is inspired by what is done in stereo estimation, as you can see here <a href="https://github.com/JiaRenChang/PSMNet/blob/master/finetune.py#L93" rel="nofollow noopener">code</a>.</p>
<pre><code class="lang-auto"># consider only the elements different of -10
mask = (target != -10)
mask.detach_()
...
loss = F.mse_loss(input[mask], target[mask])
loss.backward()
...
</code></pre>

username: Rakuen
cooked: <p>Ok, I will try. Thank you.</p>