# When should I create a custom loss function?

I'm using a neural network to solve a multi regression problem because I'm trying to predict continuous values. To be more specific, I'm making a tracking algorithm to track the position of an object, I'm trying to predict two values, the latitude and longitude of an object.

Now, to calculate the loss of the model, there are some common functions, like mean squared error or mean absolute error, etc., but I'm wondering if I can use some custom function, like this, to calculate the distance between the two longitude and latitude values, and then the loss would be the difference between the real distance (calculated from the real longitude and latitude) and the predicted distance (calculated from the predicted longitude and latitude). These are some thoughts from me, so I'm wondering if such an idea would make sense?

Would this work in my case better than using the mean squared error as a loss function?

I had another question in mind. In my case, I'm predicting two values (longitude and latitude), but is there a way to transform these two target values to only one value so that my neural network can learn better and faster? If yes, which method should I use? Should I calculate the summation of the two and make that as a new target? Does this make sense?

Using two value and using MSE is probably a better approach. I'd you combine the value to one value, like the case of summation, the network may fits to output 0 on one axis and the value on the other. The method you propose also have the same issue. There are many combination to the real distance, but only one is correct. For a neural network to learn faster, one value will not help it learn faster. Instead, accuracy is often increased if the predicted value is a one hot vector of labels instead of a single value. Hope this can help you.

• I didn't understand what you mean by this: " the network may fits to output 0 on one axis and the value on the other". and how can the accuracy be increased with one hot encoding? I'm trying to predict continuous values that means accuracy doesn't make sense in this case. Accuracy would make a sense in case of discrete values not continuous values Oct 22, 2019 at 10:34
• If you use 2 values a and b and you use the (a+b) as the value to compute the loss, there is many different ways that a and b can form a combination, not only the combination (longitude and latitude) you want. Oct 22, 2019 at 11:28
• exactly, that's why I hope that someone can give me a better way to do this because obviously I need to calculate the distance between two coordinates and use it as a loss function otherwise I don't know how my loss should be Oct 22, 2019 at 11:40
• MSE should work. Oct 22, 2019 at 12:20

Alternatively, you might measure the angle between the two vectors (assuming they are points on a sphere), perhaps using their scalar product and use that as the loss function.

$$a \cdot b = \|a\|\|b\|cos\theta$$

(or just use polar co-ordinates)

An important question is whether the direction of the errors is likely to be uniform or whether errors in particular directions happen more often than others (in which case that needs to be built into the loss function)