deepxde icon indicating copy to clipboard operation
deepxde copied to clipboard

Tensorflow 1.x backend: define a model multiple times in the same code

Open vl-dud opened this issue 1 year ago • 3 comments

When you define deepxde.Model multiple times in the same code it leads to errors. This was briefly mentioned in https://github.com/lululxvi/deepxde/issues/54. So this code:

import os

os.environ["DDEBACKEND"] = "tensorflow.compat.v1"
import deepxde as dde
import numpy as np


def func(x):
    return x * np.sin(5 * x)


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

net = dde.nn.FNN([1] + [20] * 3 + [1], "tanh", "Glorot uniform")
model = dde.Model(data, net)
model.compile("adam", lr=0.001)
model.train(iterations=1000)
model.save("dump/model")

net_2 = dde.nn.FNN([1] + [20] * 3 + [1], "tanh", "Glorot uniform")
model_2 = dde.Model(data, net_2)
model_2.compile("adam", lr=0.001)
model_2.restore("dump/model-1000.ckpt", verbose=1)

will not be executed. I suggest resetting the global default graph to fix this.

vl-dud avatar Nov 14 '23 15:11 vl-dud

Actually DeepXDE is not indented to work for defining Model for several times. I am not sure if doing this modification would cause some issues at somewhere else.

There is an utility function for this purpose: https://deepxde.readthedocs.io/en/latest/modules/deepxde.utils.html#deepxde.utils.external.apply

lululxvi avatar Nov 17 '23 19:11 lululxvi

If you think it might cause any issues, then let's close this PR. But I spent a lot of time trying to figure out what the problem was with my code and this feature is quite unobvious. It might make sense to pay more attention to this in the documentation and examples to save time for other users.

vl-dud avatar Nov 18 '23 12:11 vl-dud

You are right. Let us think more about this. A note in the documentation could be helpful.

lululxvi avatar Nov 18 '23 15:11 lululxvi