Skip to content

Backward Layer Method

Functions

backward_layer(model, layer_index, X, Y, A_prev, loss, m)

Perform backward propagation for a single layer in the model.

Parameters:

Name Type Description Default
model Sequential

The neural network model.

required
layer_index int

Index of the current layer.

required
X ndarray

Input data.

required
Y ndarray

True labels.

required
A_prev ndarray

Activation from the previous layer.

required
loss str

Loss function used.

required
m int

Number of training examples.

required

Returns:

Name Type Description
tuple

(dZ, dW, db) gradients for the current layer.

Example
dZ, dW, db = backward_layer(model, 1, X, Y, A_prev, 'categorical_crossentropy', 32)
print(dZ.shape, dW.shape, db.shape)
Source code in microkeras/operations/backward/backward_layer.py
def backward_layer(model, layer_index, X, Y, A_prev, loss, m):
    """
    Perform backward propagation for a single layer in the model.

    Args:
        model (Sequential): The neural network model.
        layer_index (int): Index of the current layer.
        X (numpy.ndarray): Input data.
        Y (numpy.ndarray): True labels.
        A_prev (numpy.ndarray): Activation from the previous layer.
        loss (str): Loss function used.
        m (int): Number of training examples.

    Returns:
        tuple: (dZ, dW, db) gradients for the current layer.

    Example:
        ```python
        dZ, dW, db = backward_layer(model, 1, X, Y, A_prev, 'categorical_crossentropy', 32)
        print(dZ.shape, dW.shape, db.shape)
        ```
    """
    layer = model.layers[layer_index]

    dZ = calculate_dZ_wrapper(model, layer_index, Y, loss)
    layer.dZ = dZ  # Set dZ for the current layer
    dW = calculate_dW_wrapper(model, layer_index, X if layer_index == 0 else A_prev, m)
    db = calculate_db_wrapper(model, layer_index, m)

    return dZ, dW, db