C4W4A2 -- cannot get the correct "compute_layer_style_cost"

In the second program assignment of C4W4 ( “Art_Generation_with_Neural_Style_Transfer” ), for some reason, my “compute_layer_style_cost” cannot pass unit test and got the error "assert np.isclose(J_style_layer_SG, 14.017805), “Wrong value.” from testing “compute_layer_style_cost”, while J_style_layer_SG from my output is “<tf.Tensor: shape=(), dtype=float32, numpy=2.920395>”.
I have looked into the method and tested it line by line multiple times.

Here is my code for “compute_layer_style_cost”. Can anyone please take a quick look and check if anything goes wrong? Thank you!

{moderator edit - solution code removed}

The problem is that you are just directly reshaping the tensors to the shape you want. That does not work, because it “scrambles” the data. You need to do it in two steps:

  1. Reshape from (n_H, n_W, n_C) to (n_H * n_W, n_C).
  2. Then transpose that to get the final result.

They give hints about this in the instructions and then in the pictures, but they aren’t as explicit about it as perhaps they should have been. The reason for this is similar to why the flattening of the images in Course 1 Week 2 requires the transpose. Here’s a thread about that particular case. The high level point is that just because the shapes end up the same does not mean the results are equivalent. It matters exactly how you do it.

Update: here’s a more recent thread that shows the analogous experiment to the previous link, but it’s specific to the case here in C4 W4 A2. The link points to the experiment, but the earlier parts of thread are worth a look as well.

3 Likes

Thank you for the info, Paul! Fixed it.

Hello Paul,

I seem to have done that only:

  1. first reshaping to (n_H*n_W,n_C) and then transposing it to (n_C,n_H*n_W)
  2. subtracting the 2 gram matrices then squaring and subsequently adding all the elements followed by division with (4*(n_H*n_W*n_C)^2)

But still I’m not getting the correct answer to pass the tests.
Can you point out the issue here?

Note that exponentiation in python is **, right? ^ is exclusive OR.

You are right. I made a blunder there.
Was stuck for long. Thank you so much for your prompt reply.

I wasn’t sure whether you meant the ^ as the code you actually wrote, but thought it was worth a guess. It’s one of the things that’s hard when you switch from MATLAB to python, but ** is common in other languages like c and C++. :nerd_face:

Thanks. I got really stuck on this. Summary to help others: if you’re error is your ~2.9 return isn’t matching the assertion of ~14, you’re prolly ordering wrong. paulinpaloalto put it well, but to provide an example:
given matrix of shape (x,y,z) you can reshape to matrix (x,yz) or matrix (x,zy). Same dimensions, different matrices.

General example below:
X = np.reshape(np.arange(1,25), [2,3,4])
Y = np.reshape(X, [4,6])
Z = np.reshape(X, [6,4]).T