Week 4 Assignmen 2 UNQ_C5: AttributeError: 'list' object has no attribute 'get_shape'

When running the cell after the exercice “UNQ_C5 # GRADED FUNCTION: train_step” in Assignment 2 of week 4 I get the following error:

    <ipython-input-100-e9e1eb98ee23>:19 train_step  *
        J_style = compute_layer_style_cost(a_S, a_G)
    <ipython-input-83-afd748e746db>:16 compute_layer_style_cost  *
        _, n_H, n_W, n_C = a_G.get_shape().as_list()

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

So the problem seems to be in how a_G is defined, which I believe there is just one way to implement it by following the instructions “Compute the encoding of the generated image using vgg_model_outputs. Assing the result to a_G”.

A hint will be very much appreciated.

First of all, please see this thread.

There are 6 outputs in a list. That’s a\_G. For the style cost calculation, we need to handle 5 outputs in a list. On the other hand, “compute_layer_style_cost()” is for one single layer (output) which is unpacked from a list. (In this sense, if you pass this list to “compute_layer_style_cost()”, then, the above error should occur.)
In stead, you need to call other function which actually calls “compute_layer_style_cost()” with unpacking a list.

Hope this helps.

1 Like

Thank for the quick answer. I see now that I was using the wrong funcion and the link was very useful to understand where this list of outputs comes from.

However after rebooting and rerunning I still get an error

TypeError: in user code:

    <ipython-input-26-bf53beb222ea>:19 train_step  *
        J_style = compute_style_cost(a_S, a_G)
    <ipython-input-14-d70c9653228b>:28 compute_style_cost  *
        J_style_layer = compute_layer_style_cost(a_S[i], a_G[i])
    <ipython-input-9-afd748e746db>:19 compute_layer_style_cost  *
        a_S = tf.transpose(tf.reshape(a_S,[n_H * n_W, n_C], [1,0]))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper  **
        return target(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:195 reshape
        result = gen_array_ops.reshape(tensor, shape, name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py:8234 reshape
        "Reshape", tensor=tensor, shape=shape, name=name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:353 _apply_op_helper
        with g.as_default(), ops.name_scope(name) as scope:
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:6492 __enter__
        return self._name_scope.__enter__()
    /usr/lib/python3.6/contextlib.py:81 __enter__
        return next(self.gen)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:4189 name_scope
        if not _VALID_OP_NAME_REGEX.match(name):

    TypeError: expected string or bytes-like object

Check the location of “close” parenthesis for tf.reshape(). It needs first two. Third one, [1,0], should be for tf.transpose() .

Thanks again for the very fast response. It worked. It is funny that I passed the test and got the right value for the “UNQ_C3, GRADED FUNCTION: compute_layer_style_cost” even with the issues with the parenthesis.

As you are using “Positional Argument”, an interpretation is what Tensorflow assumes. The third position for tf.Reshape is name which expects “string”. We do not know how [1,0] is handled in tf.Reshape, but, one big difference is “complete_layer_style_cost” is called from “train_step” which is inside “@tf.function”. This changes an execution mode from “Eager execution” to “Graph execution”, which runs faster. We can easily imagine that this additional parameter, [1,0], caused an error in a graph computation. You can put @tf.function in front of “def compute_layer_style_cost(a_S, a_G):” to see what will happen. I believe the same error occurs even by a local test. :wink: