Code Monkey home page Code Monkey logo

neuraloperator's Introduction

PyPI

image

Neural Operator

neuraloperator is a comprehensive library for learning neural operators in PyTorch. It is the official implementation for Fourier Neural Operators and Tensorized Neural Operators.

Unlike regular neural networks, neural operators enable learning mapping between function spaces, and this library provides all of the tools to do so on your own data.

NeuralOperators are also resolution invariant, so your trained operator can be applied on data of any resolution.

Installation

Just clone the repository and install locally (in editable mode so changes in the code are immediately reflected without having to reinstall):

git clone https://github.com/NeuralOperator/neuraloperator
cd neuraloperator
pip install -e .
pip install -r requirements.txt

You can also just pip install the library:

pip install neuraloperator

Quickstart

After you've installed the library, you can start training operators seemlessly:

from neuralop.models import FNO

operator = FNO(n_modes=(16, 16), hidden_channels=64,
                in_channels=3, out_channels=1)

Tensorization is also provided out of the box: you can improve the previous models by simply using a Tucker Tensorized FNO with just a few parameters:

from neuralop.models import TFNO

operator = TFNO(n_modes=(16, 16), hidden_channels=64,
                in_channels=3, 
                out_channels=1,
                factorization='tucker',
                implementation='factorized',
                rank=0.05)

This will use a Tucker factorization of the weights. The forward pass will be efficient by contracting directly the inputs with the factors of the decomposition. The Fourier layers will have 5% of the parameters of an equivalent, dense Fourier Neural Operator!

Checkout the documentation for more!

Using with weights and biases

Create a file in neuraloperator/config called wandb_api_key.txt and paste your Weights and Biases API key there. You can configure the project you want to use and your username in the main yaml configuration files.

Contributing code

All contributions are welcome! So if you spot a bug or even a typo or mistake in the documentation, please report it, and even better, open a Pull-Request on GitHub. Before you submit your changes, you should make sure your code adheres to our style-guide. The easiest way to do this is with black:

pip install black
black .

Running the tests

Testing and documentation are an essential part of this package and all functions come with uni-tests and documentation. The tests are ran using the pytest package. First install pytest:

pip install pytest

Then to run the test, simply run, in the terminal:

pytest -v neuralop

Citing

If you use NeuralOperator in an academic paper, please cite1,2:

@misc{li2020fourier,
   title={Fourier Neural Operator for Parametric Partial Differential Equations}, 
   author={Zongyi Li and Nikola Kovachki and Kamyar Azizzadenesheli and Burigede Liu and Kaushik Bhattacharya and Andrew Stuart and Anima Anandkumar},
   year={2020},
   eprint={2010.08895},
   archivePrefix={arXiv},
   primaryClass={cs.LG}
}

@article{kovachki2021neural,
   author    = {Nikola B. Kovachki and
                  Zongyi Li and
                  Burigede Liu and
                  Kamyar Azizzadenesheli and
                  Kaushik Bhattacharya and
                  Andrew M. Stuart and
                  Anima Anandkumar},
   title     = {Neural Operator: Learning Maps Between Function Spaces},
   journal   = {CoRR},
   volume    = {abs/2108.08481},
   year      = {2021},
}

  1. Li, Z., Kovachki, N., Azizzadenesheli, K., Liu, B., Bhattacharya, K., Stuart, A., and Anandkumar A., “Fourier Neural Operator for Parametric Partial Differential Equations”, ICLR, 2021. doi:10.48550/arXiv.2010.08895.

  2. Kovachki, N., Li, Z., Liu, B., Azizzadenesheli, K., Bhattacharya, K., Stuart, A., and Anandkumar A., “Neural Operator: Learning Maps Between Function Spaces”, JMLR, 2021. doi:10.48550/arXiv.2108.08481.

neuraloperator's People

Contributors

ashiq24 avatar bonevbs avatar btolooshams avatar crwhite14 avatar dhpitt avatar gegewen avatar helgehr avatar imanliao avatar jeankossaifi avatar juliusberner avatar kovachki avatar m4e7 avatar robertboy18 avatar rtu715 avatar rybchuk avatar samaonline avatar slanthaler avatar sleepyeye avatar suyashbire1 avatar zijieli-jlee avatar zongyi-li avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

neuraloperator's Issues

4d application

