Error in UNQ_C5

I have a strange error while implementing training_step in the second assignment of 4th week (Art_Generation_with_Neural_Style_Transfer).

When the code is tested I am getting following error (please see below)

If I am trying to print a_G in the function it gives me the following:

a_G: [<tf.Tensor 'model_3/block1_conv1/Relu:0' shape=(1, 400, 400, 64) dtype=float32>, <tf.Tensor 'model_3/block2_conv1/Relu:0' shape=(1, 200, 200, 128) dtype=float32>, <tf.Tensor 'model_3/block3_conv1/Relu:0' shape=(1, 100, 100, 256) dtype=float32>, <tf.Tensor 'model_3/block4_conv1/Relu:0' shape=(1, 50, 50, 512) dtype=float32>, <tf.Tensor 'model_3/block5_conv1/Relu:0' shape=(1, 25, 25, 512) dtype=float32>, <tf.Tensor 'model_3/block5_conv4/Relu:0' shape=(1, 25, 25, 512) dtype=float32>]

When, a_G = vgg_model_outputs(generated_image)

I tried to print a_S and a_C they looks like an image tensor. Moreover if I am trying to print the a_G before the function call it gives me tensor of an image

print(generated_image)
generated_image = tf.Variable(generated_image)
a_G = vgg_model_outputs(generated_image)
print("a_G: ", a_G)

@tf.function()
def train_step(generated_image):

The result of the above:

<tf.Variable 'Variable:0' shape=(1, 400, 400, 3) dtype=float32, numpy=
array([[[[0.2626779 , 0.30240813, 0.4622658 ],
         [0.04750122, 0.2796671 , 0.23306447],
         [0.        , 0.3454881 , 0.46654248],
         ...,
         [0.03353901, 0.18342546, 0.33278564],
         [0.27362907, 0.16752036, 0.35444131],
         [0.        , 0.        , 0.56736994]],

I am not sure what I am doing wrong. It looks like that getting a_G as I described above is what expected and there is some issue within the exercise itself.

The error I am getting.

AttributeError                            Traceback (most recent call last)
Input In [180], in <cell line: 7>()
      1 ### you cannot edit this cell
      2 
      3 # You always must run the last cell before this one. You will get an error if not.
      5 generated_image = tf.Variable(generated_image)
----> 7 train_step_test(train_step, generated_image)

File /tf/W4A2/public_tests.py:86, in train_step_test(target, generated_image)
     82 def train_step_test(target, generated_image):
     83     generated_image = tf.Variable(generated_image)
---> 86     J1 = target(generated_image)
     87     print(J1)
     88     assert type(J1) == EagerTensor, f"Wrong type {type(J1)} != {EagerTensor}"

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File /tmp/__autograph_generated_fileaddlw7_p.py:13, in outer_factory.<locals>.inner_factory.<locals>.tf__train_step(generated_image)
     11 a_G = ag__.converted_call(ag__.ld(vgg_model_outputs2), (ag__.ld(generated_image),), None, fscope)
     12 ag__.ld(print)('a_G: ', ag__.ld(a_G))
---> 13 J_style = ag__.converted_call(ag__.ld(compute_layer_style_cost), (ag__.ld(a_S), ag__.ld(a_G)), None, fscope)
     14 J_content = ag__.converted_call(ag__.ld(compute_content_cost), (ag__.ld(a_C), ag__.ld(a_G)), None, fscope)
     15 J = ag__.converted_call(ag__.ld(total_cost), (ag__.ld(J_content), ag__.ld(J_style)), dict(alpha=10, beta=40), fscope)

File /tmp/__autograph_generated_fileb_9z_f0x.py:11, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_layer_style_cost(a_S, a_G)
      9 do_return = False
     10 retval_ = ag__.UndefinedReturnValue()
---> 11 (_, n_H, n_W, n_C) = ag__.converted_call(ag__.converted_call(ag__.ld(a_G).get_shape, (), None, fscope).as_list, (), None, fscope)
     12 a_S = ag__.converted_call(ag__.ld(tf).transpose, (ag__.converted_call(ag__.ld(tf).reshape, (ag__.ld(a_S),), dict(shape=[(- 1), ag__.ld(n_C)]), fscope),), None, fscope)
     13 a_G = ag__.converted_call(ag__.ld(tf).transpose, (ag__.converted_call(ag__.ld(tf).reshape, (ag__.ld(a_G),), dict(shape=[(- 1), ag__.ld(n_C)]), fscope),), None, fscope)

AttributeError: in user code:

    File "<ipython-input-179-3cad69e780ba>", line 28, in train_step  *
        J_style = compute_layer_style_cost(a_S, a_G)
    File "<ipython-input-141-684f1f46a6c5>", line 17, in compute_layer_style_cost  *
        _, n_H, n_W, n_C = a_G.get_shape().as_list()

    AttributeError: 'list' object has no attribute 'get_shape'

The error is that you are calling the wrong function from train_step. compute_style_layer_cost needs to be called by compute_style_cost, right? But you are calling it directly from train_step.

Please look at the definition and purpose of the two functions.

compute_style_layer_cost computes the style cost for just one “style” layer, but the whole point of the way we handle the style is that it is computed from a list of selected layers and that is the purpose of the function compute_style_cost, which they wrote for us. You may have skipped over that, since you didn’t have to write that code, but it’s still critical to understand the overall point of how the style is defined here.

Hi, Paul.

Thanks a lot for replying me! You’re totally right. All tests have passed now. I also missed out from the beginning that the result of calling “vgg_model_outputs” is not a single image tensor but an array of them. And, yes, this is the whole idea to have multiple layers output and to get a total cost from all layers. My bad, there are no errors in the exercise.

I passed it without this exercise but I just was curious what is wrong in my understanding. Many thanks for clarifying and very fast response!

Hi, Sergey.

Glad to hear that you’ve gotten the full score now. It’s good to get the clear understanding of what’s happening here.

So you’ve completed DLS Course 4. Congratulations! Are you planning to move on to Course 5? There is plenty of very interesting material there that explains the technology on which LLMs are based. Definitely worth your time!