Hello, I’m on Course 4, Week 4, Assignment 2, Exercise 6.
What’s wrong with the following code ? Print statements showed that a_C, a_S, and a_G are of the same shape (1, 400, 400, 64).
Any idea ? Thanks!
Hello, I’m on Course 4, Week 4, Assignment 2, Exercise 6.
What’s wrong with the following code ? Print statements showed that a_C, a_S, and a_G are of the same shape (1, 400, 400, 64).
Any idea ? Thanks!
Please remove your code from your message - sharing your code isn’t allowed.
What makes you believe there is an error in your code?
Thanks for your reply. I got this assertion error:
AssertionError Traceback (most recent call last)
<ipython-input-27-c4cbeb243f35> in <module>
6 print(J1)
7 assert type(J1) == EagerTensor, f"Wrong type {type(J1)} != {EagerTensor}"
----> 8 assert np.isclose(J1, 25629.055, rtol=0.05), f"Unexpected cost for epoch 0: {J1} != {25629.055}"
9
10 J2 = train_step(generated_image)
AssertionError: Unexpected cost for epoch 0: -317876320.0 != 25629.055
The problem is that your cost is wrong - there’s no reason to suspect that the sizes are incorrect.
Most likely the cost returned by compute_style_cost() or compute_content_cost() is incorrect.
The first indication that your cost is incorrect is that it’s a negative number. Cost must always be >= zero.
I wouldn’t work on Ex 6 if one of my previous exercises was wrong. I got “All tests passed” from Exercises 1 - 5. The compute_style_cost() function (Ex 4) has already been implemented by given. The compute_content_cost() function (Ex 1) got “All tests passed” and I saw that the expected output matched mine.
As you can see, both cost functions (content & style) depend on the three parameters a_S, a_C, and a_G. If so, if either cost is wrong, then one of (a_S, a_C, or a_G) is wrong then. However, for Ex 6, I was asked to use the global variables a_S and a_C while a_G is the only computed parameter: a_G = vgg_model_outputs(generated_image) . Can’t tell which is wrong here!
“All tests pass” does not mean your code is perfect.
The unit tests don’t catch every possible error - they actually don’t catch very many errors at all.
compute_style_cost() is provided code, but it calls compute_layer_style_cost(). That’s one place the problem could be.
I appreciate your assistance so far! Regarding the compute_layer_style_cost(a_S, a_G) function, here’s what I have (you can remove my code after seeing it but this is the only way that I can tell you my problem):
# Retrieve dimensions from a_G (≈1 line)
_, n_H, n_W, n_C = a_G.get_shape().as_list()
# Reshape the images from (n_H * n_W, n_C) to have them of shape (n_C, n_H * n_W) (≈2 lines)
a_S = tf.reshape(tf.transpose(a_S), shape=[n_C, n_H * n_W])
a_G = tf.reshape(tf.transpose(a_G), shape=[n_C, n_H * n_W])
# Computing gram_matrices for both images S and G (≈2 lines)
GS = tf.matmul(a_S, tf.transpose(a_S))
GG = tf.matmul(a_G, tf.transpose(a_G))
# Computing the loss (≈1 line)
numerator = tf.reduce_sum(tf.square(tf.subtract(GS, GG)))
denominator = tf.cast(tf.square(2 * n_H * n_W * n_C), tf.float32)
J_style_layer = numerator / denominator
And I got the same result as the expected output for J_style_layer (= 14.017805)
Please let me know what’s wrong with the above code. Many thanks!
Why do you write out the computation of the gram matrices? You built a function to do that for you, right?
I have seen problems in the past (including negative cost values) on this function if you use TF integer arithmetic with n_H and so forth. The code you have written looks like it should work, but another thing to try is just to use normal python arithmetic or numpy operations for the scalar quantities and see if that helps.
Good to see you Paul!
You are right, I was blinded, I should have called gram_matrix(a_S) and gram_matrix(a_G) instead.
I will try your recommendation. Thanks!