Spectral Derivative with FFT in NumPy

One can use the Fast-Fourier Transform to obtain derivatives of functions on periodic domains. This builds the basis of any spectral method. A crucial ingredient is the creation of the wavenumber vector. Here is the code: github.com/Ceyron/machine-lea...
-----
👉 This educational series is supported by the world-leaders in integrating machine learning and artificial intelligence with simulation and scientific computing, Pasteur Labs and Institute for Simulation Intelligence. Check out simulation.science/ for more on their pursuit of 'Nobel-Turing' technologies (arxiv.org/abs/2112.03235 ), and for partnership or career opportunities.
-------
📝 : Check out the GitHub Repository of the channel, where I upload all the handwritten notes and source-code files (contributions are very welcome): github.com/Ceyron/machine-lea...
📢 : Follow me on LinkedIn or Twitter for updates on the channel and other cool Machine Learning & Simulation stuff: / felix-koehler and / felix_m_koehler
💸 : If you want to support my work on the channel, you can become a Patreon here: / mlsim
🪙: Or you can make a one-time donation via PayPal: www.paypal.com/paypalme/Felix...
-------
⚙️ My Gear:
(Below are affiliate links to Amazon. If you decide to purchase the product or something else on Amazon through this link, I earn a small commission.)
- 🎙️ Microphone: Blue Yeti: amzn.to/3NU7OAs
- ⌨️ Logitech TKL Mechanical Keyboard: amzn.to/3JhEtwp
- 🎨 Gaomon Drawing Tablet (similar to a WACOM Tablet, but cheaper, works flawlessly under Linux): amzn.to/37katmf
- 🔌 Laptop Charger: amzn.to/3ja0imP
- 💻 My Laptop (generally I like the Dell XPS series): amzn.to/38xrABL
- 📱 My Phone: Fairphone 4 (I love the sustainability and repairability aspect of it): amzn.to/3Jr4ZmV
If I had to purchase these items again, I would probably change the following:
- 🎙️ Rode NT: amzn.to/3NUIGtw
- 💻 Framework Laptop (I do not get a commission here, but I love the vision of Framework. It will definitely be my next Ultrabook): frame.work
As an Amazon Associate I earn from qualifying purchases.
-------
Timestamps:
00:00 Intro
00:20 Creating a mesh (without the right boundary point)
01:11 A simple function and its plot
01:59 Analytical derivative and its plot
03:12 Rough implementation of the spectral derivative
03:45 A first attempt of getting the wavenumbers
04:06 Plot of the first attempt and fix a complex warning
05:01 Fix the wavenumber setup
06:26 Summary
07:57 Outro

Пікірлер: 16

  • @ThomasBorge
    @ThomasBorge8 ай бұрын

    Just saw this pop up, greetings from the exchange student from Norway : )

  • @MachineLearningSimulation

    @MachineLearningSimulation

    8 ай бұрын

    Nice, thanks Thomas ♥️ Glad I made you subscribe back in the days 😉

  • @invinity3982
    @invinity39828 ай бұрын

    Beautiful explained. Very helpful and exactly what I needed. Thank you

  • @MachineLearningSimulation

    @MachineLearningSimulation

    8 ай бұрын

    You're very welcome 🤗 I'm glad it was helpful.

  • @tagoma3504
    @tagoma35048 ай бұрын

    A lot of valuable cmments in this video. Many thanks.

  • @MachineLearningSimulation

    @MachineLearningSimulation

    8 ай бұрын

    You are welcome! 😊

  • @SrEngr
    @SrEngr8 ай бұрын

    I like to plot the points of actual on x-axis and predicted on y-axis. Sometimes I like to plot actual on x-axis and difference between actual and predicted on the y-axis. This can give a diagnostic for fit.

  • @MachineLearningSimulation

    @MachineLearningSimulation

    8 ай бұрын

    Sure, that's also a good indicator of how close the two functions are 👍

  • @h.e.a311
    @h.e.a3118 ай бұрын

    thank you excellent

  • @MachineLearningSimulation

    @MachineLearningSimulation

    8 ай бұрын

    Welcome 😊

  • @nicklam3594
    @nicklam35948 ай бұрын

    Spectral methods! Noice

  • @sibsdts
    @sibsdts2 ай бұрын

    Thank you, really enjoy your videos, very insightful ! I try to reduplicate your codes with jax.numpy instead of numpy, and i find that the jnp.fft.irfft is quite imprecise (1e-6) compared to the numpy one (1e-15), even with double precision enabled. I wonder if it is a bug or if it is due the way they implement irfft with XLA...

  • @MachineLearningSimulation

    @MachineLearningSimulation

    Ай бұрын

    Thanks so much for the kind feedback ❤️ That's an interesting observation. I don't think it should be that imprecise. Did you double check that your JAX array is indeed double precision (for example via ARRAY_NAME.dtype). And also check if the array in Fourier space has complex double precision. You could also check if this occurs on CPU and on GPU. For matrix multiplications on the GPU, JAX has a flag to set the precision. Maybe it has sth similar for FFT, although I doubt that. :o

  • @sibsdts

    @sibsdts

    Ай бұрын

    @@MachineLearningSimulation Thank you for your input. Yes, i do have float64 and complex128 precision enabled for the array and its fourier space. When i switch to CPU, i do have good errors (1e-15). So i guess the problem stems from the jnp.ifft implementation for GPU... For torch, i got good results with both CPU and GPU.

  • @galenseilis5971
    @galenseilis59718 ай бұрын

    Amazing! Does the process work if I create a 2D grid and use np.fft.fft2 together with np.fft.ifft2? I am hoping that would give me a spectral approach to computing a Hessian (by repeating this pairwise for each pair of variables). And just to be general, could we similarly use np.fft.fftn and np.fft.ifftn?

  • @MachineLearningSimulation

    @MachineLearningSimulation

    5 ай бұрын

    Hi, Thanks for the kind feedback and the nice question! :) I will do a video on 2D spectral derivatives in a future video. One has to be careful to take the derivatives in the correct direction. Let's assume you have the 1d "mesh" like in the video (call it lowercase "x" for now). You also have the same "k". Let's assume the domain has the same extent in both directions with the same number of degrees of freedom. You would get the spatial mesh by "X, Y = np.meshgrid(x, x)" and the wavenumber mesh as "K_X, K_Y = np.meshgrid(k, k)". You then have a function discretized on X & Y and call the pointwise values "f". Its first spectral derivative in the x direction is then "ifft2(1j * K_X * fft2(f)).real". To assemble a hessian matrix, you need the second derivative in x, the second in y, and the mixed derivative. You get them as : 1. "ifft2(- K_X * K_X * fft2(f)).real" 2. "ifft2(- K_Y * K_Y * fft2(f)).real" 3. "ifft2(- K_Y * K_X * fft2(f)).real" respectively. This concept of taking spectral derivatives generalizes to higher dimensions (with the "fftn" you mentioned). For example, in 3d, you would do "X, Y, Z = np.meshgrid(x, x, x)" and "K_X, K_Y, K_Z = np.meshgrid(k, k, k)". A common gotcha is that you need the same order/indexing strategy for the wavenumbers as you do in creating the spatial mesh. This is controlled with the "indexing" keyword argument in the meshgrid function: numpy.org/doc/stable/reference/generated/numpy.meshgrid.html . If you use the default "ij" style for both, you are fine. Hope that helped :)