-
Notifications
You must be signed in to change notification settings - Fork 0
Add Fourier Embedding layer #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -47,6 +47,7 @@ | |||||
| import torch.optim as optim | ||||||
| import numpy as np | ||||||
| from enum import Enum | ||||||
| import warnings | ||||||
| from utils import parse_args, get_activation, print_args, save_frame, make_video_from_frames | ||||||
| from utils import is_notebook, cleanfiles, fourier_analysis, get_scheduler_generator, scheduler_step | ||||||
| # from SOAP.soap import SOAP | ||||||
|
|
@@ -126,21 +127,60 @@ def set_pde(self, pde: PDE): | |||||
|
|
||||||
|
|
||||||
| # %% | ||||||
| class FourierEmbedding(nn.Module): | ||||||
| def __init__(self, dim_inputs:int, half_dim_output:int, sigma:int=5): | ||||||
| """ | ||||||
| Fourier Features Embedding for the input data. This can help learning high frequency functions. | ||||||
| Args: | ||||||
| dim_inputs: Dimension of the input data | ||||||
| half_dim_outputs: The output dimension is 2*half_dim_output. | ||||||
| sigma: Scaling factor for the frequencies. Recommended is [1, 10] | ||||||
| Ref: https://arxiv.org/abs/2006.10739 | ||||||
| """ | ||||||
| super().__init__() | ||||||
| self.sigma = sigma | ||||||
| m = half_dim_output # number of frequencies pairs (cos, sin) | ||||||
| B = torch.rand(m, dim_inputs) * sigma | ||||||
|
||||||
| B = torch.rand(m, dim_inputs) * sigma | |
| B = torch.randn(m, dim_inputs) * sigma |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to find a way to make this part more comprehensible.
Basically, if fourier embedding is enabled with output dimension 2m=hidden. The dimension of m is defined by first hidden layer argument.
Fourier layer: dim_input -> 2m
First linear: hidden -> hidden
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there is a better way to ddefine this like
FourierLayer(input, output, sigma)
but how to make argument simple is challenging
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -101,6 +101,7 @@ def parse_args(args=None): | |||||
| help="Configuration for learning rate scheduler. " | ||||||
| "Follow https://docs.pytorch.org/docs/stable/optim.html for full list of schedulers. " | ||||||
| "The setting is corresponding to `--scheduler` setting.") | ||||||
| parser.add_argument("--fourier_embedding_sigma", type=float, default=-1, help="Sigma for Fourier embedding. Recommended [1,10] ") | ||||||
|
||||||
| parser.add_argument("--fourier_embedding_sigma", type=float, default=-1, help="Sigma for Fourier embedding. Recommended [1,10] ") | |
| parser.add_argument("--fourier_embedding_sigma", type=float, default=None, help="Sigma for Fourier embedding. Recommended [1,10] ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be Gaussian not rand