Hi Zongyi,

Another question, would this be applicable for 4d problem? would 4d FT be a problem?

Thanks
George

Adam Optimizer Implemented Incorrectly for Complex Tensors

The pytorch 1.8 version starts to support the complex number, but the Adam optimizer hasn't been updated for the complex number (see pytorch). As a result, there could be a difference between the pytorch 1.8 implementations and the pytorch 1.6. If you would like to reproduce the results in the paper, we suggest you use the pytorch 1.6 version.

Input size

Hi zongyi,
Thanks for the amazing algorithm.
But i'm a bit confused about the input size, for example, in 'fourier_2d_time.py', in line 82, should it be (batch size, x=64, y=64, c=10)? and c=10 corresponding to 10 timesteps? Please correct me if misundertanding your code.

there are som wrong in code

fourier_neural_operator/fourier_1d.py

class SimpleBlock1d(nn.Module):
......
def forward(self, x):

    x = self.fc0(x)
    x = x.permute(0, 2, 1)

    x1 = self.conv0(x)
    x2 = self.w0(x)
    x = self.bn0(x1 + x2)
    x = F.relu(x)
    x1 = self.conv1(x)
    x2 = self.w1(x)
    x = self.bn1(x1 + x2)
    x = F.relu(x)
    x1 = self.conv2(x)
    x2 = self.w2(x)
    x = self.bn2(x1 + x2)
    x = F.relu(x)
    x = self.conv3(x)
    x2 = self.w3(x)
    x = self.bn3(x1 + x2)

......

the third row to the last,maybe you means “ x1 = self.conv3(x)”

Regarding trained model

The model you have provided can it be used for evaluating naiver stokes equation on 64*64 mesh with different boundary conditions.

Regarding data generation

Hi, I am using this method for my college project. I am solving diffusion equation with some definite initial condition(u(x,0) = 0). I have solved it in python and now want to check in NN for accuracy and time consumption with final task being predict the result at specific location in time and space.

I am using my initial condition for 256 grid as u(x,0) = 0 at every where and then save the u for every time step and try to learn it for some definite time interval and then predict the output as you used in fourier 2d_time.
My concern is that i will create 25620 point at t0 and make it (batch=20, point =256) and then again fro t1 and t2.... and then use it and same for 2d(20256*256) at t0, t1, t2...
I just want to know i am right with the data generation? Please check and let me know. Thanks.

Benchmark

Hi Zongyi Li,
Incredible work, very interesting!

I am studying this subject in my PhD.
But I am having some difficulty in obtaining the same results as figure 2 of the article (benchmark).
Do you have the codes of the other models that you used to compare with the FNO available?

Thank you,
Joao

Parameter not understood in nn.Linear(?, self.width)

Dear fellow scholars,
Great paper, but I struggle to understand the first argument in this call
1d:
self.fc0 = nn.Linear(2, self.width) [line 59]
2d:
self.fc0 = nn.Linear(3, self.width) [line 68]
2d_rnn:
self.fc0 = nn.Linear(12, self.width) [line 66]
3d:
self.fc0 = nn.Linear(13, self.width) [line 76]

Could you let me know, why 2, 3, 12, and 13?
Thanks for the amazing work :)

Data generation code for Burgers

Hi, I have another question about the data generation code for the Burgers equation. As far as I understood, in the code that you have provided here, the GRF function yields the initial condition values (1024 values at 1024 grid points) at one specific time-step. Using these initial values, the Burgers is solved at 201 time-steps and the solutions are stored in the 'Output' variable (1x201x1024 final size). Assuming my understanding is correct, I am having difficulty wrapping my head around the fields 'a' and 'u' in the 'burgers_data_R10.mat' file. If both have the same shape (2048x8192),

  1. If 'a' is the initial conditions 'u_0', how did you generate 2048 instances of it?

  2. Are these all separate initial conditions, which will all produce separate solutions 'u' for each of the instances? Or is 'a' built from the solution of Burgers at each time-step? Which I doubt because you don't really use the last time-step as your train data for y, y_train has the shape 1000x1024.

I guess I am just really confused. Could you please help me out here? Thanks!

About the spatial domain D

