Hi, I am trying to implement a regression model that will predict the absolute angle of rotation of an image. For this, I need to implement a custom loss function, however I cannot figure out how to do this.

I first prototyped what this function would look like for a single training instance. It finds the angle between y and yhat:

def loss(y, yhat):

option1 = abs(y-yhat)

option2 = 360 - option1

return min(option1,option2)

However, I am lost when it comes to how I would define this function to be compatible with fastai. Based on my research, it seems like I would need to wrap my function in the BaseLoss class. Could someone show me what this is supposed to look like? I couldn’t find examples online.

I’ve seen that once I figure this out, I can just pass it to the learner:

learn.loss_func = my_loss_function

Then I can train just like normal.

Note: I am working with fastai v2