Convolutional Neural Networks, Week 4, Programming Assignment 2: Art Generation with Neural Style Transfer

I implemented all the previous functions correctly. But for train_step
‘code ignored’

It threw the error below:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [73], in <cell line: 7>()
      1 ### you cannot edit this cell
      2 
      3 # You always must run the last cell before this one. You will get an error if not.
      5 generated_image = tf.Variable(generated_image)
----> 7 train_step_test(train_step, generated_image)

File /tf/W4A2/public_tests.py:86, in train_step_test(target, generated_image)
     82 def train_step_test(target, generated_image):
     83     generated_image = tf.Variable(generated_image)
---> 86     J1 = target(generated_image)
     87     print(J1)
     88     assert type(J1) == EagerTensor, f"Wrong type {type(J1)} != {EagerTensor}"

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File /tmp/__autograph_generated_file6kzsm2qi.py:20, in outer_factory.<locals>.inner_factory.<locals>.tf__train_step(generated_image)
     18     ag__.ld(print)('\n\nJ:', ag__.ld(J))
     19 grad = ag__.converted_call(ag__.ld(tape).gradient, (ag__.ld(J), ag__.ld(generated_image)), None, fscope)
---> 20 ag__.converted_call(ag__.ld(optimizer).apply_gradients, ([(ag__.ld(grad), ag__.ld(generated_image))],), None, fscope)
     21 ag__.converted_call(ag__.ld(generated_image).assign, (ag__.converted_call(ag__.ld(clip_0_1), (ag__.ld(generated_image),), None, fscope),), None, fscope)
     22 try:

File /usr/local/lib/python3.8/dist-packages/keras/optimizers/optimizer_v2/optimizer_v2.py:640, in OptimizerV2.apply_gradients(self, grads_and_vars, name, experimental_aggregate_gradients)
    599 def apply_gradients(self,
    600                     grads_and_vars,
    601                     name=None,
    602                     experimental_aggregate_gradients=True):
    603   """Apply gradients to variables.
    604 
    605   This is the second part of `minimize()`. It returns an `Operation` that
   (...)
    638     RuntimeError: If called in a cross-replica context.
    639   """
--> 640   grads_and_vars = optimizer_utils.filter_empty_gradients(grads_and_vars)
    641   var_list = [v for (_, v) in grads_and_vars]
    643   with tf.name_scope(self._name):
    644     # Create iteration if necessary.

File /usr/local/lib/python3.8/dist-packages/keras/optimizers/optimizer_v2/utils.py:73, in filter_empty_gradients(grads_and_vars)
     71 if not filtered:
     72   variable = ([v.name for _, v in grads_and_vars],)
---> 73   raise ValueError(f"No gradients provided for any variable: {variable}. "
     74                    f"Provided `grads_and_vars` is {grads_and_vars}.")
     75 if vars_with_empty_grads:
     76   logging.warning(
     77       ("Gradients do not exist for variables %s when minimizing the loss. "
     78        "If you're using `model.compile()`, did you forget to provide a `loss`"
     79        "argument?"),
     80       ([v.name for v in vars_with_empty_grads]))

ValueError: in user code:

    File "<ipython-input-72-c43c474a756b>", line 37, in train_step  *
        optimizer.apply_gradients([(grad, generated_image)])
    File "/usr/local/lib/python3.8/dist-packages/keras/optimizers/optimizer_v2/optimizer_v2.py", line 640, in apply_gradients  **
        grads_and_vars = optimizer_utils.filter_empty_gradients(grads_and_vars)
    File "/usr/local/lib/python3.8/dist-packages/keras/optimizers/optimizer_v2/utils.py", line 73, in filter_empty_gradients
        raise ValueError(f"No gradients provided for any variable: {variable}. "

    ValueError: No gradients provided for any variable: (['Variable:0'],). Provided `grads_and_vars` is ((None, <tf.Variable 'Variable:0' shape=(1, 400, 400, 3) dtype=float32>),).

The values of J_style, J_content, and J should be scalars, as shown in previous cells in the assignment. Don’t know why they became

J_style:  Tensor("add_4:0", shape=(), dtype=float32)


J_content: Tensor("mul_10:0", shape=(), dtype=float32)


J: Tensor("PartitionedCall:0", shape=(), dtype=float32)

Thanks for any hints :smile:

PS: a_G looks like below:

a_G :  [<tf.Tensor 'model/block1_conv1/Relu:0' shape=(1, 400, 400, 64) dtype=float32>, <tf.Tensor 'model/block2_conv1/Relu:0' shape=(1, 200, 200, 128) dtype=float32>, <tf.Tensor 'model/block3_conv1/Relu:0' shape=(1, 100, 100, 256) dtype=float32>, <tf.Tensor 'model/block4_conv1/Relu:0' shape=(1, 50, 50, 512) dtype=float32>, <tf.Tensor 'model/block5_conv1/Relu:0' shape=(1, 25, 25, 512) dtype=float32>, <tf.Tensor 'model/block5_conv4/Relu:0' shape=(1, 25, 25, 512) dtype=float32>]

@daminglu Uh oh, don’t post your code because that is a ‘no no’, but I will look.

@daminglu I’m not exactly sure how to get you there, what you have is mostly right, but I’d check your a_G line.

He he… Sorry it is late here and I am thinking ‘whose code is what’ ? What I have is much simpler than what you are doing.

We have a function, just drive the function.

sorry, should I delete the code now?

Thanks, Nevermnd! So you mean I did it wrong for calculating a_G?

@daminglu I referenced my code and gave you my best hint. I just think you are thinking too hard on this. The solution is easier.

Hey, Nevermnd! I guess my a_G is correct. In the assignment, it wrote

"Set this encoding to the variable a_C. Later in the assignment, you will need to do the same for the generated image, by setting the variable a_G to be the appropriate hidden layer activations. "

So that is what I did to generated_image in order to get a_G

Ah, thanks Nevermnd!
I got it!
In the test, it already did:

generated_image = tf.Variable(generated_image)

done~

Hey, Nevermnd! Sorry for the trouble I caused. It’s my first time posting a question here. Should I delete the code block?

@daminglu 没事. But yes you should do that, someone will have a problem.