But what is a convolution?
Discrete convolutions, from probability to image processing and FFTs. Video on the continuous case: https://youtu.be/IaSGqQa5O-M Help fund future projects: https://www.patreon.com/3blue1brown Special thanks to these supporters: https://3b1b.co/lessons/convolutions#thanks An equally valuable form of support is to simply share the videos. Other videos I referenced Live lecture on image convolutions for the MIT Julia lab https://youtu.be/8rrHTtUzyZA Lecture on Discrete Fourier Transforms https://youtu.be/g8RkArhtCc4 Reducible video on FFTs https://youtu.be/h7apO7q16V0 Veritasium video on FFTs https://youtu.be/nmgFG7PUHfo A small correction for the integer multiplication algorithm mentioned at the end. A “straightforward” application of FFT results in a runtime of O(N * log(n) log(log(n)) ). That log(log(n)) term is tiny, but it is only recently in 2019, Harvey and van der Hoeven found an algorithm that removed that log(log(n)) term. Another small correction at 17:00. I describe O(N^2) as meaning "the number of operations needed scales with N^2". However, this is technically what Theta(N^2) would mean. O(N^2) would mean that the number of operations needed is at most constant times N^2, in particular, it includes algorithms whose runtimes don't actually have any N^2 term, but which are bounded by it. The distinction doesn't matter in this case, since there is an explicit N^2 term. Thanks to these viewers for their contributions to translations Hebrew: Omer Tuchfeld Italian: Emanuele Vezzoli Vietnamese: lkhphuc -------- These animations are largely made using a custom python library, manim. See the FAQ comments here: https://www.3blue1brown.com/faq#manim https://github.com/3b1b/manim https://github.com/ManimCommunity/manim/ You can find code for specific videos and projects here: https://github.com/3b1b/videos/ Music by Vincent Rubinetti. https://www.vincentrubinetti.com/ Download the music on Bandcamp: https://vincerubinetti.bandcamp.com/album/the-music-of-3blue1brown Stream the music on Spotify: https://open.spotify.com/album/1dVyjwS8FBqXhRunaG5W5u Timestamps 0:00 - Where do convolutions show up? 2:07 - Add two random variables 6:28 - A simple example 7:25 - Moving averages 8:32 - Image processing 13:42 - Measuring runtime 14:40 - Polynomial multiplication 18:10 - Speeding up with FFTs 21:22 - Concluding thoughts ------------------ 3blue1brown is a channel about animating math, in all senses of the word animate. And you know the drill with YouTube, if you want to stay posted on new videos, subscribe: http://3b1b.co/subscribe Various social media stuffs: Website: https://www.3blue1brown.com Twitter: https://twitter.com/3blue1brown Reddit: https://www.reddit.com/r/3blue1brown Instagram: https://www.instagram.com/3blue1brown Patreon: https://patreon.com/3blue1brown Facebook: https://www.facebook.com/3blue1brown
But what is a convolution?
Introduction to Convolution
In this video, the speaker introduces the concept of convolution and its applications in various fields. The speaker explains how convolution is a fundamental operation for combining two lists of numbers or functions.
Combining Lists and Functions
- Two lists of numbers or functions can be combined by adding them together term by term.
- Another way to combine two lists or functions is to multiply them term by term.
- Convolution is a new kind of combination that is just as fundamental as addition and multiplication but less commonly discussed.
- Convolution is used in image processing, probability theory, solving differential equations, and multiplying polynomials.
Motivation for Convolution
- The formulaic definition of convolution can look intimidating without context, but it's an incredibly beautiful operation when unpacked.
- Visualizing convolving two different functions can lead to insights about normal distributions in probability theory.
Discrete Case
- This video will focus on the discrete case of convolution and building up to an algorithm for computing convolutions.
- Probability will be used as an example to introduce convolution before moving onto other applications like image processing.
Rolling Dice Example
- Rolling a pair of dice and figuring out the chances of seeing various sums is a simple example used to introduce probability-based convolutions.
- All pairs with a constant sum are visible along one diagonal when arranged in a grid.
Alternative Visualization
- Different ways exist to visualize the same question of all the distinct pairs that have a given sum.
- A special set of dice that's not uniform can be used to calculate probabilities.
Understanding Convolution
In this section, the speaker explains what convolution is and how it works. They use examples to illustrate the concept of convolution.
What is Convolution?
- Convolution is a process where two different arrays of numbers are taken, flipped around, lined up at various off-set values, multiplied pairwise and added up.
- The result of this process is a new sequence of numbers that represents the sum of pairwise products.
- This operation can be represented mathematically as the convolution of two sequences A and B.
- The Nth element in the resulting sequence is equal to the sum over all pairs of indices I and J such that I + J = N.
Examples of Convolution
- To illustrate convolution, consider taking two lists: [1, 2, 3] and [4, 5, 6]. Flip one list around and slide it over the other list. At each position where they overlap multiply corresponding elements together and add them up. This gives us a new sequence [4, 13, 28, 27, 18].
- Another example where convolution is useful is when calculating moving averages. By taking a smaller list with values that add up to one (e.g., [0.2, 0.2, 0.2, 0.2]), we can smooth out data by multiplying each value in our data by these weights and adding them all together.
Blurring Images with Convolution
In this section, the speaker explains how convolution can be used to blur images.
Blurring Images with Convolution
- By performing a two-dimensional convolution on an image, we can blur it. This is done by taking a small matrix of values (e.g., [0.2, 0.2, 0.2; 0.2, 0.2, 0.2; 0.2, 0.2, 0.2]) and sliding it over the image at each position where they overlap multiplying corresponding elements together and adding them up.
- The result is a new image that has been smoothed out or blurred.
Conclusion
In this section, the speaker concludes the video by summarizing what was covered.
Conclusion
- Convolution is a mathematical operation that involves multiplying and summing pairwise products of two sequences of numbers.
- It can be used to calculate moving averages and blur images.
- Understanding convolution is important in fields such as signal processing and computer vision.
- Thank you for watching!
Image Processing with Convolution
In this section, the speaker explains how image processing works using convolution. They demonstrate how to blur an image and detect edges by multiplying a grid of values with corresponding pixels.
Blurring an Image
- The speaker demonstrates how to blur an image using convolution.
- A little grid of values is multiplied by each pixel in the image, giving more weight to the central pixel and less weight to those at the edge.
- This process is repeated for every single pixel in the image, resulting in a blurred version of the original.
- The speaker notes that this process can be modified by choosing a different grid of values, such as one sampled from a Gaussian distribution.
Detecting Edges
- The speaker demonstrates how to use convolution to detect vertical or horizontal edges in an image.
- By choosing a kernel with positive and negative values arranged in a specific pattern, it's possible to pick up on variations in pixel value as you move from left to right or top to bottom.
- This technique can be used for other types of image processing effects besides blurring and edge detection, such as sharpening.
- The length of the output can also be adjusted depending on what you want to achieve.
Introduction to Convolution
In this section, the instructor introduces convolution and explains how it is used in mathematics and programming.
What is Convolution?
- Convolution is a mathematical operation that combines two functions to produce a third function.
- It involves flipping one of the functions and sliding it over the other function, multiplying them together at each point, and then integrating the results.
- This process can be used to solve problems in probability theory, signal processing, image processing, and more.
Why Use Convolution?
- Although convolution may seem strange or unnecessary at first glance, it is actually a natural operation that arises from pure math contexts such as probability theory.
- In programming, using convolution can lead to much faster algorithms for computing certain operations.
- For example, using FFT convolve instead of convolve from the Numpy library resulted in a three orders of magnitude improvement in runtime for computing pairwise products between two lists of numbers.
Polynomials and Convolution
In this section, the instructor explains how convolution can be used to expand polynomials and collect like terms.
Expanding Polynomials with Convolution
- When multiplying two polynomials together, you can think of it as expanding out their full product by creating a multiplication table with all pairwise products.
- Collecting like terms along the diagonals corresponds to taking a convolution of the coefficients of each polynomial.
- This allows us to perform simple point-wise operations on two different functions by first extracting their coefficients (assuming they are polynomials) and then taking a convolution of those two lists of coefficients.
Advantages of Using Point-Wise Operations
- Multiplying two polynomials using point-wise operations only requires as many operations as there are samples taken from each polynomial's output.
- This is because with polynomials you only need finitely many samples to be able to recover the coefficients.
- In contrast, expanding out the product of two polynomials requires performing a large number of operations that scales in proportion to the square of N (the size of each list).
The Fast Fourier Transform Algorithm
In this section, the speaker explains how to use the fast Fourier transform algorithm to compute convolutions in a more efficient way.
Using Polynomials to Compute Convolution
- To compute convolutions efficiently, we can treat two lists of numbers as coefficients of two polynomials and sample those polynomials at enough outputs.
- We then multiply those samples point-wise and solve the system to recover the coefficients as a sneaky backdoor way to find the convolution.
The Discrete Fourier Transform
- By evaluating on a very specially selected set of complex numbers that sit evenly spaced on the unit circle, we can generate a system with a lot of redundancy in the different terms that you're calculating.
- This set of outputs is called the discrete Fourier transform (DFT) of the coefficients.
- There exists an algorithm called fast Fourier transform (FFT) that allows us to go from coefficients to all DFT outputs and vice versa.
Applying FFT Algorithm for Convolution
- To compute convolutions using FFT algorithm, first compute FFT for each list of numbers treating them like they're coefficients of a polynomial and evaluating it at a very specially selected set of points.
- Then multiply together the two results that you just got, point wise, which is nice and fast.
- Finally, do an inverse FFT and what that gives you is the sneaky back door way to compute convolution much faster than traditional methods.
Introduction to Continuous Case and Probability Distributions
In this section, the speaker introduces the continuous case with a special focus on probability distributions.
Continuous Case and Probability Distributions
- The speaker introduces the continuous case with a special focus on probability distributions.