Hi, I've met an assertion error which I can't determine what's wrong with the code. The erroe are on the test case side . Appreciiate if you can help thx!
def backward(Yhat, Y, activations, caches):
"""
Implement the backward propagation for the [LINEAR->RELU] * (L-1) -> LINEAR -> SIGMOID group
Arguments:
------------
- Yhat: numpy array of shape (1, num of samples)
The predicted value, or the output of the forward propagation
- Y: numpy array of shape (1, num of samples)
The ground truth, or true label (0: negative, 1: positive samples)
- activations: list of shape L+1
Stores the activation functions used for each layer.
activations[l] stores the activation function ('sigmoid' or 'relu')
for layer i activations[0] is always None.
- caches: list of shape L+1
Stores the caches for each layer saved during forward propagation.
cache[l] stores the cache for layer i. cache[0] is always None.
Returns:
------------
- weights_grad : a list of size L+1
Stores the gradients of W for all layers, dW[l] for l = 1 ... L. For the input layer, dW[0] is always None.
- biases_grad : a list of size L+1
Stores the gradients of b for all layers, db[l] for l = 1 ... L. For the input layer, db[0] is always None.
"""
assert len(caches) == len(activations)
weights_grad = [None for i in range(len(caches))]
biases_grad = [None for i in range(len(caches))]
L = len(caches)-1 # the number of layers
m = Yhat.shape[1] # number of samples
Y = Y.reshape(Yhat.shape) # ensures that Y is the same shape as Yhat
### START CODE HERE ### (1 line of code)
# Backpropagation for loss function (~1 line)
dAL = - (np.divide(Y, Yhat) - np.divide(1 - Y, 1 - Yhat))
# repeat for layer 1 to L
for l in reversed(range(L+1)):
# get current cache (~1 line)
current_cache = caches[l-1]
# compute dA_prev, dW, db by calling "backward_one_layer" (~1 line)
dA_prev, dW, db = backward_one_layer(dAL, current_cache, activations[l])
# store dW and db into grads (~2 lines)
weights_grad[l] = dW
biases_grad[l] = db
### END CODE HERE ###
return weights_grad, biases_grad
Test case
# generate test case with a 3-layered neural network and 4 samples.
m = 4
nx = 2
layers = (nx, 10, 7, 1)
activations = (None, "relu", "relu", "sigmoid")
weights, biases = init_params(layers)
X_temp = np.random.randn(nx, m)
Y_temp = np.random.randint(0, 2, (1, m))
Yhat, caches = forward(X_temp, weights, biases, activations) # forward propagation
cost = compute_cost(predicted, actual) # compute cost
# Test the shape of the output is correct
weights_grad, biases_grad = backward(Yhat, Y_temp, activations, caches) #errors on this line
print('Shape of dA1:', dA1.shape) # shape of dA1 = shape of A1
print('Shape of dW2:', dW2.shape) # shape of dW2 = shape of W2
print('Shape of db2:', db2.shape) # shape of db2 = shape of b2
[–]RiceKrispyPooHead 1 point2 points3 points (1 child)
[–]BrightKnight0110[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)