Course4 Week4 Assignment 2 - tensor shape mismatch in UNQ_C5 when computing content cost

In the UNQ_C5, the error happens at ‘compute_content_cost’.
My codes is:

{Moderator Edit: Solution Code Removed

The error message seems to tell the dimension mismatch at computing the content cost.
I do no know why that is because I have passed all the tests before. Here is just to call the functions.

ValueError                                Traceback (most recent call last)
Input In [27], 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_file3_m9ze0o.py:13, in outer_factory.<locals>.inner_factory.<locals>.tf__train_step(generated_image)
     11 a_G = ag__.converted_call(ag__.ld(vgg_model_outputs), (ag__.ld(generated_image),), None, fscope)
     12 J_style = ag__.converted_call(ag__.ld(compute_style_cost), (ag__.ld(a_S), ag__.ld(a_G)), None, fscope)
---> 13 J_content = ag__.converted_call(ag__.ld(compute_content_cost), (ag__.ld(a_C), ag__.ld(a_G)), None, fscope)
     14 J = ag__.converted_call(ag__.ld(total_cost), (ag__.ld(J_content), ag__.ld(J_style)), dict(alpha=10, beta=40), fscope)
     15 ag__.ld(print)(f'J_style: {ag__.ld(J_style)}; J_content: {ag__.ld(J_content)}; J: {ag__.ld(J)}')

File /tmp/__autograph_generated_filemdpp3woj.py:14, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_content_cost(content_output, generated_output)
     12 a_G = ag__.ld(generated_output)[(- 1)]
     13 (m, n_H, n_W, n_C) = ag__.converted_call(ag__.converted_call(ag__.ld(a_G).get_shape, (), None, fscope).as_list, (), None, fscope)
---> 14 a_C_unrolled = ag__.converted_call(ag__.ld(tf).reshape, (ag__.ld(content_output), [ag__.ld(m), (- 1), ag__.ld(n_C)]), None, fscope)
     15 a_G_unrolled = ag__.converted_call(ag__.ld(tf).reshape, (ag__.ld(generated_output), [ag__.ld(m), (- 1), ag__.ld(n_C)]), None, fscope)
     16 J_content = (ag__.converted_call(ag__.ld(tf).reduce_sum, (ag__.converted_call(ag__.ld(tf).square, ((ag__.ld(a_C_unrolled) - ag__.ld(a_G_unrolled)),), None, fscope),), None, fscope) / (((4.0 * ag__.ld(n_H)) * ag__.ld(n_W)) * ag__.ld(n_C)))

ValueError: in user code:

    File "<ipython-input-26-1ee3c8cd3309>", line 24, in train_step  *
        J_content = compute_content_cost(a_C, a_G)
    File "<ipython-input-4-ffc20a831bc3>", line 24, in compute_content_cost  *
        a_C_unrolled = tf.reshape(content_output, [m, -1, n_C])

    ValueError: Tried to convert 'tensor' to a tensor and failed. Error: Dimension 1 in both shapes must be equal, but are 50 and 25. Shapes are [1,50,50,512] and [1,25,25,512].
    	From merging shape 3 with other shapes. for '{{node Reshape_10/packed}} = Pack[N=6, T=DT_FLOAT, axis=0](Reshape_10/tensor/values_0, Reshape_10/tensor/values_1, Reshape_10/tensor/values_2, Reshape_10/tensor/values_3, Reshape_10/tensor/values_4, Reshape_10/tensor/values_5)' with input shapes: [1,400,400,64], [1,200,200,128], [1,100,100,256], [1,50,50,512], [1,25,25,512], [1,25,25,512].

Your code is correct. And as you say you have passed all the above tests, I can come up with one theory: Have you changed any code of the ungraded cell?

In addition, try Kernel → Restart and Clear Output and then run all the cells.

Best,
Saif.

PS: Sharing your code is not allowed. I am deleting it after this reply. Next time, only share your full error.

Sorry for sharing the codes. I have restarted the kernel several times, but the results remain unchanged. I did not remember I have changed the ungraded cell. Is there any method to recover the original codes?

Try getting the clean copy of your assignment as described here and then copy your code from the old notebook carefully without changing the other code.

I have restarted with the clean copy. But the error still occurs with all the blocks before passing the tests. The error message was exactly as same as before. I have submitted it for grading. Here is the output:

Code Cell UNQ_C1: Function 'compute_content_cost' is correct.
Code Cell UNQ_C2: Function 'gram_matrix' is correct.
Code Cell UNQ_C3: Function 'compute_layer_style_cost' is correct.
Code Cell UNQ_C4: Function 'total_cost' is correct.
Code Cell UNQ_C5: Unexpected error (ValueError('in user code:\n\n    File "/tmp/student_solution_cells/cell_26.py", line 50, in train_step  *\n        J_content = compute_content_cost(a_C, a_G)\n    File "/tmp/student_solution_cells/cell_4.py", line 29, in compute_content_cost  *\n        a_C_unrolled = tf.reshape(content_output, [m, -1, n_C])\n\n    ValueError: Tried to convert \'tensor\' to a tensor and failed. Error: Dimension 1 in both shapes must be equal, but are 50 and 25. Shapes are [1,50,50,512] and [1,25,25,512].\n    \tFrom merging shape 3 with other shapes. for \'{node Reshape_10/packed} = Pack[N=6, T=DT_FLOAT, axis=0](Reshape_10/tensor/values_0, Reshape_10/tensor/values_1, Reshape_10/tensor/values_2, Reshape_10/tensor/values_3, Reshape_10/tensor/values_4, Reshape_10/tensor/values_5)\' with input shapes: [1,400,400,64], [1,200,200,128], [1,100,100,256], [1,50,50,512], [1,25,25,512], [1,25,25,512].\n')) occurred during function check. We expected function `train_step` to return train_step test 1 failed. Please check that this function is defined properly. 
If you see many functions being marked as incorrect, try to trace back your steps & identify if there is an incorrect function that is being used in other steps.
This dependency may be the cause of the errors.

Please send me your code of compute_content_cost function in a private message. Click my name and message.

It turns out that there was some error in compute_content_cost function. When implementing a_C_unrolled and a_G_unrolled, it is given that "Reshape a_C and a_G", not to reshape the content_output and generated_output.

1 Like

Hi Saif,

I am having same error. Can you please help me too? Thanks

Please show us the error message that you are getting.

Code Cell UNQ_C5: Unexpected error (ValueError('in user code:
File "/tmp/student_solution_cells/cell_27.py", line 48, in train_step  *
        J_style = compute_layer_style_cost(a_S, a_G)
File "/tmp/student_solution_cells/cell_10.py", line 30, in compute_layer_style_cost  *
        a_S = tf.reshape(tf.transpose(a_S, perm=[0,3,1,2]), shape=[n_C, n_H * n_W])
ValueError: Dimension 1 in both shapes must be equal, but are 50 and 25. Shapes are [1,50,50,512] and [1,25,25,512].
    \tFrom merging shape 3 with other shapes. for \'{node transpose/a} = Pack[N=6, T=DT_FLOAT, axis=0](transpose/a/values_0, transpose/a/values_1, transpose/a/values_2, transpose/a/values_3, transpose/a/values_4, transpose/a/values_5)\' with input shapes: [1,400,400,64], [1,200,200,128], [1,100,100,256], [1,50,50,512], [1,25,25,512], [1,25,25,512].\n')) occurred during function check. We expected function `train_step` to return train_step test 1 failed. Please check that this function is defined properly. 
If you see many functions being marked as incorrect, try to trace back your steps & identify if there is an incorrect function that is being used in other steps.
This dependency may be the cause of the errors.

It looks like there are some dimension mismatch problems in your compute_layer_style_cost logic. Are you sure that all your previous functions passed the local tests?

Hey Paul, Thanks for your response.

Here is the grader output:

Code Cell UNQ_C1: Function 'compute_content_cost' is correct.
Code Cell UNQ_C2: Function 'gram_matrix' is correct.
Code Cell UNQ_C3: Function 'compute_layer_style_cost' is correct.
Code Cell UNQ_C4: Function 'total_cost' is correct.
Code Cell UNQ_C5: Unexpected error (ValueError('in user code:\n\n    File "/tmp/student_solution_cells/cell_27.py", line 48, in train_step  *\n        J_style = compute_layer_style_cost(a_S, a_G)\n    File "/tmp/student_solution_cells/cell_10.py", line 30, in compute_layer_style_cost  *\n        a_S = tf.reshape(tf.transpose(a_S, perm=[0,3,1,2]), shape=[n_C, n_H * n_W])\n\n    ValueError: Dimension 1 in both shapes must be equal, but are 50 and 25. Shapes are [1,50,50,512] and [1,25,25,512].\n    \tFrom merging shape 3 with other shapes. for \'{node transpose/a} = Pack[N=6, T=DT_FLOAT, axis=0](transpose/a/values_0, transpose/a/values_1, transpose/a/values_2, transpose/a/values_3, transpose/a/values_4, transpose/a/values_5)\' with input shapes: [1,400,400,64], [1,200,200,128], [1,100,100,256], [1,50,50,512], [1,25,25,512], [1,25,25,512].\n')) occurred during function check. We expected function `train_step` to return train_step test 1 failed. Please check that this function is defined properly. 
If you see many functions being marked as incorrect, try to trace back your steps & identify if there is an incorrect function that is being used in other steps.
This dependency may be the cause of the errors.

Why are you using the “perm” parameter?
Also check if you are transposing and reshaping in the correct order.
Also the shape dimensions may be reversed.

To close the loop on the public thread: we had a DM conversation to check the code. It turns out there was a bug in train_step: the code was using the single “layer” version of the style cost function, instead of the full version that internally iterates over the selected layers.

1 Like