Exercise 2 - convolutional_model 'Dense' object has no attribute op

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)
39
40 # YOUR CODE ENDS HERE
—> 41 model = tf.keras.Model(inputs=input_img, outputs=outputs)
42 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’

sorry may i ask what the error mean? thanks!

Please click my name and message the code for the function def convolutional_model

Please see this link.

When following the functional approach to create a model, 2 steps are involved for each layer.

  1. Layer creation.
  2. Calling the layer.

This code

x = tf.keras.layers.Dense(units=1000)(previous_layer)

is the same as:

x = tf.keras.layers.Dense(units=1000) # creates new object
x = x(previous_layer) # invokes __call__ method of that object.

HI balaji, may i ask what is the previous layer?

i pass input image to Z1 and pass Z1 to Z2 ,and treate Z2 as the input layer is correct?

tf.keras.Input is an exception where you don’t need to call the layer.

previous_layer refers to the layer whose output is fed to this layer.

Getting back to the function, here are some hints to fix the code:

input_img = Input(shape=...)
Z1 = Conv2D(...)(input_img)
A1 = ReLU() (Z1)
# keep going.

Does this help?

1 Like

Hi balaji,
i did what you said but still error here

[code removed - moderator]

may i ask any mistake i made? thanks in advance

You did the Conv2D layers correctly and the pooling logic, but you’ve made the same mistake as earlier on the ReLU layers and the Flatten layer. Those also require the 2 step process that Balaji described above. All Keras “Layer” functions require the two levels of invocation.