C4 week4 Deep Learning & Art: Exercise 3 - compute_layer_style_cost

Week 4, Programing assignment Deep Learning & Art, Exercise 3 - compute_layer_style_cost

I’ve checked my code several times, and the dimensions of the GS and GG are correct. But somehow, my result doesn’t match the correct answer!!!

I got 2.9225328 for J_style_layer_SG, but the correct value is 14.01649.

tf.Tensor(2.9225328, shape=(), dtype=float32)
"---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Input In [19], in <cell line: 3>()
1 ### you cannot edit this cell
----> 3 compute_layer_style_cost_test(compute_layer_style_cost)

File /tf/W4A2/public_tests.py:57, in compute_layer_style_cost_test(target)
55 assert np.isclose(J_style_layer_GG, 0.0), “Wrong value. compute_layer_style_cost(A, A) must be 0”
56 assert J_style_layer_SG > 0, “Wrong value. compute_layer_style_cost(A, B) must be greater than 0 if A != B”
—> 57 assert np.isclose(J_style_layer_SG, 14.01649), “Wrong value.”
59 print(“J_style_layer = " + str(J_style_layer_SG))
60 print(”\033[92mAll tests passed")

AssertionError: Wrong value."

General tips:

  • The statement that computes J_style_layer has a lot of complicated sets of parenthesis. Check that you have everything matched up correctly.

  • Also check that you’re using a_S and a_G in the correct places.

Thanks for the hint.
I stared at every steps of the matrix transformation; adjusted the code and it finally worked.

Hi,
I have the same mistake than you describe with your result.

Is this code for the reshape ?

a_S = tf.reshape(a_S, shape=[n_C, n_H * n_W])

Thank you

That’s not the same as the code I used. Maybe it’s equivalent, I have not tested it in detail.

If that’s all the code you have, then no, that is not correct. The point is that you can’t just reshape directly to the final shape you want, because that ends up scrambling the data. Here’s a thread which explains why that doesn’t work and suggests the correct way to do it. You might want to read the earlier part of that thread to get the full context for what that last post is doing.

Thanks for your answers paulinpaloalto and TMosh.
You help me solving my issue.
I forgot to use tf.transpose because I haven’t understood why I must use it !
Now, all is clear.
Have a nice day !

3 Likes