# Week3 Lab last 3.3 - Train the Model kept failing even though it seems not requires us write any code

**URL:** <https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** week-module-3, coursera-platform\
**Created:** [July 14, 2024, 6:27am UTC](https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843 "2024-07-14T06:27:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![biz2024](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@biz2024](https://community.deeplearning.ai/u/biz2024)\
**Post date:** [July 14, 2024, 6:27am UTC](https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843/1 "2024-07-14T06:27:50Z")

</div>

I am on last lab of week three, it is called 3.3 - Train the Model. It does not seem to asks us to write anything and just run the lab, and I ran the lab it kept failing on apply optimizer code, saying “No gradients provided for any variable: [‘Variable:0’, ‘Variable:0’, ‘Variable:0’, ‘Variable:0’, ‘Variable:0’, ‘Variable:0’].”, but we are not changing the code and there’s previous step of grads = tape.gradient(minibatch\_total\_loss, trainable\_variables) which supposed to provide the gradian. And I also attached the original code, which didn’t ask us to add any program to explicitly create gradian. What can I do?

 ![Screenshot 2024-07-13 at 11.36.07 PM](https://global.discourse-cdn.com/dlai/original/3X/1/5/152e3d596e2edf79c3a13fe05ccd6f133ddc4169.png)

 ![Screenshot 2024-07-13 at 11.25.50 PM](https://global.discourse-cdn.com/dlai/original/3X/6/3/63a0abfa1c80c211e01553f5878fd9b07efe842d.png)

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [July 14, 2024, 3:12pm UTC](https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843/3 "2024-07-14T15:12:13Z")

</div>

Hello, @biz2024,

From your post, I understand that you had passed all previous tests and you didn’t expect any error from this provided code. However, this provided code used functions that you wrote and if any of them didn’t work perfectly, provided code will crash. There are two possibilities here:

1. your functions are all correct, but some provided code was somehow altered unintentionally. In this case, you can get a fresh copy and retry. [This post shows you how.](https://community.deeplearning.ai/t/how-to-get-a-clean-copy-of-an-assignment-notebook/73)

2. some of your functions were not perfect, and unfortunately, the tests were not able to detect them. In this case, we need to understand the origin of the error, and try to sort it out ourselves.

# Origin of error

This error happens when there are no valid gradient values for all variables, which you can verify by adding a `print` like below.

 ![image](https://global.discourse-cdn.com/dlai/original/3X/9/5/957697dd5ea139f745084dc12c7c26142fd16d52.png)

_ **With the error, this should show a list of six `None`.** _ In normal case, they should not be `None`.

This problem happens when it didn’t use any of the trainable variables to compute the loss. In other words, to avoid this error, you need to make sure all variables are involved in the calculation of the loss.

By the design of the notebook, as your screenshot showed, the variables should go through the path ( 1 → 5 ) and end up as the loss:

 ![image](https://global.discourse-cdn.com/dlai/original/3X/c/b/cbd5ba479eb7f46ca14b7db69ffaf49f34d08b23.jpeg)

Note that this path uses your functions. Your work would be to follow this path and check each of your function to make sure the trainable variables are correctly used, and **they and their downstream processed outputs are always processed by tensorflow functions** (tensorflow functions start with `tf.xxxxx`, if you see any `np.xxxxx`, there is a problem 😉 . `tf` stands for tensorflow, whereas `np` stands for numpy. We need to use only tensorflow functions here. )

If you used any `np` function in the middle of the path, even you started out using the variables correctly, the `np` function will convert them (or their downstream processed outputs) to something else which means the variables (or their downstream processed outputs) will get kicked out of the remaining steps of the path. We don’t want them to get kicked out, so no `np` function ever in the path.

Good luck!

Cheers,  
Raymond

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [July 14, 2024, 3:43pm UTC](https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843/4 "2024-07-14T15:43:12Z")

</div>

Yes, Raymond’s very complete explanation must be what is going on: in the functions you wrote that are being called during training, you must have used a numpy operation and those do not support the automatic generation of gradients, as the TF calls do. So it breaks the compute graph and you get no gradients, so the training can’t happen.

I’ve seen this error before and here’s one surprising and kind of “sneaky” way you can get this error: notice that in the `compute_total_loss` function, you need to transpose the `labels` and `logits` before calling the TF loss function. If you do that using `np.transpose` or just `labels.T`, then it can have this effect. Try using `tf.transpose` instead, if that is what your code looks like.

---

<div class="post-metadata">

**Author:** ![biz2024](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@biz2024](https://community.deeplearning.ai/u/biz2024)\
**Post date:** [July 14, 2024, 8:07pm UTC](https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843/5 "2024-07-14T20:07:36Z")

</div>

You guys can find these kind of stuff? You are wonderful and I am impressed.

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [July 15, 2024, 6:36am UTC](https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843/6 "2024-07-15T06:36:18Z")

</div>

Paul is our Super Mentor 🙌!

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [July 15, 2024, 4:22pm UTC](https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843/7 "2024-07-15T16:22:52Z")

</div>

And Raymond is our Super Explainer! 👍 🤓

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [July 16, 2024, 4:34am UTC](https://community.deeplearning.ai/t/week3-lab-last-3-3-train-the-model-kept-failing-even-though-it-seems-not-requires-us-write-any-code/662843/8 "2024-07-16T04:34:15Z")

</div>

🙌🙌🙌
