Convolutional Neural Networks - Week 2/Assignment 1/Residual Networks

When creating a post, please add:
On Exercise 1 - identity_block, I am getting the followinng error. I am stuck how to progress, any help is appreciated.


AssertionError Traceback (most recent call last)
Input In [10], in <cell line: 30>()
26 A4 = identity_block(X, f=2, filters=[3, 3, 3],
27 initializer=lambda seed=0:constant(value=1))
28 print(np.around(A4.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))
—> 30 public_tests.identity_block_test(identity_block)

File /tf/W2A1/public_tests.py:36, in identity_block_test(target)
33 assert np.floor(resume[1, 1, 0]) == 2 * np.floor(resume[1, 1, 3]), “Check the padding and strides”
34 assert np.floor(resume[1, 1, 0]) == 2 * np.floor(resume[1, 1, 3]), “Check the padding and strides”
—> 36 assert resume[1, 1, 0] - np.floor(resume[1, 1, 0]) > 0.7, “Looks like the BatchNormalization units are not working”
38 assert np.allclose(resume,
39 np.array([[[ 0., 0., 0., 0., ],
40 [ 0., 0., 0., 0., ]],
(…)
44 [290.99988, 290.99988, 290.99988, 146.99994]]]),
45 atol = 1e-5 ), “Wrong values with training=False”
47 tf.keras.backend.set_learning_phase(True)

AssertionError: Looks like the BatchNormalization units are not working

Here is the code after:

you cannot edit this cell

tf.keras.backend.set_learning_phase(False)

np.random.seed(1)
tf.random.set_seed(2)
X1 = np.ones((1, 4, 4, 3)) * -1
X2 = np.ones((1, 4, 4, 3)) * 1
X3 = np.ones((1, 4, 4, 3)) * 3

X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)

A3 = identity_block(X, f=2, filters=[4, 4, 3],
initializer=lambda seed=0:constant(value=1))
print(‘\033[1mWith training=False\033[0m\n’)
A3np = A3.numpy()
print(np.around(A3.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))
resume = A3np[:,(0,-1),:,:].mean(axis = 3)
print(resume[1, 1, 0])

tf.keras.backend.set_learning_phase(True)

print(‘\n\033[1mWith training=True\033[0m\n’)
np.random.seed(1)
tf.random.set_seed(2)
A4 = identity_block(X, f=2, filters=[3, 3, 3],
initializer=lambda seed=0:constant(value=1))
print(np.around(A4.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))

public_tests.identity_block_test(identity_block)

The error message “Looks like the BatchNormalization units are not working” suggests that there might be an issue with the BatchNormalization layer in the identity_block function.

Looking at the code, I notice that the BatchNormalization layer is applied with the ‘training’ parameter set to ‘training’. However, in the testing phase, I’m setting the learning phase to both True and False before calling the identity_block function. This setup may be causing the BatchNormalization layer to behave differently than expected.

The code in the “cell you cannot edit” is correct. Do not modify it, and do not worry about its settings for the training phase.

Your code in identity_block() should not make any reference to the training phase.

The assert is triggered when your code is tested with a specific combination of kernel size and filters.

So check your code in identity_block() to verify it is handling the f and filters parameters correctly.

Thank you for replying. I believe my code is handling the f and filters parameters correctly. Please could I DM you to share the code.

Thank you

Robbie

Update for those who find this thread later:

  1. Do not modify any code outside of a “YOUR CODE HERE” area.
  2. Do not use training=training inside the identity_block() function.
1 Like