Art Generation with Neural Style Transfer:

In def compute_layer_style_cost(a_S, a_G)

Both of these pass all the tests in the next cell:

  • J_style_layer = … / tf.cast(tf.square(2 * n_H * n_W * n_C), tf.float32)
  • J_style_layer = … / (2 * n_H * n_W * n_C) ** 2

They also print the same result:

  • tf.Tensor(14.01649, shape=(), dtype=float32)
  • tf.Tensor(14.01649, shape=(), dtype=float32)

However, if I use the version with tf.square, then train_step_test(train_step, generated_image) fails with:

AssertionError: Unexpected cost for epoch 0: -318755264.0 != 25629.055

Any idea why?

Yes, the issue is how type coercion rules work in TF when you mix integers and floating point values. Here’s a thread which discusses that in more detail and shows examples.