C4 Week 4 Assignment 2 Exercise 6

I am getting the following error when trying to do the ‘train_step’ function and I have no idea how to fix this.
I have looked at the functions I have created that are used and they all seem to work fine.

NotImplementedError                       Traceback (most recent call last)
Input In [46], in <cell line: 7>()
      1 ### you cannot edit this cell
      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_filequundupz.py:12, in outer_factory.<locals>.inner_factory.<locals>.tf__train_step(generated_image)
     10 with ag__.ld(tf).GradientTape() as tape:
     11     a_G = ag__.converted_call(ag__.ld(vgg_model_outputs), (ag__.ld(generated_image),), None, fscope)
---> 12     J_style = ag__.converted_call(ag__.ld(compute_style_cost), (ag__.ld(a_S), ag__.ld(a_G)), None, fscope)
     13     J_content = ag__.converted_call(ag__.ld(compute_content_cost), (ag__.ld(a_C), ag__.ld(a_G)), None, fscope)
     14     J = ag__.converted_call(ag__.ld(total_cost), (ag__.ld(J_content), ag__.ld(J_style)), dict(alpha=10, beta=40), fscope)

File /tmp/__autograph_generated_filesn2nhuk1.py:31, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_style_cost(style_image_output, generated_image_output, STYLE_LAYERS)
     29 J_style_layer = ag__.Undefined('J_style_layer')
     30 i = ag__.Undefined('i')
---> 31 ag__.for_stmt(ag__.converted_call(ag__.ld(zip), (ag__.converted_call(ag__.ld(range), (ag__.converted_call(ag__.ld(len), (ag__.ld(a_S),), None, fscope),), None, fscope), ag__.ld(STYLE_LAYERS)), None, fscope), None, loop_body, get_state, set_state, ('J_style',), {'iterate_names': '(i, weight)'})
     32 try:
     33     do_return = True

File /tmp/__autograph_generated_filesn2nhuk1.py:25, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_style_cost.<locals>.loop_body(itr)
     23 nonlocal J_style
     24 (i, weight) = itr
---> 25 J_style_layer = ag__.converted_call(ag__.ld(compute_layer_style_cost), (ag__.ld(a_S)[ag__.ld(i)], ag__.ld(a_G)[ag__.ld(i)]), None, fscope)
     26 J_style = ag__.ld(J_style)
     27 J_style += (weight[1] * J_style_layer)

File /tmp/__autograph_generated_filefzajk99g.py:13, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_layer_style_cost(a_S, a_G)
     11 (_, n_H, n_W, n_C) = ag__.converted_call(ag__.ld(a_G).shape.as_list, (), None, fscope)
     12 a_S = ag__.converted_call(ag__.ld(np).reshape, (ag__.converted_call(ag__.ld(np).transpose, (ag__.ld(a_S), [0, 3, 1, 2]), None, fscope), [ag__.ld(n_C), (ag__.ld(n_H) * ag__.ld(n_W))]), None, fscope)
---> 13 a_G = ag__.converted_call(ag__.ld(np).reshape, (ag__.converted_call(ag__.ld(np).transpose, (ag__.ld(a_G), [0, 3, 1, 2]), None, fscope), [ag__.ld(n_C), (ag__.ld(n_H) * ag__.ld(n_W))]), None, fscope)
     14 GS = ag__.converted_call(ag__.ld(gram_matrix), (ag__.ld(a_S),), None, fscope)
     15 GG = ag__.converted_call(ag__.ld(gram_matrix), (ag__.ld(a_G),), None, fscope)

File <__array_function__ internals>:180, in transpose(*args, **kwargs)

File /usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py:660, in transpose(a, axes)
    601 @array_function_dispatch(_transpose_dispatcher)
    602 def transpose(a, axes=None):
    603     """
    604     Reverse or permute the axes of an array; returns the modified array.
    659     """
--> 660     return _wrapfunc(a, 'transpose', axes)

File /usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py:54, in _wrapfunc(obj, method, *args, **kwds)
     52 bound = getattr(obj, method, None)
     53 if bound is None:
---> 54     return _wrapit(obj, method, *args, **kwds)
     56 try:
     57     return bound(*args, **kwds)

File /usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py:43, in _wrapit(obj, method, *args, **kwds)
     41 except AttributeError:
     42     wrap = None
---> 43 result = getattr(asarray(obj), method)(*args, **kwds)
     44 if wrap:
     45     if not isinstance(result, mu.ndarray):

NotImplementedError: in user code:

    File "<ipython-input-39-344a2016faad>", line 19, in train_step  *
        J_style = compute_style_cost(a_S, a_G)
    File "<ipython-input-15-7b56545257a6>", line 30, in compute_style_cost  *
        J_style_layer = compute_layer_style_cost(a_S[i], a_G[i])
    File "<ipython-input-10-617022207b12>", line 20, in compute_layer_style_cost  *
        a_G = np.reshape(np.transpose(a_G, [0,3,1,2]), [n_C,n_H*n_W])
    File "<__array_function__ internals>", line 180, in transpose  **
    File "/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py", line 660, in transpose
        return _wrapfunc(a, 'transpose', axes)
    File "/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py", line 54, in _wrapfunc
        return _wrapit(obj, method, *args, **kwds)
    File "/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py", line 43, in _wrapit
        result = getattr(asarray(obj), method)(*args, **kwds)

    NotImplementedError: Cannot convert a symbolic tf.Tensor (model/block1_conv1/Relu:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.

I agree that the exception trace is pretty hard to read and doesn’t convey enough information to tell which actual line of code is “throwing”. The only definitive thing you can tell is that you must have a Numpy function someplace in your code where you really need a TF function. It could be anywhere, since all your previous functions will get called as part of the training. For example, do you use numpy .T to do the transpose anywhere or call np.reshape instead of tf.reshape? Of course you might well ask “if it was wrong in earlier code, why didn’t it fail the earlier tests?” But now that we are running training, the rules are different: when you’re doing backpropagation in TF, it’s all being handled for you automatically, but it requires that every operation in the compute graph be a TF operation. For numpy operations, TF can’t compute the gradients. I’m not sure that’s the explanation, since in the cases I’ve seen of including numpy operations it gives a more explicit message about not being able to compute gradients. FWIW here is another recent thread that shows at least one example of the kind of error you can get by mixing numpy operations in a TF compute graph.

If that’s not enough of a clue to find the error, then we can use the “in case of emergency, break glass” method and take a look at your code. I’ll send you a DM about how to do that.

Yes in my compute_layer_style_cost function I was using np.reshape and np.transpose instead of tf.reshape and tf.transpose.
Changing that fixed the problem.
Thanks for the help.

1 Like

Glad to hear you were able to find the issue based on that suggestion. One other note is that there should be no need to use “reshape” in the compute_total_cost code, but you do definitely need “transpose”. But if you pass the tests and the grader it must be ok.