Course 4 Week 4 Assignment 2 train_step (UNQ_C5)

I am currently stuck trying to implement the train_step() function for transfer learning. I am getting an AttributeError: ‘list’ object has no attribute ‘get_shape’.

I would very much appreciate some help.


This error comes from the compute_layer_style_cost(a_S, a_G), UNQ_C3 function. But I am wondering why this happens. This same code, as instructed in the notebook, “To retrieve dimensions from a tensor X, use: X.get_shape().as_list()”, is working in my case.

Are you changing something or have passed all the above tests?

I have passed all previous tests and I have not changed anything, not that I know of. I only added cells after the error so I could look at shapes and outputs.

Here is a screenshot of my compute_layer_style_cost() function:

{moderator edit - solution code removed}

It looks like your compute_layer_style_cost logic is correct, so there must be something wrong in the train_step logic that results in the a_G value being a list when it should be a tensor. A perfectly correct function can throw errors if you pass it arguments that don’t match its expectations. Please have a look at the train_step logic to understand what went wrong.

train_step() function is primarily calling other functions. So I’m guessing I should call another function for a_G. I’m posting the entire UNQ_C5 block and the error message. If nothing seems wrong, I will restart the assignment to ensure a more careful approach.

Right, so the question is where is a_G generated? Maybe start by printing the type of it right after the call to vgg_model_outputs. What does that show? Is it wrong on the first iteration or on some subsequent iteration? Then work your way backwards one step at a time. Are you sure you didn’t modify any of the cells that aren’t graded? You can also compare to a clean notebook to make sure (see the FAQ Thread topic on that).

Seeing the exact same error on this exercise:

I tried to verify if I have made a mistake in previous steps, but it didn’t seem so:

fwiw, changing from a_G = vgg_model_outputs(generated_image) to a_G = tf.Variable(vgg_model_outputs(generated_image)) seems to have updated the error message:

Try doing this instead:

print(f"type(a_G) {type(a_G)}")

But curiously when I do that, I get the type as “list” but it does not cause any kind of problem for me. There must be something different about your notebook. Please check your DMs for a message from me about how to proceed here.

Ok, we just need to look at how the code works. At the top level a_G is a list, but the elements of the list are tensors. Then compute_style_cost (which is written for us) extracts the individual layers and passes them individually to compute_style_layer_cost which is where the error was being thrown for @andreyrmacedo earlier on this thread.

Ok, one way to get the error about a_G being a list is to call compute_layer_style_cost directly from train_step, instead of calling compute_style_cost. Note that in the style case, there are multiple layers used and the job of compute_style_cost (which was just provided for you) is to “peel off” the individual style layers and call compute_layer_style_cost for each one.