W4 - A2 - Ex3

My code beeing returning the wrong value:
29.146091 instead of the expected 14.01649

I don’t know what i’m doing wrong.

that’s how i’m reshaping the tensors:

{moderator edit - solution code removed}

and that’s how i’m calculating J_style_layer:

{moderator edit - solution code removed}

Here’s what the comment says before the transpose + reshape line:

# Reshape the tensors from (1, n_H, n_W, n_C) to (n_C, n_H * n_W) (≈2 lines)

What is the final shape of your result? Does it agree with what they asked for?

It wasn’t matching the shape, i just fixed now, but still the final result is wrong,
2.9225328 instead of the expected 14.01649

Double-check the equation for J_style_layer:

J_{style}^{[l]}(S,G) = \frac{1}{4 \times {n_C}^2 \times (n_H \times n_W)^2} \sum _{i=1}^{n_C}\sum_{j=1}^{n_C}(G^{(S)}_{(gram)i,j} - G^{(G)}_{(gram)i,j})^2

So, apparently i’m not subtracting all elements of GS with all elements of GG, just the ones where the index matches, but i’m not sure how to do that (without loops of course).

You don’t need to use for loop. Check the denominator.

Denominator is giving me 9216, isn’t that correct ?

I don’t know the value but compare your implementation with the equation given to you in the notebook.

That’s the incorrect answer you get if you just directly reshape to the required shape without a transpose. That doesn’t work because it scrambles the data as explained on this thread.

So your previous code was actually closer, other than that you had the dimensions reversed. :nerd_face:

So, just to clarify, i was making something wrong during the reshaping, i inverted the order (did the transpose, then reshaped) and it worked. The Thread you cited helped a lot