In your paper, you defined the spatial domain D as a bounded, open set.
And I noticed that the spatial domain D in your experiments are always (0,1) or (0,1)^2; but I think this 'fourier neural opertor' method is still practical no matter what the shape of the spatial domain D is(for example, D is a circle or a domain with some irregular shapes).
Could you please tell me if I am right or not? Thank you very much!

NSE case studies

Hello,

I was interested in the NSE implementation (fourier_2d_time.py and fourier_3d.py). I used input data NavierStokes_V1e-5_N1200_T20.zip (from the Drive), and I understand it is structured as: [number of samples x image height x image width x timestep]

Trying it out I got the following issues:

fourier_2d_time.py (uncommenting the lines to save the model and evaluate (not sure why it was commented)):

  • out = y_normalizer.decode(out) (l. 328) shows an undefined normalizer, I assume this is just an extra line because no normalization is done in this file (as opposed to fourier_3d.py where it is done).
  • test_l2 += myloss(out.view(1, -1), y.view(1, -1)).item() shows tensor size inconsistencies (model output out has 1 time-step versus 10 for y)

fourier_3d.py: (edit - ok)

Given that both loss function evaluations are fairly standard I might have omitted to adapt something to this specific case (edit: out uses single time-step output while y has the whole information - hence the error), the rest are minor fixes

Best wishes

Why permute the data in Fourier_1d.py

For line 104 ( x = x.permute(0, 2, 1)) in Fourier_1d.py, you change the axis using permute. But I am not clear why do you do that? Is this for some special reasons?

Regarding data generation

I was looking at data generation scripts so I was not getting how you selected tau, alpha, and sigma values and for each eqaution's coefficient you set different formulas for coeff so how do u obtain them. For example in below darcys equation you have different formula while for coeff for matrix you use a different one.

coef = tau^(alpha-1).*(pi^2*(K1.^2+K2.^2) + tau^2).^(-alpha/2);
	%coef = (pi^2*(K1.^2+K2.^2)).^(-alpha/2);
	% Construct the KL coefficients
	L = s*coef.*xi;
    L(1,1) = 0;

So can u tell how you select this formulas for coef for different equations.

3D spatial domain and time

Congratulations on your great job.
It would be appreciated if you specify what modifications are required for a 3D spatial and time PDE problem.

With best regards

A few questions about fourier_2d_time.py

Hello, I have a few questions about fourier_2d_time.py

There is no normalization before training, but it has de-normalization in the prediction section (line 294).
out = y_normalizer.decode(out)
I have tried to delete line 294 directly, but this will cause an error when calculating loss because the dimensions of “out” and “y” are inconsistent. I also tried to add normalization based on fourier_3d.py. The program runs successfully, but the prediction result is not quite correct. I think the reason is that the dimension of the “x” is (1,64,64,10), “out” is (1,64,64,1) and “out” after normalization is (1,64,64,10). The dimension of “out” is improved to (1,64,64,10) by normalization instead of iteration in time. I think the prediction process should be the same as the training process, which should be iterated over time. I changed the code in the prediction section, and the results looked correct.

RuntimeError: shape '[1000, 1024, 1]' is invalid for input of size 8192000

I tried running the file fourier_1d.py:

$ python fourier_1d.py 
Traceback (most recent call last):
  File "fourier_1d.py", line 161, in <module>
    x_train = torch.cat([x_train.reshape(ntrain,s,1), grid.repeat(ntrain,1,1)], dim=2)
RuntimeError: shape '[1000, 1024, 1]' is invalid for input of size 8192000

This statement is generating the exception:

x_train.reshape(ntrain,s,1)

From the documentation of view() method, 'The returned tensor shares the same data and must have the same number of elements, but may have a different size.'

Screen Shot 2020-11-03 at 11 20 09 AM

Ideas?

Getting the reported metrics in Table 3

Hi Zongyi,

In the file lowrank_2d_time.py, you print out here two metrics on the test data: test_l2_step and test_l2_full. Just wondering, which one of these two did you use to report the numbers in Table 3 (Benchmarks on Navier Stokes) of your paper?

Run time error in Fourier_1d.py, line 227

Hi, I tried to run your 1D example but ran into a problem - see below. The script crashes in reporting the loss. Any suggestions for how to fix it?

499 0.3591817901469767 5.364698648691046e-07 0.001563040729612112 0.0031099164858460425
Traceback (most recent call last):
File "fourier_1d.py", line 227, in
pred[index] = out
RuntimeError: expand(torch.cuda.FloatTensor{[1024, 1]}, size=[1024]): the number of sizes provided (1) must be greater or equal to the number of dimensions in the tensor (2)

I am using a runtime environment with
- cudatoolkit=10.1
- pytorch=1.8.1

You can implement it better!

https://github.com/zongyi-li/fourier_neural_operator/blob/90bbd4c9ffa84b91ec2daa86660148861989adec/fourier_3d.py#L53

What you can do is using FFTshift:

def roll_n(X, axis, n):
    f_idx = tuple(slice(None, None, None) if i != axis else slice(0, n, None)
                  for i in range(X.dim()))
    b_idx = tuple(slice(None, None, None) if i != axis else slice(n, None, None)
                  for i in range(X.dim()))
    front = X[f_idx]
    back = X[b_idx]
    return torch.cat([back, front], axis)

def fftshift(data):
    for dim in range(len(data.size()) - 3, len(data.size()) - 1):
        data = roll_n(data, axis=dim, n=data.size(dim) // 2)
    return data

and then slice just the middle of the tensor (instead of four different corners)

Different input domains at test time

Dear Zongyi Li,
first of all, thank you for this amazing work and the availability of this repo.

I m trying your approach as a baseline for my PhD work.
In particular, I am training your model on data defined on a 64x64 grid with $x,y \in (0,1)$.
At test time the input is $x,y \in (0.4, 0,6)$.
And, instead of a 'zoom' in the input region, the model returns the original image with artefacts.
On the contrary, if I just upsample the input domain, e.g. 512x512 with $x,y \in (0,1)$ the results are nice.
Do you have an explanation for this behaviour?

Thank you very much

ns_data_V10000_N1200_T20.mat

fourier_2d_rnn.py references 'ns_data_V10000_N1200_T20.mat':
E.g.
TRAIN_PATH = '/data/fourier_neural_operator/ns_data_V10000_N1200_T20.mat'
TEST_PATH = '/data/fourier_neural/operator/ns_data_V10000_N1200_T20.mat'

The repository state:
"We provide the Burgers equation and Darcy flow datasets we used in the paper."

Is the Navier-Stoke file: ns_data_V10000_N1200_T20.mat available?

Code for Super-Resolution

Hi,

Thanks for the open-sourced code. I am wondering how the model can be used for super-resolution, i.e., training on a lower resolution and testing on a higher one, which is section 5.4 of the paper. In the current version, it seems that the train and test resolution should be the same.

How dataset for Burgers equation created?

In your paper called "Fourier Neural Operator for Parametric Partial Differential Equations", you described how you created the Burgers equation dataset on Page 15, like below:
"The initial condition u_0(x) is generated according to u_0 ~ µ where µ = N(0, 625((-\delta+25I) ^{-2} ) with periodic boundary conditions."

I am not very clear about the initial condition and boundary condition. Could you describe the meaning of \delta and I in u_0 and describe what are the periodic boundary conditions? Thanks for your help!

reporting something odd:

Hi!

In the 2d case, the training set is 241x241, but in fourier_2d.py you have

r = 5
h = int(((421 - 1)/r) + 1)
s = h
reader = MatReader(TRAIN_PATH)
x_train = reader.read_field('coeff')[:ntrain,::r,::r][:,:s,:s]

Did you not mean h = int(((241-1)/r) + 1?
Also

x_train = reader.read_field('coeff')[:ntrain,::r,::r][:,:s,:s]
== 
x_train = reader.read_field('coeff')[:ntrain,::r,::r]

since s > x_train.shape[1]

Maybe I am misunderstanding something.
Sincerely,
Adrian

train and test data

Can you provide links of training data in the project? (fourier_2d.py line 139 and line 140 piececonst_r421_N1024_smooth1.mat)

Reproducing Results in Table 1

Hi Zongyi,

I really enjoyed reading your paper! I was having a go at reproducing your numbers in Table 1. I'm wondering if the numbers on the last row come from the test_l2 variable of the final epoch?

If I clone your repo and run python fourier_1d.py, then I have test_l2 as 0.00254360 on epoch 500. In Table 1 of your paper, you report a value of 0.0160 for the FNO model with s = 1024. This seems to be an order of magnitude bigger. Am I looking at the right metric?

About the comparative test

Hi, Zongyi Li,

This is a very good idea and very interesting!
I want to ask you if there is any code for the comparison experiment in the experiment. Thank you very much!

Which script to use for 3D FNO and missing normalizer

Congratulations on your nice paper and the stunning results. My team and I want to use your technique and thus had a closer look into your implementation. We have two questions:

  • We have a hunch that the script in scripts/fourier_3d_time.py is the one we should use for the 3D FNO rather than the one linked in the README: fourier_3d.py. The latter seems to apply the Fourier transformation to the time grid points rather than the values at each timestep.

  • However, in scripts/fourier_3d_time.py there is no normalizer (actually there is one in line 315 but it is commented) and we are wondering if there is a reason why no normalizer is used?

Thanks for your help:)

Pytorch version

A lot of Pytorch issues occurred during my reproducing of the repo.
Could you provide the Pytorch version you used?
I think it will help a lot, thanks!

BTW, very detailed code was provided, but it would be better if more comments and more detailed 'readme' files are available.

fourier_on_images.py needs to be updated

Greetings and thank you for your awesome work!

When I wanted to run fourier_on_images.py, I encountered errors in several parts of the code:

  1. I believe that the compl_mul2d(a,b) should be updated using the codes in the fourier_2d_tuned.py as it raises an error

  2. Pytorch seems to have updated the radon transform syntax, so torch.rfft should be changed to torch.fft.rfftn

I could make the code work, but I wanted to use the official release by you. Thanks again!

RunTimeError when executing fourier_1D.py

Hi,

I tried executing your 1D example. I downloaded the corresponding data from the Google Drive folder you provided in the readme and copy-pasted the file into my env, and I get the following error:

Traceback (most recent call last):
  File "fourier_1D.py", line 196, in <module>
    x_train = torch.cat([x_train.reshape(ntrain, s, 1),
RuntimeError: shape '[1000, 1024, 1]' is invalid for input of size 8192000

This error is caused by the following code block:

# cat the locations information
grid = np.linspace(0, 2*np.pi, s).reshape(1, s, 1)
grid = torch.tensor(grid, dtype=torch.float)
x_train = torch.cat([x_train.reshape(ntrain, s, 1),
                     grid.repeat(ntrain, 1, 1)], dim=2)
x_test = torch.cat([x_test.reshape(ntest, s, 1),
                    grid.repeat(ntest, 1, 1)], dim=2)

The error seems to occur when constructing x_train.

My env configuration looks like the following:

  • python: version 3.8.5
  • pytorch: version 1.7.0
  • numpy: version 1.19.2

I changed nothing within the file, I created a corresponding data folder which contains burgers_data_R10.mat so that I didn't even have to change that line.

`torch.rfft` and `torch.ifft` arguments

@zongyi-li

It seems that Torch has modified the fft to a namespace and put all functions under it: rfft and ifft, etc.

But right now there is no arguments for one-sided, and I checked torch's official document, now it seems everything is automatically one-sided unless you want specifically the negative part: https://pytorch.org/docs/stable/fft.html. normalized arg became norm.

Can you please confirm?

Add a license

Adding a license to the repository as a LICENSE.md file protects intellectual property from unintended use from other users (commercial, not acknowledged, etc.). Possibly adding a short message at the beginning of each .py file can also be done. If you create a LICENSE.md file from the Github web interface, several templates are provided. More information are given here: https://docs.github.com/en/free-pro-team@latest/github/building-a-strong-community/adding-a-license-to-a-repository. If you have already a preferred license, I can help add it.

last layer in 3d example

Hi Zongyi,

Really nice work, maybe a stupid question, why we have x = self.bn3(x1)for the last layer in the 3d example but x = self.bn3(x1 + x2) for 1d and 2d?

Thanks
George

Can f(x) be changed during the data generation?

Hi, I am a geoscientist. It is a great paper. In the DARCY FLOW part, I wonder if you change f(x) during the data generation, and use this dataset to train, what's the influence on performance. In earth science, the f(x) is usually changed. I don't know if you have tested that. Thank you!

x_train shape error in fourier_1d.py

I tried to use the dataset called "Burgers_R10.zip" in your google drive shared files. But when I ran your "fourier_1d.py" code, it showed error information like the below:

RuntimeError                              Traceback (most recent call last)
<ipython-input-21-e4813e02a626> in <module>()
    186 grid = np.linspace(0, 2*np.pi, s).reshape(1, s, 1)
    187 grid = torch.tensor(grid, dtype=torch.float)
--> 188 x_train = torch.cat([x_train.reshape(ntrain,s,1), grid.repeat(ntrain,1,1)], dim=2)
    189 x_test = torch.cat([x_test.reshape(ntest,s,1), grid.repeat(ntest,1,1)], dim=2)
    190 

RuntimeError: shape '[1000, 1024, 1]' is invalid for input of size 8192000

It seems the shape of the x_train is [1000, 8192], but in line 188 the x_train needs to be [1000, 1024].

So, I wonder if the dataset is correct for this code? And could you describe the rows and columns of the dataset? It is really confusing now.

Assertion exception

getting this -

python3 ns_fourier_3d_rnn.py
500 0.0025 100 0.5
torch.Size([20, 256, 256, 10]) torch.Size([20, 256, 256, 10])
Traceback (most recent call last):
File "ns_fourier_3d_rnn.py", line 197, in
assert (S == train_u.shape[-2])
AssertionError

Explanation about the .mat data files

Hi, I was looking at the data files you have uploaded to google drive. Could you please provide a brief explanation of what the data files hold? For example, burgers_data_R10.mat has 5 different arrays of (2048, 8192) size. I understand 'u' to be the solution, but what are 'a', 'a_smooth', 'a_smooth_x' and 'a_x'?

NSE hyperparmeters used in paper

Hello

In order to reproduce results, I'd like to know what hyper-parameters you used for producing the various flows using NSE.

Specifically:

For the datasets with viscosities 1e-3, 1e-4, and 1e-5

for each
fourier_2d_time.py
fourier_3d.py

From reading around I only identified modes=12, width=32 for fourier_2d_time.py, and modes=8, width=20 for fourier_3d.py for viscosity=1e-4.

I think these are the most crucial ones (I will just run enough epochs etc. for the rest). I would like to train on 10 and predict 10 next frames (what is constraining the max fourier modes?)

Thanks!

Getting FNO to work on images

Hi, I thought this architecture offered a unique way to approach ML tasks, and I'd like to experiment with it on images. However, it seems like the example script you supplied for using the FNO in a Resnet-like architecture doesn't work.

I made the following changes to your SpectralConv2D code to get it to run:

#Complex multiplication

def compl_mul2d(a, b):
    # OLD CODE: op = partial(torch.einsum, "bctq,dctq->bdtq")
    op = partial(torch.einsum, "bctq,cdtq->bdtq")
    return torch.stack([
        op(a[..., 0], b[..., 0]) - op(a[..., 1], b[..., 1]),
        op(a[..., 1], b[..., 0]) + op(a[..., 0], b[..., 1])
    ], dim=-1)


class SpectralConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, mode):
        super(SpectralConv2d, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.modes1 = mode #Number of Fourier modes to multiply, at most floor(N/2) + 1
        self.modes2 = mode

        self.scale = (1 / (in_channels * out_channels))
        self.weights1 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, 2))
        self.weights2 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, 2))

    def forward(self, x):
        batchsize = x.shape[0]
        #Compute Fourier coeffcients up to factor of e^(- something constant)
        x_ft = torch.rfft(x, 2, normalized=True, onesided=True)

        # Multiply relevant Fourier modes
        # OLD CODE: out_ft = torch.zeros(batchsize, self.in_channels,  x.size(-2), x.size(-1)//2 + 1, 2, device=x.device)
        out_ft = torch.zeros(batchsize, self.out_channels,  x.size(-2), x.size(-1)//2 + 1, 2, device=x.device)
        out_ft[:, :, :self.modes1, :self.modes2] = \
            compl_mul2d(x_ft[:, :, :self.modes1, :self.modes2], self.weights1)
        out_ft[:, :, -self.modes1:, :self.modes2] = \
            compl_mul2d(x_ft[:, :, -self.modes1:, :self.modes2], self.weights2)

        #Return to physical space
        x = torch.irfft(out_ft, 2, normalized=True, onesided=True, signal_sizes=( x.size(-2), x.size(-1)))
        return x

Could you please verify if these are the correct changes that need to be made, and if not, can you explain why the code doesn't run right now?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.