What is recursion?

A concise guide from zero to one. 100% knowledge, 0% fluff. ๐Ÿ “

1/13
Functions, the central objects of mathematics and computer science, are just mappings of inputs to outputs.

A convenient (albeit quite imprecise) way to define them is to describe their effect. An explicit formula is often available, which we can translate to code.

2/13
However, giving an explicit formula is not always easy or possible.

For instance, can you calculate the number of ways we can order a deck of n cards by shuffling its cards?

3/13
There is a solution besides giving a formula.

Suppose that we shuffled n-1 cards. Given a new one, we can insert it into the deck at n possible locations.

Thus, all the possible shuffles of n can be obtained by shuffling n-1 cards first, then inserting the remaining one.

4/13
Counting this way gives rise to a formula that references itself. This is called recursion.

For the computation to end, we have to supply a so-called boundary condition. In our case, this is simple: a "deck" consisting of 1 card can be shuffled only one way.

5/13
Every recursion has two crucial components: the recursive step and the boundary condition.

6/13
In practice, we can simply implement recursive functions by calling the function in its definition. Most programming languages support this.

Frequently, recursion is an extremely convenient way to write clear and concise functions.

7/13
However, recursion is a double-edged sword.

Let's talk about a case where the recursive step involves referencing the function multiple times.

The famous Fibonacci numbers provide an example of this.

8/13
Just like previously, we can easily supply a recursive function to compute the n-th Fibonacci number.

Can you think of any potential issues?

9/13
For each call, the function calls itself two times. Those make an additional two calls individually, and so on.

This is how the recursive calls look for n = 4. (Each arrow represents a function call.)

10/13
As you probably figured, this can blow up really fast.

Essentially, computing F(n) this way involves an exponential number of calls to F.

11/13
Just out of curiosity, I have measured the time it takes to compute a few Fibonacci numbers with the recursive function.

F(40) took more than 30 seconds on my computer. I had no patience to wait out F(50).

So, recursion can be really slow.

12/13
TL;DR: a recursive function is one that references itself in its definition. They are powerful, but can be really slow.

Can you think of a better way to implement the computation of the Fibonacci numbers? Share your ideas below! (I can think of at least three.)

13/13

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Tivadar Danka

Tivadar Danka Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @TivadarDanka

19 Oct
The single best way to get into machine learning is to build something with it.

Here is an extensive list of hands-on projects that you can start right now. Take inspiration, learn tools, and find the topics you are passionate about.

Read on and go create something awesome. โ†“
I am grouping the projects into the following categories.

๐Ÿ“บ Computer vision
๐Ÿ—ฃ๏ธ NLP
๐ŸŽฎ Reinforcement learning
๐Ÿ—„๏ธ Data engineering
๐Ÿ“Š Visualization
โ˜๏ธ Deployment
A few thoughts before we start.

These hands-on projects work the best when you
โ€ข follow along and do the coding as well,
โ€ข understand why and how things work,
โ€ข and try to bring what you built to the next level.
Read 29 tweets
11 Oct
There is much more to machine learning than training models.

Most courses focus exclusively on this, but this is just a small part of the pipeline.

Here are the skills that will make you a true full stack machine learning engineer. โ†“
1. git

Breaking things is an inevitable consequence of building. Once your projects become serious, smashing Ctrl + Z won't get you out of trouble anymore.

This is where version control comes into play, which is essential to learn. (Especially when working in teams.)
Learning git can seem difficult at first because of the extensive use of the command line.

To start, I recommend these interactive tutorials:

โ€ข Git Immersion (gitimmersion.com/index.html)
โ€ข Learn Git Branching (learngitbranching.js.org)
Read 22 tweets
8 Oct
The reason PhD school is difficult is not because of the research.

Besides that, there are several key choices whose importance is underestimated by the students. Most of them are unrelated to your hard skills.

Here are the most impactful ones. โ†“
1. Picking your advisor.

Young researchers usually value fame and prestige over personal relations. However, your advisor and your fellow labmates will determine your everyday work environment.

Don't sacrifice this for some scientific pedigree.
A healthy relationship with your advisor is essential for your professional performance. Pick someone who is not only a good scientist but a good person as well. Avoid abusive personalities.

Interview students and lab alumni about your prospective advisor if you can.
Read 16 tweets
5 Oct
There is one big reason we love the logarithm function in machine learning.

Logarithms help us reduce complexity by turning multiplication into addition. You might not know it, but they are behind a lot of things in machine learning.

Here is the entire story.

๐Ÿงต ๐Ÿ‘‡๐Ÿฝ
First, let's start with the definition of the logarithm.

The base ๐‘Ž logarithm of ๐‘ is simply the solution of the equation ๐‘Žหฃ = ๐‘.

Despite its simplicity, it has many useful properties that we take advantage of all the time.
You can think of the logarithm as the inverse of exponentiation.

Because of this, it turns multiplication into addition. Exponentiation does the opposite: it turns addition into multiplication.

(The base is often assumed to be a fixed constant. Thus, it can be omitted.)
Read 10 tweets
4 Oct
As you know, I am working on teaching mathematics in a way that maximizes value for machine learning practitioners.

Do you have any work stories where mathematical knowledge was a genuine advantage?

I would appreciate it if you could share!

I'll start. โ†“
As a bioimage analyst, one of my projects involved the pixel-perfect identification of very thin objects: plant seedlings. (Like below.)

This was a classical semantic segmentation problem.

At first, I trained a UNet model using cross-entropy loss, but it didn't quite work.
The problem was that on the segmentation output, objects were not defined at all. My model predicted almost every pixel as background.

With some basic mathematical thinking, I suspected that the problem is caused by the cross-entropy loss.
Read 8 tweets
30 Sep
๐Ÿค” Should you learn mathematics for machine learning?

Let's do a thought experiment! Imagine moving to a new country without speaking the language and knowing the way of life. However, you have a smartphone and a reliable internet connection.

How do you start exploring?

1/8
With Google Maps and a credit card, you can do many awesome things there: explore the city, eat in nice restaurants, have a good time.

You can do the groceries every day without speaking a word: just put the stuff in your basket and swipe your card at the cashier.

2/8
After a few months, you'll start to pick up some language as wellโ€”simple things, like saying greetings or introducing yourself. You are off to a good start!

There are built-in solutions for common tasks that just work. Food ordering services, public transportation, etc.

3/8
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(