[Week 2] Transfer learning with mobilenet - exercise 2

hi, I get this error regarding my second exercise on Assignment 2:
TypeError Traceback (most recent call last)
in
----> 1 model2 = alpaca_model(IMG_SIZE, data_augmentation)

in alpaca_model(image_shape, data_augmentation)
45 # YOUR CODE ENDS HERE
46
—> 47 outputs = prediction_layer(x)
48 model = tf.keras.Model(inputs, outputs)
49

TypeError: ‘Tensor’ object is not callable
this is a section of my code which I think the problem is:
x = data_augmenter()(inputs
x = preprocess_input(x)
x = base_model(x, training=False)
thanks in advance for helping out.

Hi amirrshams,

This is caused by defining prediction_layer as an application of a Dense layer to a tensor. In result, prediction_layer becomes a tensor object rather than a layer. This is why you get the error 'Tensor’ object is not callable, when trying to use prediction_layer as a layer.

So you have to redefine prediction_layer in such a way that the Dense layer is not yet applied to a tensor.

thanks reinoudbosch,
this the line which I’ve defined prediction_layer:
prediction_layer = tf.keras.layers.Dense(units = 1, activation = ‘linear’)(x)
Are you suggesting that the problem is with this line?

Hi amirrshams,

You can think about it like this: is the provided definition a layer or an application of a layer to a tensor, which outputs a tensor?

I suppose it’s the latter

hello again, I’ve still hadn’t found a way to solve this issue and I’m kinda stuck, can you please give me some other pointers?

Hi amirrshams,

Look at your definition of prediction_layer. How can you define prediction_layer as a layer and not as a tensor? Remember that applying a layer to a tensor gives a tensor.

Hope this helps.

Thank you so much, I cannot believe I missed that. I changed literally everything apart from the issue. it is solved now

1 Like