# How Fourier Transform is Used in Deep Learning?

In machine studying or deep studying, the fashions are designed in such a method that they observe a mathematical perform. From knowledge evaluation to predictive modelling there is all the time some arithmetic behind it. For instance, in clustering, we use the euclidean distance to seek out out the clusters. Fourier remodel is additionally a well-known mathematical approach for remodeling the perform from one area to a different area which may also be used in deep studying. In this text, we will probably be discussing the Fourier remodel and can perceive how it may be utilized in the sector of deep studying.  The main factors to be coated in this text are listed under.
What is a Fourier Transform?Mathematics Behind Fourier TransformFourier Transform utilizing PythonHow are Neural Networks Related to Fourier Transforms?Fourier Transform in Convolutional Neural NetworkHow to make use of Fourier Transforms in Deep Learning?
What is a Fourier Transform?
In arithmetic, the transformation approach is used for mapping a perform into a unique perform area from its authentic perform area, and once we discuss in regards to the Fourier remodel it is additionally a metamorphosis approach that transforms such capabilities that are relying on the time area area area into such perform which will depend on the spatial area or temporal frequency area. We can take audio waves for example the place Fourier transformation permits us to characterize it in phrases of the volumes and frequencies of its notes.
We can say that the transformation carried out by the Fourier remodel of any perform is a perform of frequency. Where the magnitude of the resultant perform is the illustration of the frequency contained by the unique perform.
Let’s take an instance of a sign whose perform of time-domain seems to be as follows:

Let’s take a portion of one other sign in the identical timeframe

Let the title of the indicators be A(n) and B(n) the place n is the time area. So if we add these indicators the construction of the indicators will look as follows:
C(n)  = A(n) + B(n)

So right here we will see that the addition of the sign of the capabilities has one other type of a sign of every other perform and if we speak about to extraction of the sign A or B from this addition sign C it turns into an issue for us as a result of the addition of those sign occurred solely in the aptitudes, not in the time. The addition is having an analogous time however totally different magnitude.
Here the Fourier remodel permits us to separate the perform from their addition utilizing its behaviour of conversion between time area to frequency area. In addition, we discover that we will extract the frequencies from the sign however separating them utilizing the time area is not attainable. The separation of indicators utilizing the Fourier remodel will seem like the under picture.

Image supply
In the above picture, we will see that we will simply mark the distinction between the sign now if once more we need to get these indicators in the time area we will convert them utilizing the inverse Fourier remodel.
In the subsequent part of the article, we are going to talk about the arithmetic behind the Fourier remodel in order that our view may be extra clear in regards to the continuing of the Fourier remodel.
Mathematics Behind Fourier Transform
Before going into the arithmetic of Fourier tell us that the illustration of a sign in the time area may be executed by the collection of sinusoidal. So if the perform consists of a steady sign it may be represented by the perform f(t).
Where,

We can see the perform is made up of the addition of the infinite sinusoids which may be thought of because the illustration of the sign of perform. Also, we will see there are in the perform two coefficients which are essential to outline the construction of the output sign.
These coefficients may be obtained by fixing the Fourier remodel integral which is mainly a perform of the frequency. The resultant of the Fourier remodel may be thought of because the set of coefficients Mathematically it may be written as given under:
Fourier Transform

And the inverse of this perform may be thought of because the perform of time which we use for changing the frequency area perform to the time area perform.
Inverse Fourier Transform

Solving these above integral offers the worth of a and b however right hereThe case we’re speaking about is a case the place the indicators are steady indicators in actual life many of the issues are accrued from the discreetly sampled indicators and to seek out out the coefficient for such indicators transformation we’re required to carry out a discrete Fourier remodel (DFT). Using DFT we will get a same-length sequence of equally-spaced samples of a perform that is already made up of a sequence of equally-spaced samples. The coefficients for the above-given perform f(t) may be obtained by the next perform.

The worth of a and b will probably be,

Using these phrases a and b in the perform f(t) we will discover the indicators in the frequency area. Here in this part, we now have seen the arithmetic behind the Fourier remodel. Now let’s simply see how can we carry out on sine waves utilizing python.
Fourier Transform Using Python
In this part of the article, we’re going to remodel the addition of two sine waves into their frequency domains for this function we will use the scipy library of python which mainly supplies us with all of the transformations methods we use in arithmetic.
Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq
Making the sine waves and including them
#  pattern factors
N = 1200

# pattern spacing
T = 1.0 / 1600.0

x = np.linspace(0.0, N*T, N, endpoint=False)
sum = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)

plt.plot(sum)
plt.title(‘Sine wave’)
plt.xlabel(‘Time’)
plt.ylabel(‘Amplitude’)
plt.grid(True, which=”each”)
plt.present()
Output:

