Course 4: Art Generation Exercise 6

Hi. Why the Exercise 6 “train_step” and the following test step need to be executed together. I run the test step alone several times and always gives different numbers - while triggering the message “Unexpected cost for epoch 0: {J1} != {25629.055}”. I need to re start the kernel and run all cells to get the “all test passed message”


train_step(), which is decorated with @tf.function, is a tape function to record “J” and “generated_image”, and calculate gradients.

If you want to re-execute the cell just below “train_step()”, you need to do two things.

  1. reset "generated_image. - This can be done in the following cell, which is far above the current cell.
generated_image = tf.Variable(tf.image.convert_image_dtype(content_image, tf.float32))
noise = tf.random.uniform(tf.shape(generated_image), -0.25, 0.25)
generated_image = tf.add(generated_image, noise)
generated_image = tf.clip_by_value(generated_image, clip_value_min=0.0, clip_value_max=1.0)
  1. execute a cell for “train_step”.

Since “J” is calculated inside “train_step”, we do not need to care.

Hope this helps.