C4 W1 A2 Ex2 - convolutional_model_Application : do not know where is the potential error


Could you advise how I should troubleshoot?

I do not see any error message in your image. Did you post the entire error message?

I am not sure where the problem is… could you advise? thanks

Screenshot 2021-11-27 at 5.59.01 PM|690x185

It is the code used in exercise 2

Z1=tf.keras.layers.Conv2D(8,4,strides=(1,1),padding='same')(input_img),
A1=tf.keras.layers.ReLU()(Z1),
P1=tf.keras.layers.MaxPool2D(pool_size=(8,8),strides=8,padding='same',name=A1),
Z2=tf.keras.layers.Conv2D(16,2,strides=(1,1),padding='same',name=P1),
A2=tf.keras.layers.ReLU(name=Z2),
P2=tf.keras.layers.MaxPool2D(pool_size=(4, 4),strides=4,padding='same',name=A2),
F=tf.keras.layers.Flatten(name=P2),
outputs=tf.keras.layers.Dense(6,activation='softmax',name=F),

# YOUR CODE ENDS HERE
model = tf.keras.Model(inputs=input_img, outputs=outputs)
return model

=========================================================

then I got error below

==========================================================


AttributeError Traceback (most recent call last)
in
----> 1 conv_model = convolutional_model((64, 64, 3))
2 conv_model.compile(optimizer=‘adam’,
3 loss=‘categorical_crossentropy’,
4 metrics=[‘accuracy’])
5 conv_model.summary()

in convolutional_model(input_shape)
48
49 # YOUR CODE ENDS HERE
—> 50 model = tf.keras.Model(inputs=input_img, outputs=outputs)
51 return model

/opt/conda/lib/python3.7/site-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)

/opt/conda/lib/python3.7/site-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

/opt/conda/lib/python3.7/site-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

/opt/conda/lib/python3.7/site-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

/opt/conda/lib/python3.7/site-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()

/opt/conda/lib/python3.7/site-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

/opt/conda/lib/python3.7/site-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: ‘Dense’ object has no attribute ‘op’

1 Like

It looks like the fundamental problem here is that you are using a mix of the Sequential Model (from the first section) and the Functional Model. But the two don’t mix. Here in the second section we are doing the Functional API, so there are no commas between the layers: each line is the invocation of a function with an input and an output. Then for some layers (e.g. Dense), you only instantiate the layer function, but don’t actually call it with an input, which is the source of that specific error message. But there is plenty else wrong here: if you only fix that, you’ll start getting dimension errors and the like.

The instructions here are not all that useful at explaining how the two Keras APIs work, but here’s a very useful thread from ai_curious that walks you through some examples of both and explains what is going on. Please have a look and that should clarify my comments above, if they don’t make sense the first time.

2 Likes

thanks. I resolved. It is caused by this problem in my code and it is ok now