Skip to content
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

f(x, y) function approximation in 3D space #1

Open
tesnikio opened this issue Nov 19, 2020 · 6 comments
Open

f(x, y) function approximation in 3D space #1

tesnikio opened this issue Nov 19, 2020 · 6 comments

Comments

@tesnikio
Copy link

tesnikio commented Nov 19, 2020

Hey @rlorigro , thanks for your work, I didn't find a way to contact you, so I opened an issue here, sorry :)

How to extend your code so it could approximate 2 variable functions in 3D space?

For example: f(x, y) = x^2 + x + ycos(x) + sin(y)

Screenshot 2020-11-19 at 12 12 59

@rlorigro
Copy link
Owner

rlorigro commented Nov 19, 2020

Hi Nikita, no worries, this is as good a place as any. You would have to do 2 things to make this work:

  1. Edit the data generation code to create your own training/testing set for your function
  2. Modify the dimensions of the network layers to match the dimensions of your inputs and outputs

For 1, it looks like you are already generating data based on the plot you showed me, so you would just have to sample randomly from your x,y space and generate z values (ideally with some noise added). You would probably want to format your x,y pairs as a single matrix where one of its dimensions is size=2 so it can easily be handed to the NN forward method. You'll have to verify which orientation pytorch wants the matrix to be.

For 2, all fully connected networks like this one are essentially compound functions, so there is a pretty straightforward way to adapt it to approximate any other continuous function.

For the sin function, the number of inputs and outputs is 1:

D_in, H1, D_out = [1, 8, 1] # These numbers correspond to each layer: [input, hidden_1, output]

Where D_in and D_out are 1. Your function has 2 inputs and 1 output, so you would just have to change D_in to 2, and then when you call the forward method be sure to pass in the appropriate shaped x vector.

@tesnikio
Copy link
Author

Hi Nikita, no worries, this is as good a place as any. You would have to do 2 things to make this work:

  1. Edit the data generation code to create your own training/testing set for your function
  2. Modify the dimensions of the network layers to match the dimensions of your inputs and outputs

For 1, it looks like you are already generating data based on the plot you showed me, so you would just have to sample randomly from your x,y space and generate z values (ideally with some noise added). You would probably want to format your x,y pairs as a single matrix where one of its dimensions is size=2 so it can easily be handed to the NN forward method. You'll have to verify which orientation pytorch wants the matrix to be.

For 2, all fully connected networks like this one are essentially compound functions, so there is a pretty straightforward way to adapt it to approximate any other continuous function.

For the sin function, the number of inputs and outputs is 1:

D_in, H1, D_out = [1, 8, 1] # These numbers correspond to each layer: [input, hidden_1, output]

Where D_in and D_out are 1. Your function has 2 inputs and 1 output, so you would just have to change D_in to 2, and then when you call the forward method be sure to pass in the appropriate shaped x vector.

Thank you so much, Ryan! It worked out :)
The loss isn't that good as for the initial case, but I'll play around with params to get it better

@rlorigro
Copy link
Owner

Cool, I'm glad to hear it is running. I would be curious to see the output of the trained model if you have a plot. Also if you want to add your notebook to the repo, feel free to make a PR.

@tesnikio
Copy link
Author

Ok, I'll upload it when I'm finished.

@tesnikio
Copy link
Author

As for the plot, here you go, the first one is a generated function, the second one is prediction.

Screenshot 2020-11-20 at 13 43 03

Screenshot 2020-11-20 at 13 44 10

@rlorigro
Copy link
Owner

Nice, it looks like it is performing well. I think the 3D example is a much more impressive demonstration than the sin function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants