Course 4 Week 4 Art Generation Ex 3: My value of cost is incorrect


Im currently on the 3rd exercise “compute layer style cost”. I’m getting a value that is a little more than double what is expected, i’ve looked over the math many times and can’t seem to see what im doing wrong…

# Retrieve dimensions from a_G (≈1 line)
    m, n_H, n_W, n_C = a_G.get_shape().as_list()
    # Reshape the images to have them of shape (n_C, n_H*n_W) (≈2 lines)
    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]))

    # Computing gram_matrices for both images S and G (≈2 lines)
    GS = gram_matrix(a_S)
    GG = gram_matrix(a_G)
    # Computing the loss (≈1 line)
    J_style_layer = tf.reduce_sum(tf.square(tf.subtract(GS,GG)))/(4*(n_C**2)*((n_H*n_W)**2))```

my error: 

AssertionError                            Traceback (most recent call last)
<ipython-input-15-f19da0544d50> in <module>
      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.

ok, i think one of the mistakes is that i’ve transposed the values of a_S and a_G before feeding it into the gram_matrix function. But the overall cost is still wrong.

a_S = tf.transpose(tf.reshape(a_S, [n_Hn_W, n_C]))
a_G = tf.transpose(tf.reshape(a_G, [n_H
n_W, n_C]))

The above works, but im still not 100% sure why “tf.reshape(a_S, [n_C, n_H*n_W])” didnt?

1 Like

Hi EvilerEarth,

The original matrix is stored in memory in 1D row-wise. If you immediately reshape to n_C, n_H*n_W, it will start cutting into the consecutive rows of the n_H dimension. If you reshape to n_H*n_w, n_C, you create consecutive values per layer, which can then be transposed.

In case my explanation is unclear, here’s a similar explanation.


Hi @reinoudbosch , do you mean rows of the n_Hn_W dimension? Using row major ording, tensorflow will set aside rows after rows one of which has the dimension of n_Hn_W, right?

Hi 1157350959,

Yes, that’s a clearer way to put it. Thanks!