DLS C4 W4 Assignment 2, UNQ_C5, Unexpected cost for epoch 0

For UNQ_C5, my my result is so big? 20079974.0 ?

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

AssertionError Traceback (most recent call last)
in
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: 20079974.0 != 25629.055

I suspect there’s an error in your code.

My UNQ_C5 code is correct, I checked others posts and internet.

The problem most likely is to do with how you handle the scalar values in that computation. You have to be careful not to use TF functions for computing the scalar values, because they have very different type coercion rules. Either use python or numpy for the scalar parts and only use the TF functions for the tensors. Here’s a thread which discusses issues related to this.

2 Likes

According to your reply, I double checked my styly_cost function.
The issue is the "(tf.square(n_Cn_Hn_W) " , my original statement:
J_style_layer = (1. /(4. * float(tf.square(n_Cn_Hn_W) ) )) * tf.reduce_sum(tf.square(tf.subtract(GS,GG)))
I have changed to :
J_style_layer = (1 /(4 * (n_Cn_Hn_W)**2 ) ) * tf.reduce_sum(tf.square(tf.subtract(GS,GG)))
The error disappears.
Thanks very much.

3 Likes

That looks good! I’m glad to hear you found the solution.