Check the padding and strides

my code is true but i can not pass this level

Hi @Murat_Hamarat

Can you give us more information? What Exercise are you stuck on and what errors are you getting?

Cheers,

it is my code

def convolutional_block(X, f, filters, s = 2, training=True, initializer=glorot_uniform):
“”"
Implementation of the convolutional block as defined in Figure 4

Arguments:
X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
f -- integer, specifying the shape of the middle CONV's window for the main path
filters -- python list of integers, defining the number of filters in the CONV layers of the main path
s -- Integer, specifying the stride to be used
training -- True: Behave in training mode
            False: Behave in inference mode
initializer -- to set up the initial weights of a layer. Equals to Glorot uniform initializer, 
               also called Xavier uniform initializer.

Returns:
X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
"""

# Retrieve Filters
F1, F2, F3 = filters

# Save the input value
X_shortcut = X


##### MAIN PATH #####
# First component of main path glorot_uniform(seed=0)
X = Conv2D(filters = F1, kernel_size = (1,1), strides = (s, s), padding='valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X, training=training)
X = Activation('relu')(X)

## Second component of main path
X = Conv2D(F2, kernel_size = (f, f), strides = (1,1), padding = 'same', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X)
X = Activation('relu')(X)

## Third component of main path
X = Conv2D(F3, kernel_size = (1, 1), strides = (1,1), padding = 'valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis = 3)(X)
X = Activation('relu')(X)

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

# Final step: Add shortcut value to main path (Use this order [X, X_shortcut]), and pass it through a RELU activation
X = Add()([X, X_shortcut])
X = Activation('relu')(X)

return X

and i got this error

AssertionError Traceback (most recent call last)
in
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.

Why use batch normalization on X when computing X_shortcut?

1 Like

Hi @Murat_Hamarat,

As @TMosh pointed out, if you look at the graph, X_shortcut doesn’t get any transformation, but is added the result of passing X through all the convolutional block up to that point, and then the addition gets a relu afterwards.

The error you are getting is because if you apply a ‘valid’ convolution, the size of the resulting tensor will change and it won’t match the modified X one.

Hope that helps!

So many thanks neurogeek