# RecursionError: maximum recursion depth exceeded while calling a Python object - Week 2

**URL:** <https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [May 30, 2022, 4:07pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897 "2022-05-30T16:07:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patricio\_Ramirez\_Tor](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@Patricio\_Ramirez\_Tor](https://community.deeplearning.ai/u/Patricio_Ramirez_Tor)\
**Post date:** [May 30, 2022, 4:07pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/1 "2022-05-30T16:07:01Z")

</div>

![image](https://global.discourse-cdn.com/dlai/original/3X/9/d/9dd937e93f6071c981398b1389bad17488c033b3.png)  
I get this message, although I already established the sigmoid function as a formula

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [May 30, 2022, 4:13pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/2 "2022-05-30T16:13:41Z")

</div>

If you already have defined sigmoid then you should call it sigmoid(x). Why are you placing the sigmoid formula inside as a parameter the sigmoid function? The function itself should have implemented that formula.

---

<div class="post-metadata">

**Author:** ![Patricio\_Ramirez\_Tor](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@Patricio\_Ramirez\_Tor](https://community.deeplearning.ai/u/Patricio_Ramirez_Tor)\
**Post date:** [May 30, 2022, 4:15pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/3 "2022-05-30T16:15:53Z")

</div>

I tried doing that but it still shows the “maximum recursion depth exceeded” error

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [May 30, 2022, 4:18pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/4 "2022-05-30T16:18:55Z")

</div>

That makes no sense. The error in itself says that you are calling something recursively so many times, python is forced to exit the run because it has hit a limit. Check in your function implememtation of sigmoid what are you calling recursively.

---

<div class="post-metadata">

**Author:** ![Patricio\_Ramirez\_Tor](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@Patricio\_Ramirez\_Tor](https://community.deeplearning.ai/u/Patricio_Ramirez_Tor)\
**Post date:** [May 30, 2022, 4:20pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/5 "2022-05-30T16:20:42Z")

</div>

![image](https://global.discourse-cdn.com/dlai/original/3X/f/d/fd659d48b7097a50d1342d1bcebbbf95c903658b.png)  
It marks as error one line that was given to me( the first one) and twice the one I wrote

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [May 30, 2022, 4:30pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/6 "2022-05-30T16:30:40Z")

</div>

Remove that print line, its wrong. You can just print without str().

---

<div class="post-metadata">

**Author:** ![Patricio\_Ramirez\_Tor](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@Patricio\_Ramirez\_Tor](https://community.deeplearning.ai/u/Patricio_Ramirez_Tor)\
**Post date:** [May 30, 2022, 4:39pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/7 "2022-05-30T16:39:49Z")

</div>

It doesnt let me edit that cell, I think because that cell was given to me directly

---

<div class="post-metadata">

**Author:** ![Elemento](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/elemento/32/854_2.png) [@Elemento](https://community.deeplearning.ai/u/Elemento)\
**Post date:** [May 30, 2022, 4:49pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/8 "2022-05-30T16:49:33Z")

</div>

Hey @Patricio_Ramirez_Tor,  
Welcome to the community. As Gent said, you have an issue with your implementation of the `sigmoid` function. Can you please share your implementation of the same with me?

Make sure to not post the code here since it is strictly against the community guidelines. DM your code to me. For that, click on my name and select Message.

Regards,  
Elemento

---

<div class="post-metadata">

**Author:** ![anon57530071](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anon57530071](https://community.deeplearning.ai/u/anon57530071)\
**Post date:** [May 30, 2022, 4:56pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/9 "2022-05-30T16:56:59Z")

</div>

My best guess is s=sigmoid(x) is inside def sigmoid(x):

---

<div class="post-metadata">

**Author:** ![Patricio\_Ramirez\_Tor](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@Patricio\_Ramirez\_Tor](https://community.deeplearning.ai/u/Patricio_Ramirez_Tor)\
**Post date:** [May 30, 2022, 5:02pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/10 "2022-05-30T17:02:55Z")

</div>

Hi I already sent you a message

---

<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:** [May 30, 2022, 7:07pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/11 "2022-05-30T19:07:55Z")

</div>

> [@anon57530071](#):
>
> My best guess is s=sigmoid(x) is inside def sigmoid(x):

Exactly: you can see the bug in the exception trace. You are calling sigmoid from within the sigmoid function. That is called “recursion”. There are some circumstances in which that can be a useful technique, but this is not one of them. 🤓 The result is effectively an infinite loop: what will ever terminate the calling? With each additional nested call, you’re going deeper into the stack, so you eventually get a stack overflow as seen above.

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [May 31, 2022, 7:03am UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/12 "2022-05-31T07:03:34Z")

</div>

Failed to see that with the code starts and code end are usually inside the implementation.

---

<div class="post-metadata">

**Author:** ![Patricio\_Ramirez\_Tor](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@Patricio\_Ramirez\_Tor](https://community.deeplearning.ai/u/Patricio_Ramirez_Tor)\
**Post date:** [June 1, 2022, 3:50pm UTC](https://community.deeplearning.ai/t/recursionerror-maximum-recursion-depth-exceeded-while-calling-a-python-object-week-2/132897/13 "2022-06-01T15:50:54Z")

</div>

Thank you! That helped me fix it, finally it all came down to rewritting it.
