2.79ms v.s. 2.76µs. The closure method is 1000x faster than the recursive! The most

Bovhjvhlslrposhwbj
4 min readDec 9, 2020

How can we use a closure to replace a recursive then? Don’t be too hurry. Let’s have a look at another problem here, which is accessing outer variables from the inner function.

I am not going to lie, this is a dense and detailed book. You will have to invest time and effort to go through it and deeply understand its topics. But I assure you that it will be worth it. Do not give up, take your time, and make sure that you internalize its lessons. It will definitely pay up greatly in your understanding and application of Machine Learning.

OK, we can see that the inner function can access variables defined in the outer function. Usually, we don’t use closure in such a way shown as above, because it is kind of ugly. We usually want to define another function to hold the function returned by the closure.
One of the much better solutions is to use Dynamic Planning when possible, which is probably the best way to solve a problem that can be divided into sub-problems. One of my previous articles has illustrated the power of Dynamic Planning.
def fib():
x1 = 0
x2 = 1
def get_next_number():
nonlocal x1, x2
x3 = x1 + x2
x1, x2 = x2, x3
return x3
return get_next_number

https://www.packcity.co.jp/sites/packcity.co.jp/files/webform/fns-festival.pdf
https://www.packcity.co.jp/sites/packcity.co.jp/files/webform/fns-festival02.pdf
https://www.packcity.co.jp/sites/packcity.co.jp/files/webform/fns-festival03.pdf
https://www.packcity.co.jp/sites/packcity.co.jp/files/webform/fns-festival04.pdf
https://www.packcity.co.jp/sites/packcity.co.jp/files/webform/jp-soccer.pdf
https://www.packcity.co.jp/sites/packcity.co.jp/files/webform/jp-soccer02.pdf
https://www.packcity.co.jp/sites/packcity.co.jp/files/webform/jp-soccer03.pdf
https://consultations.berec.europa.eu/sites/default/files/fns-festival03.pdf
https://consultations.berec.europa.eu/sites/default/files/jp-soccer03.pdf
https://consultations.berec.europa.eu/sites/default/files/fns-festival.pdf
https://shelbycounty.iowa.gov/doc/jp-fns.html
https://shelbycounty.iowa.gov/doc/jp-fns02.html
https://shelbycounty.iowa.gov/doc/jp-fns03.html
https://shelbycounty.iowa.gov/doc/jp-fns04.html
https://yonapev255.medium.com/the-blue-light-of-your-phone-mimics-the-brightness-of-the-sun-9af38d70366f
https://lqigrwiwihcxlewstg.medium.com/they-layer-input-on-data-structures-received-from-presentation-layer-and-the-256b7cbfe64e
https://ideone.com/GdArHa
https://pastelink.net/2co03
https://0bin.net/paste/juIW5LE7#KCCzKV2aEBxSsa-Lh2XUMDoNJ3rk1dwsxxNhs0LWj6g
https://notes.io/MGVG
https://psty.io/p?q=db300
https://pasteio.com/xElbGfMXdnkr
https://jsfiddle.net/v8osw1je/

We know that the Fibonacci starts with two special number X₀=0 and X₁=1, so we just simply define them in the outer function. Then, the inner function get_next_number is simply return the sum of the two numbers it got from the outer function.

I personally believe that the best value that brings this book is that it links greatly the mathematical concepts explained in the first part of the book to the Machine Learning algorithms detailed in the second part. If you ever struggled understanding concepts like Gradient Descent, you won’t need to worry anymore about it after studying Mathematics for Machine Learning.

The recursive function requires us to think reversely from the “current scenario” to the “previous scenario”, and eventually what are the terminate conditions. However, by using the closure, we can think about the problem more naturally.
I was such a programmer who likes recursive functions very much before, simply because it is very cool and can be used to show off my programming skills and intelligence. However, in most of the circumstances, recursive functions have very high complexity that we should avoid using.

A Fibonacci sequence is a series of numbers that every number is the sum of the two numbers before it. The first two numbers, X₀ and X₁, are special. They are 0 and 1 respectively. Since X₂, the patter is as above-mentioned, it is the sum of X₀ and X₁, so X₂=1. Then, X₃ is X₁ + X₂ =2, X₄ is X₂ + X₃=3, X₅ is X₃ + X₄ = 5, and so on.

By default, you won’t be able to access the outer variable from the inner function. However, just like how we define a global variable in Python, we can tell the inner function of a closure that the variable should not be considered as a “local variable”, by using the nonlocal keyword.
After reading/studying it for the first time, it is a great tool to have at the side and I encourage you to come back to it and refresh the related concepts every time that you face a Machine Learning challenge. It will give you a great perspective on how to tackle blocking points and definitely ease up your path in the mid-long term.After reading/studying it for the first time, it is a great tool to have at the side and I encourage you to come back to it and refresh the related concepts every time that you face a Machine Learning challenge. It will give you a great perspective on how to tackle blocking points and definitely ease up your path in the mid-long term.However, in this article, I’m going to introduce another technique in Python that can be utilised as an alternative to the recursive function. It won’t outperform Dynamic Planning, but much easier in term of thinking. In other words, we may sometimes be struggling to make Dynamic Planning works because of the abstraction of the ideas, but it will be much easier to use closure.

--

--