lululxvi/deepxde

Question on Learning Function from Formula

Opened this issue · 8 comments

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.

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

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

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!

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.

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

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

image

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!

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

@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.