Answers.Body: <p>Yes, you can use <strong>weighted MSE</strong> by applying different coefficients to each data point.</p>
  <p>Here is an implementation given by Francisco Massa at <a href="https://discuss.pytorch.org/t/how-to-implement-weighted-mean-square-error/2547" rel="nofollow noreferrer">https://discuss.pytorch.org/t/how-to-implement-weighted-mean-square-error/2547</a>.</p>
  <pre><code>def mse_loss(input, target):
      return torch.sum((input - target) ** 2)
  
  def weighted_mse_loss(input, target, weight):
      return torch.sum(weight * (input - target) ** 2)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-03T05:58:15.217Z
Answers.LastActivityDate: 2022-08-03T05:58:15.217Z
Answers.UserDisplayName: Jaume Oliver Lafont
Body: <p>I have a very unbalanced data set that I am running a CNN on for regression.  Most of the values are 0, while it is possible for the values to range from 0 to 32.</p>
  <p>Is it possible to &quot;tweak&quot; the cost function to penalize rarer cases more severely?</p>
  <p>I'm using <code>torch.nn.MSELoss(reduction='none')</code> as my loss function.</p>
  
Tags: <convolutional-neural-networks><pytorch><loss>
Title: "Tweaking" the cost function to penalize rarer cases more severely
UserDisplayName: Paul Reiners
ThreadText: # "Tweaking" the cost function to penalize rarer cases more severely
  
  (Asked by: username_0 on 2022-08-02)
  
  I have a very unbalanced data set that I am running a CNN on for regression. Most of the values are 0, while it is possible for the values to range from 0 to 32.
  
  Is it possible to "tweak" the cost function to penalize rarer cases more severely?
  
  I'm using `torch.nn.MSELoss(reduction='none')` as my loss function.
  
  **Tags:** convolutional-neural-networks, pytorch, loss
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-08-03. Score: 2 | Accepted answer
  
  Yes, you can use **weighted MSE** by applying different coefficients to each data point.
  
  Here is an implementation given by Francisco Massa at <https://discuss.pytorch.org/t/how-to-implement-weighted-mean-square-error/2547>.
  
  ```
  def mse_loss(input, target):
      return torch.sum((input - target) ** 2)
  
  def weighted_mse_loss(input, target, weight):
      return torch.sum(weight * (input - target) ** 2)
  
  ```
  
  
  ---
  
  