# Build Customer Loss Function

**URL:** <https://community.konduit.ai/t/build-customer-loss-function/420>\
**Category:** DL4J\
**Created:** [April 15, 2020, 6:22pm UTC](https://community.konduit.ai/t/build-customer-loss-function/420 "2020-04-15T18:22:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jin](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jin](https://community.konduit.ai/u/jin)\
**Post date:** [April 15, 2020, 6:22pm UTC](https://community.konduit.ai/t/build-customer-loss-function/420/1 "2020-04-15T18:22:47Z")

</div>

I am building a regression model with dense layers. The provided MSE loss function does not accurately reflect the main concerns of the functional domain. In addition to MSE, I would like to penalize more when the model fails to predicate value beyond certain threshold. So the real loss function would be the weighted average of MSE and getting predication wrong when actual is greater than a threshold. What’s the best starting place to implement a customer loss function? I looked at the ILossFunction interface. It looks that one has to supply the gradient computation instead of just supplying the expression of the loss function and letting the numerical gradient calculation taking it over. I would appreciate any useful pointer. Thanks.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [April 15, 2020, 7:47pm UTC](https://community.konduit.ai/t/build-customer-loss-function/420/2 "2020-04-15T19:47:59Z")

</div>

You are right that custom loss functions currently require both an implementation for a forward pass and a backward pass.

But there is nothing that should stop you from utilizing SameDiff (for examples of general use see [https://github.com/eclipse/deeplearning4j-examples/tree/master/nd4j-examples/src/main/java/org/nd4j/samediff](https://github.com/eclipse/deeplearning4j-examples/tree/master/nd4j-examples/src/main/java/org/nd4j/samediff) and [https://github.com/eclipse/deeplearning4j-examples/tree/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/samediff](https://github.com/eclipse/deeplearning4j-examples/tree/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/samediff)) to do that.

I know SameDiff isn’t as well documented as we’d like it to be, but if you don’t feel like implementing the backwards pass yourself.

---

<div class="post-metadata">

**Author:** ![jin](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jin](https://community.konduit.ai/u/jin)\
**Post date:** [April 16, 2020, 3:48am UTC](https://community.konduit.ai/t/build-customer-loss-function/420/3 "2020-04-16T03:48:53Z")

</div>

Paul,

Thanks for the pointers.

---

<div class="post-metadata">

**Author:** ![Dris101](https://avatars.discourse-cdn.com/v4/letter/d/94ad74/32.png) [@Dris101](https://community.konduit.ai/u/Dris101)\
**Post date:** [July 31, 2020, 9:16pm UTC](https://community.konduit.ai/t/build-customer-loss-function/420/4 "2020-07-31T21:16:26Z")

</div>

Take a look at **SameDiffLoss** :-

> <https://github.com/eclipse/deeplearning4j/blob/master/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/lossfunctions/SameDiffLoss.java>

You only have to implement:-

```java
public abstract SDVariable defineLoss(SameDiff sd, SDVariable layerInput, SDVariable labels);

```

and the grad calculation is autodiff’d for you via SameDiff and wrapped up so you can use it in DL4J. It’s very helpful as it does all the heavy lifting for you.
