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!
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:
Reshape from (n_H, n_W, n_C) to (n_H * n_W, n_C).
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.
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++.
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