Here in the above output, we will see the sine waves which we now have generated utilizing NumPy now we will remodel it utilizing the FFT module of the scipy library.
sumf = fft(sum)
xf = fftfreq(N, T)[:N//2]
plt.ylabel(‘frequency’)
plt.xlabel(‘pattern’)
plt.title(“FFT of sum of two sines”)
plt.plot(xf, 2.0/N * np.abs(sumf[0:N//2]))
plt.present()
Output:

Here we will clearly see what are the frequencies of various waves which was not clear in the addition as a result of earlier than this the wave was generated because the perform of the time area. As of now, we now have seen the assorted insights of the Fourier transformation however now the query is the way it is associated to the neural community? By above all instinct we will say that the Fourier remodel is additionally a method to approximate different capabilities in the frequency area which makes it associated to neural networks. In the subsequent part of this text, we are going to see how neural community and Fourier remodel is associated.
How are Neural Networks Related to Fourier Transforms?
As of now, we see the Fourier remodel as a perform that may assist in approximating different capabilities and in addition we all know that the neural networks may also be thought of because the perform approximation approach or common perform approximation approach. Let’s take a look on the under picture.

Image supply
This picture is a illustration of the neural community in which the Fourier remodel approach is used. A fundamental neural community comes with the motivation to approximate a perform that is unknown and its worth at some given factors. The majority of neural networks have a job to study the general perform or study the perform on the values level which is given in the algorithm or knowledge the place iteration methods assist parameters to study in accordance with the scenario the place Fourier community finds the parameters by evaluating the given perform. The method used in the precise perform helps in the computation of the parameter.
Let’s take an instance of a convolutional neural community to know this relationship extra in-depth. In the subsequent part of the article, we are going to talk about the connection of Fourier remodel with neural community in the context of a convolutional neural community.
Fourier Transform in Convolutional Neural Network
As we all know in regards to the convolutional neural community, the convolutional layers are the principle base of such form of community and in the community, the principle work of any convolutional layer is to use the filter to the enter knowledge or to the characteristic maps. So that it could convolution the output from the earlier layer. The job of the layer is to study the burden of the filter. We see in a fancy convolutional neural community that the variety of layers is excessive and the filters per layer are additionally excessive which makes the calculation price very excessive.
Using the Fourier remodel into the convolutional neural community we will remodel the layers calculation in element-wise merchandise in the frequency area and the duty of the community would be the similar. We can simply save the calculator vitality by utilizing the Fourier remodel. Most of the circumstances we see are making use of the quick Fourier remodel in the convolutional community.
By the above, we will say that the convolutional layer or the method of the convolutional layer is associated to the Fourier remodel. Mostly the convolutional layers in the time area may be thought of because the multiplication in the frequency area. We can simply perceive the convolutional by the polynomial multiplication.
Let’s say we now have to perform y and g of any worth x like under:

y(x) = ax + b
g(x) = cx + d
And the polynomial multiplication of those capabilities may be written by a perform h
h(x) = y(x).g(x)
= (ax + b)(cx + d)
= ac x^2 + (advert+bc) x + bd
By the above, we will say that the convolutional layer course of may be outlined because the multip[lication of these above-given functions. The vector form of  the functions can be written as the following:
y[n] = ax[n] + b
g[n] = cx[n] + d
And the vector multiplication of the vector types is:
h[n] = y[n] X g[n]
H[w] = F(y[n]) ‧ F(g[n]) = Y[w] ‧ G[w]
h[n] = F^-1(H[w])
The notation ‘.’ in the multiplication represents the multiply and X is convolutional. The F and F^-1 are Fourier remodel and inverse Fourier remodel respectively. “n” and “w” donate time area and frequency area respectively.
By the above, we now have confirmed that finally the convolutional layer implies the Fourier remodel and its inverse in the multiplication if the capabilities are associated to the time area. We have seen in this part how the Fourier transformation takes half in the convolution course of. In the subsequent part of the article, we are going to find out how we will use the Fourier in the deep studying algorithms.
How to make use of Fourier Transforms in Deep Learning?
In the above part, we now have seen that the convolution course of in the time area may be merely thought of because the multiplication in the frequency area. Which is proof that it may be used in the assorted deep studying algorithms regardless that it may be used in the assorted static predictive modelling algorithms. In the above part of the article, we now have seen the way it helps in convolutional neural networks and the way it replaces the convolution technique of the convolutional layer. Let’s go together with an analogous instance of the convolutional neural community so that we’ll not get diverted from the principle topic of the article.
As we now have mentioned and seen that the arithmetic behind the convolution is to carry out multiplication in the time area and the arithmetic behind the Fourier remodel is to do multiplication in the frequency area. So to use the Fourier remodel in any convolutional neural community we will carry out some modifications in the enter and the filter.
If the matrices of the enter and filters in the CNN may be transformed into the frequency area to carry out the multiplication and the end result matrices of the multiplication in the frequency area may be transformed into the time area won’t carry out any hurt to the accuracy of the mannequin. The conversion of matrices from the time area to the frequency area may be executed by the Fourier remodel or quick Fourier remodel and conversion from the frequency area to the time area may be executed by the inverse Fourier remodel or inverse quick Fourier remodel.
The under picture is a illustration of how we will use Fast Fourier Transform in place of the convolution.

As we now have mentioned the variety of filters and layers in any complicated community is very excessive and due to the upper numbers, the calculation course of utilizing the convolution turns into very sluggish. Whereas, utilizing the Fourier remodel we will scale back the complexity of such calculation and might make the mannequin work sooner.
Final Words
In this text, we now have seen a fundamental definition of the Fourier remodel together with arithmetic behind it and the way we will carry out it utilizing python. With all this, we get to understand how the neural networks and Fourier remodel are associated. To perceive this relation, we now have taken an instance of the convolutional neural community and seen how we will use the Fourier remodel in deep studying fashions like convolutional neural networks.