Week 1 Programming Assignment 2 Error when adding dense layer to the sequential model

In the happyModel() function while adding the layers in the sequential model, all the layers are added successfully but only the Dense layer gives the following error:
TypeError: The added layer must be an instance of class Layer. Found: tf.Tensor(

This is my code line for Dense layer:
tfl.Dense(1, activation = ‘sigmoid’)

Please tell how to resolve this

2 Likes

Hello Ani,
Hope you are doing well.

Can you please reply to this thread with a screenshot of the relevant code so I can help you better?
(Please note that I will ask you to delete it when we solve the problem).

Regards,

LinkedIn

Here is the snapshot of my code:
Code

Here is the error:

Hello Ani,

I am really sorry for the late reply, is your problem solved yet?

Regards,

LinkedIn

I’m heaving the same problem, Looked in the API. But have some examples that show the same usage as described.

I am having the same problem. Tried quite a few ways to solve the problem, but nothing is working.

Metioning the ZeroPadding2D part like
tfl.ZeroPadding2D(padding=3, input_shape=(64,64,3))
worked for me.
I did not mention the shape part anywhere else.

4 Likes

Worked for me too the dense part isok too. Got a Warning:
WARNING:tensorflow:Layer conv2d is casting an input tensor from dtype float64 to the layer’s dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because its dtype defaults to floatx.

If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.

To change all layers to have dtype float64 by default, call tf.keras.backend.set_floatx('float64'). To change just this layer, pass dtype=‘float64’ to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.

But i already done what the warning said.

Thank you @akksh01

1 Like


HELP IM HAVING TROUBLE WITH MY TRANSFORMER PROGRAMMING ASSIGNMENT DLS COURSE 5

I found the same error like your.
I code this " tf.keras.Input(shape=(64,64,3)) "
before the layer tfl.ZeroPadding2D and it’s work for me.

1 Like

Yes I tried the solution provided by @akksh01 given and it worked. Thanks

2 Likes

Thanks a lot, worked for me!