Answers.Body: <p>Your suggestion should work to focus the ML more on larger angle examples.</p>
  <p>You may want to try a slightly simpler approach of weighting the loss (or the resulting gradient) by a factor depending on rarity of the angle in the dataset. That would give you greater control, and a mathematical rationale for choosing weight values (usually inverse of frequency in the dataset for each group). This is a common weighting method for imbalanced classes, that you would be adapting for your regression problem - your could start by bucketing your data into 5 degree &quot;classes&quot; with an outermost class of e.g. everything greater than 40 degrees, and then assign a weighting to each bucket.</p>
  <p>In terms of end results, it doesn't usually matter what loss function you end up using to train your model. What matters is how well the resulting model works in production environments where its predictions are being used for something practical.</p>
  <p>Ultimately you should base decision of what the &quot;best&quot; model is using some metric on untrained data, and ideally you set that metric <em>before</em> you go about trying different approaches. Otherwise you could get lost looking at errors you don't like, but have no way to assess if fixing them and dealing with the inevitable <em>other errors</em> this will produce ends up with a better model. The metric should, as much as possible, reflect the actual costs to your project for inaccuracy.</p>
  <p>There is no free lunch, and whatever approach you take you will need to concern yourself with:</p>
  <ul>
  <li><p>Focusing on fitting better to rarer data means potentially fitting worse to the more common data.</p>
  </li>
  <li><p>Focusing on fitting better to rarer data means larger risk of overfitting in general.</p>
  </li>
  <li><p>Techniques for weighting or adaptive loss functions add more hyperparameters to tune.</p>
  </li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-09T08:25:01.267Z
Answers.Text: Thanks for your advice. I find the idea of weighting the loss in terms of the frequency of the data very interesting. Indeed in production I used another metric with other images. Then as you say, It may make sense to include this during validation.
Answers.UserDisplayName: Cezoz08
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-08T16:45:32.447Z
Answers.LastActivityDate: 2023-11-08T16:50:35.930Z
Answers.LastEditDate: 2023-11-08T16:50:35.930Z
Answers.UserDisplayName: Neil Slater
Body: <p>I'm working on a regression problem with a CNN in which the input is a single image, and the output is an angle in degrees (which determines a specific measure related to the image).</p>
  <p>Sometimes, the model fails to retrieve the output accurately (for output angles wider than 20°). By analyzing the data, I can suspect there is a problem of imbalance since there are a lot of training samples with outputs between -20° and 20° but very few for wider angles (the wider the angle, the fewer the examples).</p>
  <p>There is no possibility to generate more data to balance the training set artificially. I want to try a robust loss function focusing more on wide angles.</p>
  <p>My model is typically trained with MSE, but I would like to implement a loss function in pytorch, which is linear for ground-truth values lower than 20° (in absolute value) and becomes quadratic for greater values.
  This is similar to the Huber Loss, but 1) the conditions would be the opposite. 2) The condition of the Huber function would be based on the GT instead of the absolute difference between the GT and the output.</p>
  <p>So, my question is whether this solution would make sense from an ML point of view since I haven't seen something like this in ML literature. Or is there maybe a more &quot;standard&quot; way to face this problem ? (apart of data augmentation)</p>
  <p>Thanks in advance.</p>
  
Tags: <machine-learning><deep-learning><ai-design><regression><loss>
Title: Regression loss conditioned by the ground-truth values
UserDisplayName: Cezoz08
ThreadText: # Regression loss conditioned by the ground-truth values
  
  (Asked by: username_0 on 2023-11-08)
  
  I'm working on a regression problem with a CNN in which the input is a single image, and the output is an angle in degrees (which determines a specific measure related to the image).
  
  Sometimes, the model fails to retrieve the output accurately (for output angles wider than 20°). By analyzing the data, I can suspect there is a problem of imbalance since there are a lot of training samples with outputs between -20° and 20° but very few for wider angles (the wider the angle, the fewer the examples).
  
  There is no possibility to generate more data to balance the training set artificially. I want to try a robust loss function focusing more on wide angles.
  
  My model is typically trained with MSE, but I would like to implement a loss function in pytorch, which is linear for ground-truth values lower than 20° (in absolute value) and becomes quadratic for greater values.
  This is similar to the Huber Loss, but 1) the conditions would be the opposite. 2) The condition of the Huber function would be based on the GT instead of the absolute difference between the GT and the output.
  
  So, my question is whether this solution would make sense from an ML point of view since I haven't seen something like this in ML literature. Or is there maybe a more "standard" way to face this problem ? (apart of data augmentation)
  
  Thanks in advance.
  
  **Tags:** machine-learning, deep-learning, ai-design, regression, loss
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-11-08. Score: 3 | Accepted answer
  
  Your suggestion should work to focus the ML more on larger angle examples.
  
  You may want to try a slightly simpler approach of weighting the loss (or the resulting gradient) by a factor depending on rarity of the angle in the dataset. That would give you greater control, and a mathematical rationale for choosing weight values (usually inverse of frequency in the dataset for each group). This is a common weighting method for imbalanced classes, that you would be adapting for your regression problem - your could start by bucketing your data into 5 degree "classes" with an outermost class of e.g. everything greater than 40 degrees, and then assign a weighting to each bucket.
  
  In terms of end results, it doesn't usually matter what loss function you end up using to train your model. What matters is how well the resulting model works in production environments where its predictions are being used for something practical.
  
  Ultimately you should base decision of what the "best" model is using some metric on untrained data, and ideally you set that metric *before* you go about trying different approaches. Otherwise you could get lost looking at errors you don't like, but have no way to assess if fixing them and dealing with the inevitable *other errors* this will produce ends up with a better model. The metric should, as much as possible, reflect the actual costs to your project for inaccuracy.
  
  There is no free lunch, and whatever approach you take you will need to concern yourself with:
  
  * Focusing on fitting better to rarer data means potentially fitting worse to the more common data.
  * Focusing on fitting better to rarer data means larger risk of overfitting in general.
  * Techniques for weighting or adaptive loss functions add more hyperparameters to tune.
  
  > **Comments:**
  > **username_0**: Thanks for your advice. I find the idea of weighting the loss in terms of the frequency of the data very interesting. Indeed in production I used another metric with other images. Then as you say, It may make sense to include this during validation.  
  
  ---
  
  