NanoComp / meep

free finite-difference time-domain (FDTD) software for electromagnetic simulations
GNU General Public License v2.0
1.24k stars 626 forks source link

Vanishing gradients #2374

Closed kdotom closed 1 year ago

kdotom commented 1 year ago

This is likely user error, but:

My goal is to optimize a photonic waveguide crossing in 3D by using the 04-Splitter.ipynb as a template. The structure optimizes just fine in 2D, but when moving to 3D, for whatever reason, the gradients appear to be vanishing after about iteration 2 of each beta epoch.

The setup has:

So far, I've:

What I'm seeing is:

For mapping, I've tried:

As noted at the beginning, the setup works just fine when optimizing in 2D, but struggles with 3D.

I might be missing something simple, so just wanted to reach out and see if anyone has seen and solved this.

Thanks in advance. -K

smartalecH commented 1 year ago

I don't see anything "obviously wrong" from your description above. If it works fine in 2D, just not in 3D, then it's probably something with the simulation itself (ie not with the optimization, the gradient computation, or even the hyperparameters).

So maybe look a bit more carefully at the FOM itself and the values that are being returned. Do they seem logical? Does their evolution seem logical? You mentioned you are taking a mean... look at all the individual FOMs and try to pick up on any trends. Make sure they are all on the same scale and that there aren't any weird outliers.

I would look at the fields themselves at a few different points in time too. Maybe even swap in a CW source.

It would be worth it to look at all the individual gradients as a sanity check too.

The goal is to figure out where in the pipeline things are breaking down. This is tricky, since the pipeline consists of so many steps that could have bugs. So start with some assumptions to narrow down your search, and work from there.

(@Oskooi we mentioned at one point we should add a page dedicated toward debugging adjoint optimization problems... we should probably revisit that)

oskooi commented 1 year ago

My goal is to optimize a photonic waveguide crossing in 3D by using the 04-Splitter.ipynb

In addition to the notebook examples, it might be useful to review this recently added tutorial which provides helpful tips for using the adjoint-solver module: https://meep.readthedocs.io/en/latest/Python_Tutorials/Adjoint_Solver/#broadband-waveguide-mode-converter-with-minimum-feature-size.

(We are also planning to add 3d tutorial examples. Stay tuned.)

kdotom commented 1 year ago

I put visuals on everything to check placement and looked at values at different points in the pipeline. FOM values were going the other direction, so (after a bit of reading) I rebuilt it ab initio and used an even simpler cost function than I was using. + It's now working! Thanks for the tips @oskooi @smartalecH