CNN course Week 3 assignment 2 conv_block

its giving me an unknown error which I’m not able to Debug .
So, pls help me debug and solve it.
AttributeError Traceback (most recent call last)
in
3 inputs = Input(input_size)
4 cblock1 = conv_block(inputs, n_filters * 1)
----> 5 model1 = tf.keras.Model(inputs=inputs, outputs=cblock1)
6
7 output1 = [[‘InputLayer’, [(None, 96, 128, 3)], 0],

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in new(cls, *args, **kwargs)
240 # Functional model
241 from tensorflow.python.keras.engine import functional # pylint: disable=g-import-not-at-top
→ 242 return functional.Functional(*args, **kwargs)
243 else:
244 return super(Model, cls).new(cls, *args, **kwargs)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
→ 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py in init(self, inputs, outputs, name, trainable)
113 # ‘arguments during initialization. Got an unexpected argument:’)
114 super(Functional, self).init(name=name, trainable=trainable)
→ 115 self._init_graph_network(inputs, outputs)
116
117 @trackable.no_automatic_dependency_tracking

/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
→ 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py in _init_graph_network(self, inputs, outputs)
140
141 if any(not hasattr(tensor, ‘_keras_history’) for tensor in self.outputs):
→ 142 base_layer_utils.create_keras_history(self._nested_outputs)
143
144 self._validate_graph_inputs_and_outputs()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer_utils.py in create_keras_history(tensors)
189 the raw Tensorflow operations.
190 “”"
→ 191 _, created_layers = _create_keras_history_helper(tensors, set(), )
192 return created_layers
193

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
224 'op wrapping. Please wrap these ops in a Lambda layer: ’
225 ‘\n\n\n{example}\n\n’.format(example=example))
→ 226 op = tensor.op # The Op that created this Tensor.
227 if op not in processed_ops:
228 # Recursively set _keras_history.

AttributeError: ‘MaxPooling2D’ object has no attribute ‘op’

We can’t see enough from the exception trace to see exactly what the error is, but it’s a problem with how you are invoking the “layer” functions here. Please note that we are using the “Functional API” here, not the “Sequential API”. That means that every layer involves two steps:

  1. Defining the layer function by specifying the relevant sizes and other parameters.
  2. Invoking the defined function from step 1 with particular input tensors and storing the outputs in other tensors.

The syntax looks a little strange at first, but here’s an example of how to invoke the max pooling function using the “Functional API”:

outputTensor = MaxPooling2D(pool_size = (2,2))(inputTensor)

Note the way there are two separate sets of parens there: the first set is calling the layer function with the pool_size parameter, which then returns the actual defined function that we want to call. Then we invoke that resulting function with the inputTensor as the argument. That’s why there are separate parens around the inputTensor: that’s where we actually call the function we just defined with the given pooling size.

3 Likes

thanks that really helped me a lot.