Week#2 Exercise 1 - identity_block Error: 'Input has undefined rank:', TensorShape(None)

Does anyone know what this error means?
Thanks

ValueError Traceback (most recent call last)
in
8 A3 = identity_block(X, f=2, filters=[4, 4, 3],
9 initializer=lambda seed=0:constant(value=1),
—> 10 training=False)
11 print(‘\033[1mWith training=False\033[0m\n’)
12 A3np = A3.numpy()

in identity_block(X, f, filters, training, initializer)
32 ## Second component of main path (≈3 lines)
33 X = Conv2D(filters = F2, kernel_size = 1, strides = (1,1), padding = ‘valid’, kernel_initializer = initializer(seed=0))
—> 34 X = BatchNormalization(axis = 3)(X, training = training)
35 X = Activation(‘relu’)(X)
36

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)
980 with ops.name_scope_v2(name_scope):
981 if not self.built:
→ 982 self._maybe_build(inputs)
983
984 with ops.enable_auto_cast_variables(self._compute_dtype_object):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
2641 # operations.
2642 with tf_utils.maybe_init_scope(self):
→ 2643 self.build(input_shapes) # pylint:disable=not-callable
2644 # We must set also ensure that the layer is marked as built, and the build
2645 # shape is stored since user defined build functions may not be calling

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/normalization.py in build(self, input_shape)
285 input_shape = tensor_shape.TensorShape(input_shape)
286 if not input_shape.ndims:
→ 287 raise ValueError(‘Input has undefined rank:’, input_shape)
288 ndims = len(input_shape)
289

ValueError: (‘Input has undefined rank:’, TensorShape(None))

I think it means that “training = training” should be used inside the function call, not as part of a tuple that contains the X dataset.

Thanks for your help.
I removed “training = training”, but I still get the same error.

They give you an example of what the BatchNorm call looks like in the template code. Yours should look the same. It looks like you have not supplied an input argument to the Conv2D call in the “Second Component”. There again, they gave you an example in the “First Component”. Your Conv2D layer should be the same, but you need to adjust the filter, kernel size, stride and padding. Other than that, the way the input works is the same. We are doing the Keras Functional API here with the “layer” functions, so there are two layers of inputs: the first set of parameters define the layer characteristics and that returns the actual function that you call with an input tensor. They don’t give much useful documentation in the notebooks, other than the sample code. Here’s a thread that goes into more detail about the Sequential and Functional APIs.

1 Like

Thanks for your help.
You are completely right, I missed the input argument (X) to the Conv2D for the second component.

I had to have the “training = training” term with the argument (X) to be able to run and pass the test. Same as below:

X = BatchNormalization(axis = 3)(X, training = training)

Thanks for your help