quacknet.lossFunctions

 1import math
 2import numpy as np
 3
 4def MSELossFunction(predicted, true):
 5    """
 6    Calculates the Mean Squared Error (MSE) loss.
 7
 8    Args:
 9        predicted (list / ndarray): The predicted values from the model.
10        true (list / ndarray): The true target values.
11
12    Returns:
13        float: The mean squared error between predicted and true values.
14    """
15    return np.mean((np.array(true) - np.array(predicted)) ** 2)
16
17def MAELossFunction(predicted, true):
18    """
19    Calculates the Mean Absolute Error (MAE) loss.
20
21    Args:
22        predicted (list / ndarray): The predicted values from the model.
23        true (list / ndarray): The true target values.
24
25    Returns:
26        float: The mean absolute error between predicted and true values.
27    """
28    return np.mean(np.abs(np.array(true) - np.array(predicted)))
29
30def CrossEntropyLossFunction(predicted, true):
31    """
32    Calculates the Cross Entropy loss.
33
34    Args:
35        predicted (list / ndarray): The predicted probabilities from the model.
36        true (list / ndarray): The true target values.
37
38    Returns:
39        float: The cross entropy loss between predicted probabilities and true values.
40    
41    Notes:
42        Predicted probabilities are clipped to the range [1e-10, 1-1e-10] to avoid numerical instability.
43    """
44    predicted = np.clip(predicted, 1e-10, 1-1e-10)
45    return -np.sum(np.array(true) * np.log(predicted))
def MSELossFunction(predicted, true):
 5def MSELossFunction(predicted, true):
 6    """
 7    Calculates the Mean Squared Error (MSE) loss.
 8
 9    Args:
10        predicted (list / ndarray): The predicted values from the model.
11        true (list / ndarray): The true target values.
12
13    Returns:
14        float: The mean squared error between predicted and true values.
15    """
16    return np.mean((np.array(true) - np.array(predicted)) ** 2)

Calculates the Mean Squared Error (MSE) loss.

Args: predicted (list / ndarray): The predicted values from the model. true (list / ndarray): The true target values.

Returns: float: The mean squared error between predicted and true values.

def MAELossFunction(predicted, true):
18def MAELossFunction(predicted, true):
19    """
20    Calculates the Mean Absolute Error (MAE) loss.
21
22    Args:
23        predicted (list / ndarray): The predicted values from the model.
24        true (list / ndarray): The true target values.
25
26    Returns:
27        float: The mean absolute error between predicted and true values.
28    """
29    return np.mean(np.abs(np.array(true) - np.array(predicted)))

Calculates the Mean Absolute Error (MAE) loss.

Args: predicted (list / ndarray): The predicted values from the model. true (list / ndarray): The true target values.

Returns: float: The mean absolute error between predicted and true values.

def CrossEntropyLossFunction(predicted, true):
31def CrossEntropyLossFunction(predicted, true):
32    """
33    Calculates the Cross Entropy loss.
34
35    Args:
36        predicted (list / ndarray): The predicted probabilities from the model.
37        true (list / ndarray): The true target values.
38
39    Returns:
40        float: The cross entropy loss between predicted probabilities and true values.
41    
42    Notes:
43        Predicted probabilities are clipped to the range [1e-10, 1-1e-10] to avoid numerical instability.
44    """
45    predicted = np.clip(predicted, 1e-10, 1-1e-10)
46    return -np.sum(np.array(true) * np.log(predicted))

Calculates the Cross Entropy loss.

Args: predicted (list / ndarray): The predicted probabilities from the model. true (list / ndarray): The true target values.

Returns: float: The cross entropy loss between predicted probabilities and true values.

Notes: Predicted probabilities are clipped to the range [1e-10, 1-1e-10] to avoid numerical instability.