quacknet.lossDerivativeFunctions

 1import numpy as np
 2from quacknet.activationDerivativeFunctions import SoftMaxDerivative
 3
 4def MSEDerivative(value, trueValue, sizeOfLayer):
 5    """
 6    Calculates the derivative of the Mean Squared Error (MSE) loss function.
 7
 8    Args:
 9        value (ndarray): The predicted values from the model.
10        trueValue (ndarray): The true target values.
11        sizeOfLayer (int): The size of the output layer.
12    
13    Returns:
14        ndarray: The gradients of the MSE loss.
15    """
16    return 2 * (value - trueValue) / sizeOfLayer
17
18def MAEDerivative(value, trueValue, sizeOfLayer):
19    """
20    Calculates the derivative of the Mean Absolute Error (MAE) loss function.
21
22    Args:
23        value (ndarray): The predicted values from the model.
24        trueValue (ndarray): The true target values.
25        sizeOfLayer (int): The size of the output layer.
26    
27    Returns:
28        ndarray: The gradients of the MAE loss.
29    """
30    #summ = value - trueValue
31    #if(summ > 0):
32    #    return 1 / sizeOfLayer
33    #elif(summ < 0):
34    #    return -1 / sizeOfLayer
35    #return 0
36    return np.sign(value - trueValue) / sizeOfLayer
37
38def CrossEntropyLossDerivative(value, trueVale, activationDerivative):
39    """
40    Calculates the derivative of the Cross Entropy loss function.
41
42    Args:
43        value (ndarray): The predicted values from the model.
44        trueValue (ndarray): The true target values.
45        activationDerivative (function): The derivative of the activation function.
46    
47    Returns:
48        ndarray: The gradients of the Cross Entropy loss.
49    """
50    if(activationDerivative == SoftMaxDerivative):
51        return value - trueVale
52    return -1 * (trueVale / value)
def MSEDerivative(value, trueValue, sizeOfLayer):
 5def MSEDerivative(value, trueValue, sizeOfLayer):
 6    """
 7    Calculates the derivative of the Mean Squared Error (MSE) loss function.
 8
 9    Args:
10        value (ndarray): The predicted values from the model.
11        trueValue (ndarray): The true target values.
12        sizeOfLayer (int): The size of the output layer.
13    
14    Returns:
15        ndarray: The gradients of the MSE loss.
16    """
17    return 2 * (value - trueValue) / sizeOfLayer

Calculates the derivative of the Mean Squared Error (MSE) loss function.

Args: value (ndarray): The predicted values from the model. trueValue (ndarray): The true target values. sizeOfLayer (int): The size of the output layer.

Returns: ndarray: The gradients of the MSE loss.

def MAEDerivative(value, trueValue, sizeOfLayer):
19def MAEDerivative(value, trueValue, sizeOfLayer):
20    """
21    Calculates the derivative of the Mean Absolute Error (MAE) loss function.
22
23    Args:
24        value (ndarray): The predicted values from the model.
25        trueValue (ndarray): The true target values.
26        sizeOfLayer (int): The size of the output layer.
27    
28    Returns:
29        ndarray: The gradients of the MAE loss.
30    """
31    #summ = value - trueValue
32    #if(summ > 0):
33    #    return 1 / sizeOfLayer
34    #elif(summ < 0):
35    #    return -1 / sizeOfLayer
36    #return 0
37    return np.sign(value - trueValue) / sizeOfLayer

Calculates the derivative of the Mean Absolute Error (MAE) loss function.

Args: value (ndarray): The predicted values from the model. trueValue (ndarray): The true target values. sizeOfLayer (int): The size of the output layer.

Returns: ndarray: The gradients of the MAE loss.

def CrossEntropyLossDerivative(value, trueVale, activationDerivative):
39def CrossEntropyLossDerivative(value, trueVale, activationDerivative):
40    """
41    Calculates the derivative of the Cross Entropy loss function.
42
43    Args:
44        value (ndarray): The predicted values from the model.
45        trueValue (ndarray): The true target values.
46        activationDerivative (function): The derivative of the activation function.
47    
48    Returns:
49        ndarray: The gradients of the Cross Entropy loss.
50    """
51    if(activationDerivative == SoftMaxDerivative):
52        return value - trueVale
53    return -1 * (trueVale / value)

Calculates the derivative of the Cross Entropy loss function.

Args: value (ndarray): The predicted values from the model. trueValue (ndarray): The true target values. activationDerivative (function): The derivative of the activation function.

Returns: ndarray: The gradients of the Cross Entropy loss.