Error in convolutional block

12 assert type(A) == EagerTensor, “Use only tensorflow and keras functions”
13 assert tuple(tf.shape(A).numpy()) == (3, 2, 2, 6), “Wrong shape.”
—> 14 assert np.allclose(A.numpy(), convolutional_block_output1), “Wrong values when training=False.”
15 print(A[0])
16

AssertionError: Wrong values when training=False.

How to fix this error - please advise

You don’t say which assignment you are talking about here but it looks like it might be the Residual Net assignment (ConvNet Week 2, Assignment 1).

Popular mistakes here are:

  1. Forgetting to pass the “training” parameter on all that BatchNorm layers. They give you a sample of what the BatchNorm layers should look like in the template code. Yours should all look the same.
  2. Not using the correct input values in the “Shortcut” section. If you used X as the input in that section, that will not end well …
5 Likes

paul - you are correct about the assignment - it is CNN week2 - first assignment Residual Networks.
I have taken care of all the points mentioned by you and checked the same a number of times. I am stuck !!!

##### SHORTCUT PATH ##### (≈2 lines)
X_shortcut = Conv2D(filters = F3, kernel_size = (1,1), strides = (s, s), padding='valid', kernel_initializer = glorot_uniform(seed=0))(X_shortcut)
X_shortcut = BatchNormalization(axis = 3)(X_shortcut, training=training)

I made changes in my shortcut section as Paul suggested it worked… might be helpful to take a look at these two lines as I passed X_Shortcut as my input here.

5 Likes

It doesn’t work for me. I have tried this earlier also.

edited to delete the code template

The submission worked fine. The error was because of the tester and not the code.

Thanks Paul for the help and guidance

No, it’s a bug in your code: you are “hard-coding” the kernel_initializer values to the global value glorot_uniform. You should be using the “initializer” argument that was passed in. You may get lucky that it happens to match the parameters that the grader uses. The test in the notebook should also pass with that, because they do not pass that optional parameter and glorot_uniform is the default.

I see you even hacked the template code as well. They gave you a worked example of the correct code, but then you broke it.

Just for my ignorance - what is the difference between kernel_initializer and glorot_uniform? These terms have been used in the explanation document again and again

glorot_uniform is a function provided by TF. It is imported in the import cell that is the first executable cell in the notebook. Here is the documentation.

initializer is the variable name given to one of the optional parameters in the function definition of convolutional_block. The default value specified for that parameter is glorot_uniform.

kernel_initializer is the variable name of one of the optional parameters of the function Conv2D, which is also a TF function imported by the import cell in this notebook. Here is the documentation.

With the above in mind, now go take another look at the source for the convolutional_block function and it should make sense. But you probably first want to get a clean copy without your changes, because you have incorrectly modified the examples they gave you in the template code of how to correctly invoke Conv2D. There is a topic about that on the FAQ Thread.

The overall point here is that the variable initializer is the parameter to convolutional_block that should determine what initializer function is passed to Conv2D. But the way you have changed the code, it will always pass glorot_uniform, regardless of what value is passed in to convolutional_block at the top level. That is a bug, although it may not have any ill effect as long as you don’t try to use a different initializer than glorot_uniform.

1 Like

Thanks Paul. That is very helpful.
I will try with a clean copy today and see the results.
Appreciate your help.