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

Question on Learning Function from Formula #1816

Open
LHLevin opened this issue Aug 6, 2024 · 8 comments
Open

Question on Learning Function from Formula #1816

LHLevin opened this issue Aug 6, 2024 · 8 comments

Comments

@LHLevin
Copy link

LHLevin commented Aug 6, 2024

Hi Dr. Lu,

I'm trying to understand how the "Learning a function from a formula" works. From the docs, an example with an unknown is given. If I have a formula of more than one unknown, for instance: f(x,y) = sin(x) + y^2, how do I introduce another unknown to be learned?

Thank you in advance for any reply.

@praksharma
Copy link
Contributor

For two inputs you can use a 2D geometry such as a rectangle.

geom = dde.geometry.Rectangle([0, 0], [1, 1])

@LHLevin
Copy link
Author

LHLevin commented Aug 7, 2024

Yes, I tried using 2D Geometry and changed the net to 2 input nodes. It now looks like:
def func(x,y):
return np.sin(x) + y^2)
# return x * np.sin(5 * x)

geom = dde.geometry.Rectangle([-1, -1], [1, 1])
num_train = 16
num_test = 100
data = dde.data.Function(geom, func, num_train, num_test)

activation = "tanh"
initializer = "Glorot uniform"
net = dde.nn.FNN([2] + [20] * 3 + [1], activation, initializer)

model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(iterations=10000)

dde.saveplot(losshistory, train_state, issave=True, isplot=True)

But it gives an error: TypeError: func() missing 1 required positional argument: 'y'. (in function.py)

If I changed the function to an array: return np.sin(x[:,0]) + x[:,1]^2), it gives another error:
ValueError: Cannot feed value of shape (16,) for Tensor Placeholder_3:0, which has shape (None, 1) (in model.py)

Any idea on how to solve this? Or how can I define the function with >1 variables? Thank you very much!

@praksharma
Copy link
Contributor

Try this.

def func(x):
    return np.sin(x[:,0]) + x[:,1]**2

geom = dde.geometry.Rectangle([-1, -1], [1, 1])
num_train = 16
num_test = 100
data = dde.data.Function(geom, func, num_train, num_test)

activation = "tanh"
initializer = "Glorot uniform"
net = dde.nn.FNN([2] + [20] * 3 + [1], activation, initializer)

model = dde.Model(data, net)
model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(iterations=10000)

dde.saveplot(losshistory, train_state, issave=True, isplot=True)

You will still get an error when using dde.saveplot which might be a bug.

@LHLevin
Copy link
Author

LHLevin commented Aug 7, 2024

Yeah, I tried that as well, it gives an Exception error: ValueError: Cannot feed value of shape (16,) for Tensor Placeholder_3:0, which has shape (None, 1)

The complete error messages look like below:
Exception has occurred: ValueError
Cannot feed value of shape (16,) for Tensor Placeholder_3:0, which has shape (None, 1)
File "\deepxde\model.py", line 546, in _outputs_losses
return self.sess.run(outputs_losses, feed_dict=feed_dict)
File "\deepxde\model.py", line 832, in _test
) = self._outputs_losses(
File "\deepxde\model.py", line 643, in train
self._test()
File "\deepxde\utils\internal.py", line 22, in wrapper
result = f(*args, **kwargs)
File "\func.py", line 32, in
losshistory, train_state = model.train(iterations=10000)
ValueError: Cannot feed value of shape (16,) for Tensor Placeholder_3:0, which has shape (None, 1)

The error happens in model.train, so I think this is not due to dde.saveplot

@praksharma
Copy link
Contributor

It is working on my PC. Although there is a bug with saveplot function. I am using pytorch backend.

image

@LHLevin
Copy link
Author

LHLevin commented Aug 7, 2024

I see. Then it's probably a backend problem. I'm using tensorflow backend. I'll try checking for compatibility again. Thank you very much!

@lululxvi
Copy link
Owner

@praksharma @LHLevin Have you found what the bug is?

@LHLevin
Copy link
Author

LHLevin commented Aug 26, 2024

@lululxvi I did not continue to find the reason why it doesn't work with tensorflow. I changed my backend to pytorch as @praksharma suggested and it works.

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

3 participants