Skip to content

Calculate DZ Method

Functions

calculate_dZ(W_next, dZ_next, Z, activation)

Calculate dZ for hidden layers.

Parameters:

Name Type Description Default
W_next ndarray

Weights of the next layer.

required
dZ_next ndarray

dZ of the next layer.

required
Z ndarray

Linear output of the current layer.

required
activation str

Activation function of the current layer.

required

Returns:

Type Description

numpy.ndarray: Gradient of Z.

Example
dZ = calculate_dZ(W_next, dZ_next, Z, 'relu')
print(dZ.shape)
Source code in microkeras/operations/backward/calculate_dZ.py
def calculate_dZ(W_next, dZ_next, Z, activation):
    """
    Calculate dZ for hidden layers.

    Args:
        W_next (numpy.ndarray): Weights of the next layer.
        dZ_next (numpy.ndarray): dZ of the next layer.
        Z (numpy.ndarray): Linear output of the current layer.
        activation (str): Activation function of the current layer.

    Returns:
        numpy.ndarray: Gradient of Z.

    Example:
        ```python
        dZ = calculate_dZ(W_next, dZ_next, Z, 'relu')
        print(dZ.shape)
        ```
    """
    activation_derivatives = {
        'sigmoid': sigmoid_derivative,
        'relu': relu_derivative,
        'linear': linear_derivative
    }
    activation_derivative = activation_derivatives[activation]
    return np.dot(W_next.T, dZ_next) * activation_derivative(Z)