Message Boards Message Boards

Use custom loss function in NetTrain?

As far as I know, there are 3 loss layers available to use in NetTrain function. But is there anyway to use custom and more complex loss functions? As a simple example, I just tried to define a custom (but the same) MeanAbsoluteLossLayer exactly like what it was in it's documentations:

meanAbsLoss = Mean@Flatten[Abs[#Input - #Target], Infinity] &;

But when I use this loss function in NetTrain, I get this failure error:

"Loss specification should be a loss layer, Automatic, or an output \port or list of ports."

Function::slot1: (Mean[Flatten[Abs[#Input-#Target],\[Infinity]]]&)[] is expected to have an Association as the first argument.

Can you please help me with this problem?

POSTED BY: Iman Nazari
2 Replies

For 11.0 and 11.1 there is only one way of defining a custom loss: build it out of existing net layers (any net that has a scalar output can be used for training). There are many more layers coming in 11.1 which will allow for a great deal more flexibility in defining your own loss functions (including the loss you are trying to define above).

We are looking into supporting users writing loss functions that cannot be written as compositions of existing layers, but this will only be ready earliest 11.2.

Could you demonstrate how to construct a "mean squared relative error" loss function using existing net layers in 11.0 (if possible) ?

POSTED BY: Daniel George
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract