Failed to train on Colab - C1W4_Assignment

I got error when,epochs=10) was finally added on Colab for training. The error message is: TypeError: Exception encountered when calling layer “my_vgg_21” (type MyVGG). Could anyone please help me to solve the problem?

You must be doing something wrong somewhere in your code, did you pass the grader for the submission part?

Hello -

At the risk of resurrecting an obsolete conversation, I recently experienced this issue & wondered if anyone had insights. In the C1W4 assignment, the last block (commented out to spare the grader) trains the VGG model used in the assignment. The suggested approach is to upload your notebook to Colab, comment out the grader utils & uncomment the training block to complete training. My notebook passed the grader with 100% so I expected no drama. However, the fit line triggers, epochs=10)
TypeError: Exception encountered when calling layer ‘my_vgg’ (type MyVGG).
TypeError: Exception encountered when calling layer ‘block_1’ (type Block)
TypeError: Inputs to a layer should be tensors. Got ‘<keras.layers.pooling.max_pooling2d.MaxPooling2D object at 0x7f80f5396500>’ (of type <class ‘keras.layers.pooling.max_pooling2d.MaxPooling2D’>) as input for layer ‘conv2d_2’.
The full Python trace is available if helpful. Often Python traces are misleading or cryptic. Any insights into why a “working” notebook in Coursera would not work in Colab? Mismatched TensorFlow versions perhaps?

Thanks, Matthew

I think perhaps that might be the issue too, Mathew.

Quick check using:

print(tf. version)





So perhaps the next question is how to best accommodate Coursera’s outdated TF usage when migrating to Colab.


You have to go on it step by step and if problems arise give solution to the problem and move on to the next step, I dont think you can find a one do all trick for this.

Is there a way to “force” a colab session to a particular TF version? thanks, matthew

Oh yes for eg. !pip install tensorflow ===2.8.0

Hi - Appreciate your insights. I tried uninstalling Colab TensorFlow(2.12.0) & installing the Coursera version (2.1.0). No joy! Colab couldn’t find a package satisfying 2.1.0. Is there perhaps a search path that needs to be configured? My thought was to quickly confirm the commented Coursera code operates correctly if the 2.1.0 TF version is installed. Perhaps this is not a productive approach. Thoughts? Thanks, Matthew

!pip uninstall tensorflow

Found existing installation: tensorflow 2.12.0 Uninstalling tensorflow-2.12.0: Would remove: /usr/local/bin/estimator_ckpt_converter /usr/local/bin/import_pb_to_tensorboard /usr/local/bin/saved_model_cli /usr/local/bin/tensorboard /usr/local/bin/tf_upgrade_v2 /usr/local/bin/tflite_convert /usr/local/bin/toco /usr/local/bin/toco_from_protos /usr/local/lib/python3.10/dist-packages/tensorflow-2.12.0.dist-info/* /usr/local/lib/python3.10/dist-packages/tensorflow/* Proceed (Y/n)? Y Successfully uninstalled tensorflow-2.12.0

!pip install tensorflow==2.1.0

Looking in indexes:, ERROR: Could not find a version that satisfies the requirement tensorflow==2.1.0 (from versions: 2.8.0rc0, 2.8.0rc1, 2.8.0, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.9.0rc0, 2.9.0rc1, 2.9.0rc2, 2.9.0, 2.9.1, 2.9.2, 2.9.3, 2.10.0rc0, 2.10.0rc1, 2.10.0rc2, 2.10.0rc3, 2.10.0, 2.10.1, 2.11.0rc0, 2.11.0rc1, 2.11.0rc2, 2.11.0, 2.11.1, 2.12.0rc0, 2.12.0rc1, 2.12.0, 2.13.0rc0, 2.13.0rc1) ERROR: No matching distribution found for tensorflow==2.1.0

I have not tried this process myself Mathew, but as I said go on it step by step and tackle each error as it comes along if possible…

synnyay, the type error you experienced in Colab for the Week 4 assignment may actually be caused by an error in your class definition for ‘Block’ (it was in my case).

For the MaxPool2D layer defined in the Block class’s call function, did you remember to pass ‘x’ into the pool from the previous line of code? Remember, you are using the Functional API here. If you leave out the input shape, you will get an error in Colab–even though your code may pass the grader in the Coursera notebook.

In my case, the code ran fine in Colab (after I commented-out the utils-related items as described in the instructions). I don’t think that differing TensorFlow versions (Coursera vs. Colab) caused your problem. In my case, I did nothing to change the TensorFlow version in Colab and the code ran fine once I fixed the error with the pool.