Week 1 assignment 2 convolutional_model

I searhed but can’t find anything
please help me with it

ValueError 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)
37 # YOUR CODE STARTS HERE
38 Z1=tfl.Conv2D(filters=8,kernel_size=4,strides=1,padding=‘same’,input_shape=“input_img”)
—> 39 A1=tfl.ReLU()(Z1)
40 P1=tfl.MaxPool2D(pool_size=(8,8), strides=8, padding=‘same’)(A1)
41 Z2=tfl.Conv2D(16,2,strides=1,padding=‘same’)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)
983
984 with ops.enable_auto_cast_variables(self._compute_dtype_object):
→ 985 outputs = call_fn(inputs, *args, **kwargs)
986
987 if self._activity_regularizer:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/advanced_activations.py in call(self, inputs)
367 alpha=self.negative_slope,
368 max_value=self.max_value,
→ 369 threshold=self.threshold)
370
371 def get_config(self):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 “”“Call target, and fall back on dispatchers if there is a TypeError.”“”
200 try:
→ 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/backend.py in relu(x, alpha, max_value, threshold)
4562 clip_max = False
4563 else:
→ 4564 x = nn.relu(x)
4565
4566 if clip_max:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_nn_ops.py in relu(features, name)
10421 try:
10422 return relu_eager_fallback(

10423 features, name=name, ctx=_ctx)
10424 except _core._SymbolicException:
10425 pass # Add nodes to the TensorFlow graph.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_nn_ops.py in relu_eager_fallback(features, name, ctx)
10455
10456 def relu_eager_fallback(features, name, ctx):

10457 _attr_T, (features,) = _execute.args_to_matching_eager([features], ctx)
10458 _inputs_flat = [features]
10459 _attrs = (“T”, _attr_T)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in args_to_matching_eager(l, ctx, default_dtype)
261 ret.append(
262 ops.convert_to_tensor(
→ 263 t, dtype, preferred_dtype=default_dtype, ctx=ctx))
264 if dtype is None:
265 dtype = ret[-1].dtype

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1497
1498 if ret is None:
→ 1499 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1500
1501 if ret is NotImplemented:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
336 as_ref=False):
337 _ = as_ref
→ 338 return constant(v, dtype=dtype, name=name)
339
340

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
262 “”"
263 return _constant_impl(value, dtype, shape, name, verify_shape=False,
→ 264 allow_broadcast=True)
265
266

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
273 with trace.Trace(“tf.constant”):
274 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
→ 275 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
276
277 g = ops.get_default_graph()

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
298 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
299 “”“Implementation of eager constant.”“”
→ 300 t = convert_to_eager_tensor(value, ctx, dtype)
301 if shape is None:
302 return t

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
—> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100

ValueError: Attempt to convert a value (<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7fb41243a310>) with an unsupported type (<class ‘tensorflow.python.keras.layers.convolutional.Conv2D’>) to a Tensor.

Your first line invoking tfl.Conv2D is incorrect, because you just instantiate the Conv2D, but don’t feed it an input tensor. So the result is a function which gets assigned to Z1. Then the next line throws an error because you’d tried to feed a function Z1 as the input to the ReLU() layer.

This is the Functional API, not the Sequential API, so everything is explicit: you have to specify the inputs and outputs.

1 Like

can you please help me out with how to pass input shape to Conv2D.

In the sequential mode, you don’t pass an input shape to Conv2D.
The shape is derived from the data you pass to the input layer.

1 Like

If you take the model given in the statement, you can see that it’s necessary to give a matrix as a parameter after having detailed the arguments: the (input_img) part is important.

tf.keras.layers.Conv2D(filters= … , kernel_size= … , padding=‘same’)(input_img):

1 Like