Week 4 - Assignment 2 - Neural Art - Exercise 3

Hi guys,

I am running into trouble when computing the function: compute_layer_style_cost(a_S, a_G).
My implementation of the Gram matrix is correct and I simply implement the loss function with the tf functions, however I get an error stating that it the value is wrong. I’ve double checked the size of the a_S and a_G matrices which should be (3,16) based on the test values which is correct. I am not sure where I am going wrong here

tf.Tensor(2.9203947, shape=(), dtype=float32)

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

AssertionError: Wrong value.

Help would be much appreciated!


Can you please post your code of the function, so that we can see where the issue exactly lies. And you can remove the code once your query is resolved.

I guess the error is in these lines of code

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

The original shape is (1, n_h, n_w, n_c), so you will have to use tf.transpose as well in order to get the dimensions in the right order.

a_S = tf.transpose(tf.reshape(a_S, shape=(-1, n_C)), perm=[1, 0])
a_G = tf.transpose(tf.reshape(a_G, shape=(-1, n_C)), perm=[1, 0])

Try using these lines of code instead.
P.S. - tf.square worked just fine in my case.


This worked, thanks very much!

Posting code breaks the course Honor Code. Please don’t ask other students to do that.

I used transpose to reorder the last 3 dimensions (with 0 3 1 2) and only then reshape to get the right shape and it did the trick too! :slight_smile:

Sorry Sir, I am well aware of it. But it’s a little hard to debug without looking at the code, and I did ask the author to remove the code. I am sure the author will remove the code soon