Skip to content

Calculate DW Method

Functions

calculate_dW(dZ, A_prev, m)

Calculate the gradient of the weights.

Parameters:

Name Type Description Default
dZ ndarray

Gradient of the cost with respect to the linear output.

required
A_prev ndarray

Activations from the previous layer.

required
m int

Number of training examples.

required

Returns:

Type Description

numpy.ndarray: Gradient of the weights.

Example
dW = calculate_dW(dZ, A_prev, 32)
print(dW.shape)
Source code in microkeras/operations/backward/calculate_dW.py
def calculate_dW(dZ, A_prev, m):
    """
    Calculate the gradient of the weights.

    Args:
        dZ (numpy.ndarray): Gradient of the cost with respect to the linear output.
        A_prev (numpy.ndarray): Activations from the previous layer.
        m (int): Number of training examples.

    Returns:
        numpy.ndarray: Gradient of the weights.

    Example:
        ```python
        dW = calculate_dW(dZ, A_prev, 32)
        print(dW.shape)
        ```
    """
    result = (1 / m) * np.dot(dZ, A_prev.T) 
    return np.array(result).astype(np.float64)