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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vl-dud
Copy link
Contributor

@vl-dud vl-dud commented Nov 14, 2023

When you define deepxde.Model multiple times in the same code it leads to errors. This was briefly mentioned in #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.

@@ -9,6 +9,14 @@ class NN:
"""Base class for all neural network modules."""

def __init__(self):
try:
seed = tf.random.get_seed(None)[0]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why setting the seed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To handle the situation when a user has set a seed before (e.g. using deepxde.config.set_random_seed)

@lululxvi
Copy link
Owner

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

@vl-dud
Copy link
Contributor Author

vl-dud commented Nov 18, 2023

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.

@lululxvi
Copy link
Owner

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

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

Successfully merging this pull request may close these issues.

2 participants