# Creating custom loss function in CNN for regression

**URL:** <https://discuss.pytorch.org/t/creating-custom-loss-function-in-cnn-for-regression/99945>\
**Category:** vision\
**Created:** [October 20, 2020, 1:42am UTC](https://discuss.pytorch.org/t/creating-custom-loss-function-in-cnn-for-regression/99945 "2020-10-20T01:42:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hbolandi](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hbolandi/32/31062_2.png) [@hbolandi](https://discuss.pytorch.org/u/hbolandi)\
**Post date:** [October 20, 2020, 1:42am UTC](https://discuss.pytorch.org/t/creating-custom-loss-function-in-cnn-for-regression/99945/1 "2020-10-20T01:42:24Z")

</div>

I am trying to make my CNN regression network work, but output is far from ground truth. The input is a binary image (600x600) which the background is black, and foreground is white. The ground truth associated with each input is an image with color range from 0 t 255 which is normalized between 0 and 1.

Below, you can see one sample of Input (x), ground truth(y) and predicted output (y\_hat), respectively. The network can almost detect edges and background but in foreground all the predicted values are almost same (see predicted one).

I believe that since all foregrounds in input are 1, network cannot update weights correctly and it may need custom loss function to penalize loss function.

I am wondering whether my idea is right or not, if yes what kind of custom function I need?

I tried to penalize the foreground by custom loss function below, but it didn’t improve the result.

> mse = nn.MSELoss(reduction=‘mean’)
> 
> def criterion(y, y\_hat, x, loss, weight=0.1):  
> y\_hat\_modified = torch.where(x[:,0:1]==1, weight\*y\_hat,y\_hat) # x[:,0:1] is input  
> return loss(y,y\_hat\_modified)

 ![610](https://discuss.pytorch.org/uploads/default/original/3X/3/f/3fa204b3989fca0dece23fc95661625762a3e078.png) ![real-n1000](https://discuss.pytorch.org/uploads/default/original/3X/a/f/afe1c5a71678e4b22f291c52328f0c4dcf67b9b5.png) ![3layers-0res-epoch15-filter12-n1000](https://discuss.pytorch.org/uploads/default/original/3X/1/8/183e506d4f2f0ece66072e7cc561a0d699835c7f.png)
