Class DiceLoss<T>
- Namespace
- AiDotNet.LossFunctions
- Assembly
- AiDotNet.dll
Implements the Dice loss function, commonly used for image segmentation tasks.
public class DiceLoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
DiceLoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Dice loss measures the overlap between predicted and actual segments in an image. It's based on the Dice coefficient (also known as F1 score), which is a statistical measure of similarity.
The formula is: DiceLoss = 1 - (2 * intersection) / (sum of predicted + sum of actual)
Where:
- intersection is the sum of element-wise multiplication of predicted and actual values
- A value of 0 means perfect overlap (ideal predictions)
- A value of 1 means no overlap at all (worst predictions)
Key properties:
- It's ideal for problems where the positive class (what you're trying to detect) is rare
- Handles imbalanced data better than cross-entropy in many cases
- Focuses on maximizing the overlap between predictions and ground truth
- Commonly used in medical image segmentation, satellite imagery, and other segmentation tasks
Unlike cross-entropy, which treats each pixel independently, Dice loss considers the global relationship between predicted and actual masks, which often leads to better segmentation results.
Constructors
DiceLoss()
Initializes a new instance of the DiceLoss class.
public DiceLoss()
Methods
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the derivative of the Dice loss function.
public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values.
actualVector<T>The actual (target) values.
Returns
- Vector<T>
A vector containing the derivatives of Dice loss for each prediction.
CalculateLoss(Vector<T>, Vector<T>)
Calculates the Dice loss between predicted and actual values.
public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>The predicted values (typically probabilities between 0 and 1).
actualVector<T>The actual (target) values (typically 0 or 1).
Returns
- T
The Dice loss value.