Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
A NetGraph layer implementing focal loss
ResourceFunction["FocalLossLayer"][α,γ] represents a net layer that implements focal loss. |
"Input" | scalar values between 0 and 1, or arrays of these |
"Target" | scalar values between 0 and 1, or arrays of these |
"Loss" | real number |
Create a focal loss layer:
In[1]:= | ![]() |
Out[1]= | ![]() |
Apply it to a given input and target:
In[2]:= | ![]() |
Out[2]= | ![]() |
Use FocalLossLayer with single probabilities for the input and target:
In[3]:= | ![]() |
Out[4]= | ![]() |
Apply FocalLossLayer with inputs and targets being matrices of binary-class probabilities:
In[5]:= | ![]() |
Out[6]= | ![]() |
Apply the layer to an input and target:
In[7]:= | ![]() |
Out[7]= | ![]() |
Use a focal loss layer during training:
In[8]:= | ![]() |
Out[11]= | ![]() |
In[12]:= | ![]() |
Out[12]= | ![]() |
Define a FocalLossLayer operating on color images:
In[13]:= | ![]() |
Out[13]= | ![]() |
In[14]:= | ![]() |
Out[14]= | ![]() |
Plot a comparison of FocalLossLayer (abbreviated as "FL") and standard CrossEntropy (abbreviated as "CE"). Note that setting γ>0 reduces the relative loss for well-classified examples (pt>.5), thus focusing on the harder examples:
In[15]:= | ![]() |
Out[736]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License