Computing layer style cost function

Hi all,
This is how I’m computing my layer cost function:

{moderator edit - solution code removed}

yet the test gives me this error:


AssertionError Traceback (most recent call last)
in
** 11 assert np.isclose(J_style_layer_GG, 0.0), “Wrong value. compute_layer_style_cost(A, A) must be 0”**
** 12 assert J_style_layer_SG > 0, “Wrong value. compute_layer_style_cost(A, B) must be greater than 0 if A != B”**
—> 13 assert np.isclose(J_style_layer_SG, 14.017805), "Wrong value."
** 14 **
** 15 print("J_style_layer = " + str(J_style_layer_SG))**

AssertionError: Wrong value.

Can someone help me with understanding where I’m coding wrong please?

Thanks

The problem is that you are just doing the reshape directly to the desired final shape. That does not work, because it scrambles the data. You need to do it in two steps: first a reshape, then a transpose. They discuss this in the instructions, but perhaps they are not as explicit about it as they should be. The reason for the scrambling is the same as the reason we needed a transpose when we “flattened” our images in Course 1 Week 2. Here’s a thread which discusses that and demonstrates why the transpose is required.

Update: here’s a more recent thread that shows a very specific example for the given case here of the right and wrong ways to do the reshaping.

2 Likes