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
      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_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.
    605 
   (...)
    658 
    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.

1 Like

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.

Im getting this erro when i run my train_step function
all other function pass but not this one
Shape of a_S after reshape: (160000, 64)
Shape of a_S after transpose: (64, 160000)
Data type of a_S: <dtype: ‘float32’>
Shape of GS: (64, 64)
Data type of GS: <dtype: ‘float32’>
Shape of a_S after reshape: (40000, 128)
Shape of a_S after transpose: (128, 40000)
Data type of a_S: <dtype: ‘float32’>
Shape of GS: (128, 128)
Data type of GS: <dtype: ‘float32’>
Shape of a_S after reshape: (10000, 256)
Shape of a_S after transpose: (256, 10000)
Data type of a_S: <dtype: ‘float32’>
Shape of GS: (256, 256)
Data type of GS: <dtype: ‘float32’>
Shape of a_S after reshape: (2500, 512)
Shape of a_S after transpose: (512, 2500)
Data type of a_S: <dtype: ‘float32’>
Shape of GS: (512, 512)
Data type of GS: <dtype: ‘float32’>
Shape of a_S after reshape: (625, 512)
Shape of a_S after transpose: (512, 625)
Data type of a_S: <dtype: ‘float32’>
Shape of GS: (512, 512)
Data type of GS: <dtype: ‘float32’>
Shape of a_C after adjustment: (1, 1, 25, 25, 512)
Shape of a_G after adjustment: (1, 200, 200, 128)

ValueError Traceback (most recent call last)
Input In [300], 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..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_fileo9g9cu3.py:14, in outer_factory..inner_factory..tf__train_step(generated_image)
12 J_style = ag
_.converted_call(ag__.ld(sum), ((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) for i in ag__.converted_call(ag__.ld(range), (ag__.converted_call(ag__.ld(len), (ag__.ld(STYLE_LAYERS),), None, fscope),), None, fscope)),), None, fscope)
13 (content_layer_name, content_layer_index) = ag__.ld(content_layer)[0]
—> 14 J_content = ag__.converted_call(ag__.ld(compute_content_cost), (ag__.ld(a_C), ag__.ld(a_G)[ag__.ld(content_layer_index)]), None, fscope)
15 J = ag__.converted_call(ag__.ld(total_cost), (ag__.ld(J_content), ag__.ld(J_style)), None, fscope)
16 ag__.converted_call(ag__.ld(tf).print, (‘Total cost:’, ag__.ld(J)), None, fscope)

File /tmp/autograph_generated_filegthc1_2t.py:64, in outer_factory..inner_factory..tf__compute_content_cost(content_output, generated_output)
62 ag
.ld(print)(‘Shape of a_C after adjustment:’, ag__.ld(a_C).shape)
63 ag__.ld(print)(‘Shape of a_G after adjustment:’, ag__.ld(a_G).shape)
—> 64 (_, n_H, n_W, n_C) = ag__.ld(a_C).shape
65 a_C_unrolled = ag__.converted_call(ag__.ld(tf).reshape, (ag__.ld(a_C), [(- 1), ag__.ld(n_C)]), None, fscope)
66 a_G_unrolled = ag__.converted_call(ag__.ld(tf).reshape, (ag__.ld(a_G), [(- 1), ag__.ld(n_C)]), None, fscope)

ValueError: in user code:

File "<ipython-input-264-fd039bd4926f>", line 32, in train_step  *
    J_content = compute_content_cost(a_C, a_G[content_layer_index])
File "<ipython-input-274-6ded96ed5479>", line 34, in compute_content_cost  *
    _, n_H, n_W, n_C = a_C.shape

ValueError: too many values to unpack (expected 4)

You’ve posted this on a dormant thread, but the error message points you to the problem: you can see that your a_C value has 5 dimensions, but it is expected to have 4. So that should give you something pretty concrete to investigate: where is a_C created and why does it have that extra dimension?