Questions about torchmetrics MSE

username: Chin_GM
cooked: <p>Dear all,</p>
<p>good day.<br>
I computed the MSE using torchmetrics after the test.<br>
I found that the value is strange after I descale.</p>
<p>MSE value before descale is 0.0018.</p>
<p>MSE after descale is 0.7052.</p>
<p>May I know the correct implementation for the MSE?<br>
Shall I compute it before descale since the train, val and test all done in scaling?</p>
<p>I suspect that the MSE is amplify after descale.</p>
<p>Please give me some feedback or comments if anyone know about this.</p>
<p>Thank you very much</p>

username: ptrblck
cooked: <p>Could you describe your use case a bit more and what “descale” refers to, please?</p>

username: Chin_GM
cooked: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>I use minmax scaler from sklearn.<br>
scaler = MinMaxScaler(feature_range=(-1,1))</p>
<p>scaler = scaler.fit(features_df)</p>
<p>dummy_data = scaler.data_max_ + 5, scaler.data_min_ - 5</p>
<p>scaler = scaler.fit(dummy_data)</p>
<p>scaler.transform(train_df)</p>
<p>After trained and test,</p>
<p>descaler =MinMaxScaler()</p>
<p>descaler.min_, descaler.scale_ = scaler.min_[2], scaler.scale_[2]</p>
<p>def descale(descaler, values):</p>
<p>values_2d = np.array(values)</p>
<p>return descaler.inverse_transform(values_2d)</p>
<p>predictions_descaled = descale(descaler, sq)</p>
<p>speed_labels_descaled = descale(descaler, speed_labels)</p>
<p>The high MSE loss happened after I use predictions_descaled and speed_labels_descaled</p>
<p>Thanks</p>

username: ptrblck
cooked: <p>The change in the loss value would be expected, since you are comparing the mse loss for tensors on a different scale.<br>
Here is a very simple example:</p>
<pre><code class="lang-python">a = torch.randn(10)
b = torch.randn(10)
mse_loss = ((a - b)**2).mean()
&gt; tensor(1.9161)

a_scaled = a * 1000.
b_scaled = b * 1000.
mse_loss_scaled = ((a_scaled - b_scaled)**2).mean()
&gt; tensor(1916108.7500)
</code></pre>
<p>As you can see, the loss difference is created by the multiplication with <code>1000.</code> for <code>a</code> and <code>b</code>.<br>
This doesn’t mean that one error is “better” or “worse” than the other and depends how you are interpreting the output values.</p>
<p>E.g. scaling of target and model prediction values is sometimes done in regression tasks for e.g. keypoint detection. During training it might be beneficial for the model to predict the keypoint coordinates in the range <code>[0, 1]</code> while the original coordinate space could have been <code>[0, 224]</code>.<br>
While calculating the <code>MSELoss</code> for these scaled outputs and targets allows the model to train properly, its meaning might be hard to interpret since you won’t be able to easily map it to a location error in the “pixel space”. Unscaling the predictions and comparing them to the targets in the original range would allow you to use the <code>MSELoss</code> in this pixel space.</p>

username: Chin_GM
cooked: <p>hi <a class="mention" href="/u/ptrblck">@ptrblck</a> ,</p>
<p>Thanks for the reply.<br>
I am not sure about the last sentence from your reply.<br>
’ Unscaling the predictions and comparing them to the targets in the original range would allow you to use the <code>MSELoss</code> in this pixel space.’</p>
<p>Is this means unscale the predctions and labels in training process just before the computation of MSELoss?<br>
I understand and see many practice that unscale is done only after the testing.<br>
I saw some journal papers often publish the value of MSELoss, so I wonder the MSELoss is before unscale or after unscale.<br>
That is the part I confuse.</p>
<p>Thanks</p>

username: ptrblck
cooked: <p>The last sentence explained how the <code>MSELoss</code> can be interpreted.<br>
I.e. I would claim you are free to scale the loss during training to make the model converge, but the loss value itself might not be meaningful. E.g. considering the previous example of the keypoint detection I would like to know how many pixels are my predicted keypoints away from the target keypoints. To do so, I would need to get predictions in the “pixel space”, i.e. the predictions should have values in the range <code>[0, image_height]</code> and <code>[0, image_width]</code> (same as the target). If I’ve scaled the predictions during training to e.g. a range of <code>[0, 1]</code> (for better convergence), I would need to unscale them in order to interpret the loss in my target domain (i.e. the pixel space or image coordinates).</p>
<aside class="quote no-group" data-username="Chin_GM" data-post="5" data-topic="137018">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chin_gm/48/30412_2.png" class="avatar"> Chin_GM:</div>
<blockquote>
<p>I saw some journal papers often publish the value of MSELoss, so I wonder the MSELoss is before unscale or after unscale.</p>
</blockquote>
</aside>
<p>I don’t know, but would assume the authors explain it in their paper.</p>