TypeError: 'function' object is not subscriptable

When creating a post, please add:
Week 2 Assignment 2 Excercise 2 alpaca_model
Transfer_learning_with_MobileNet_v1
I am getting the following error.
why shouldnt the input_shape be same as image_shape?
what is the meaning of ‘Function’ object is not subscriptable???


Incase there already exists a post for this please let me know unable to find.
Incase I am doing anything against our policies please alert me

Also this the original error i had

I got it.
Solution:

  1. I went through all the related queries on this community
  2. looked at the entire code properly and could identify:

a) I was giving wrong input to input_shape
b) I also learnt about augmentation part from the forums and corrected it
c) used the documentation to correct some format issues and resolved it

Thanks again to the community :slightly_smiling_face:

2 Likes

This is an important point here, use the documentation when in doubt!

That’s great to hear that you were able to take advantage of both the forum content and the TF docs to solve the problems here. Nice work!

For anyone else who’s having issues with the Sequential or Functional Keras models in general, here’s a recommended forum thread to read.

From a quick peek at the code on github, seems the call to tf.keras.applications.MobileNetV2() acts like a Factory pattern; it instantiates and configures a Functional model under the covers. But it doesn’t expose the Sequential or Functional syntax to the user. It’s just a function call to the virtual constructor.


def MobileNetV2(…):

…

# Create model.
   model = Functional(inputs, x, 
   name=f"mobilenetv2_{alpha:0.2f}_{rows}")

…

    return model

Looks like the OP figured this out, but for those of you reading this later, the error message ____ is not subscriptable means the code is attempting an indexed lookup on an object of a type that doesn’t support that style of access. Meaning it probably isn’t iterable ie a Collection. In this code, according to the documentation of the MobilenetV2 function, the input_shape argument is expected to be a tuple with exactly 3 elements representing width, height, and channels, the last of which must have the value of 3. A tuple in Python is iterable, and does support indexed (subscripted) lookup.

In the first error, image_shape, which is assigned to input_shape in the call, does seem to be a tuple, but not one with 3 values (the first error message specifies the incorrect length). In the second error, where tf.keras.Input is assigned to input_shape, the error is because tf.keras.Input is a function that creates an InputLayer object instance, and functions in Python are not tuples, are not iterable, and don’t support indexed lookup. Hope this helps.

Ps: a confirmation of the above would be to examine the details of image_shape. If my explanation is valid, it won’t be of shape (width, height, channels).

3 Likes