Art_Generation_with_Neural_Style_Transfer exercise 3

I’ve retrieved dimensions successfully, converted the shape of a_S and a_G from(1,n_H,n_W,n_C) to (n_C,n_H*n_W) , calculated the gram matrices using the predefined function gram_matrix(A) and computed J_style_layer as follow:
J_style_layer = (1 / (4 * n_C**2 * (n_H * n_W)**2)) * tf.reduce_sum(tf.square(tf.subtract(GS, GG)))

this error appears and I can’t understand
"AssertionError Traceback (most recent call last)
Input In [34], in <cell line: 3>()
1 ### you cannot edit this cell
----> 3 compute_layer_style_cost_test(compute_layer_style_cost)

File /tf/W4A2/public_tests.py:57, in compute_layer_style_cost_test(target)
55 assert np.isclose(J_style_layer_GG, 0.0), “Wrong value. compute_layer_style_cost(A, A) must be 0”
56 assert J_style_layer_SG > 0, “Wrong value. compute_layer_style_cost(A, B) must be greater than 0 if A != B”
—> 57 assert np.isclose(J_style_layer_SG, 14.01649), “Wrong value.”
59 print(“J_style_layer = " + str(J_style_layer_SG))
60 print(”\033[92mAll tests passed")

AssertionError: Wrong value."

Your J_style_layer code seems correct. How you are reshaping the tensors from (1, n_H, n_W, n_C) to (n_C, n_H * n_W)? You have to use both, tf.reshape and tf.transpose. First, use tf.reshape and then tf.transpose. Maybe this thread seems relevant to your query.

2 Likes

I’m using this method “a_S = tf.transpose(tf.reshape(a_S, shape=[n_C, n_H * n_W]))
a_G = tf.transpose(tf.reshape(a_G, shape=[n_C, n_H * n_W]))” … am I right ?

No. Please read this again:

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

If you are using transpose, then what should be the shape for tf.reshape to have (n_C, n_H * n_W)?

1 Like

Got it ! thank you for your helpful hint :heart: