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

fix external variable initialization #1775

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions deepxde/data/pde.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from .. import backend as bkd
from .. import config
from ..backend import backend_name
from ..utils import get_num_args, run_if_all_none, mpi_scatter_from_rank0
from ..utils import (
get_num_args,
has_default_values,
run_if_all_none,
mpi_scatter_from_rank0,
)


class PDE(Data):
Expand Down Expand Up @@ -150,9 +155,18 @@ def losses(self, targets, outputs, loss_fn, inputs, model, aux=None):
elif get_num_args(self.pde) == 3:
if self.auxiliary_var_fn is not None:
f = self.pde(inputs, outputs_pde, model.net.auxiliary_vars)
elif backend_name == "jax" and len(aux) == 2:
elif backend_name == "jax":
# JAX inverse problem requires unknowns as the input.
f = self.pde(inputs, outputs_pde, unknowns=aux[1])
if len(aux) == 2:
# External trainable variables in aux[1] are used for unknowns
f = self.pde(inputs, outputs_pde, unknowns=aux[1])
elif len(aux) == 1 and has_default_values(self.pde)[-1]:
# No external trainable variables, default values are used for unknowns
f = self.pde(inputs, outputs_pde)
lululxvi marked this conversation as resolved.
Show resolved Hide resolved
else:
raise ValueError(
"Default unknowns are required if no trainable variables are provided."
)
else:
raise ValueError("Auxiliary variable function not defined.")
if not isinstance(f, (list, tuple)):
Expand Down
8 changes: 4 additions & 4 deletions deepxde/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@ def _compile_jax(self, lr, loss_fn, decay):
if self.params is None:
key = jax.random.PRNGKey(config.jax_random_seed)
self.net.params = self.net.init(key, self.data.test()[0])
external_trainable_variables_arr = [
var.value for var in self.external_trainable_variables
]
self.params = [self.net.params, external_trainable_variables_arr]
external_trainable_variables_val = [
var.value for var in self.external_trainable_variables
]
self.params = [self.net.params, external_trainable_variables_val]
Copy link
Owner

Choose a reason for hiding this comment

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

What if self.external_trainable_variables is []?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's working fine, var.value is never evaluated and the output is []

# TODO: learning rate decay
self.opt = optimizers.get(self.opt_name, learning_rate=lr)
self.opt_state = self.opt.init(self.params)
Expand Down
12 changes: 12 additions & 0 deletions deepxde/utils/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ def get_num_args(func):
params = inspect.signature(func).parameters
return len(params) - ("self" in params)

def has_default_values(func):
bonneted marked this conversation as resolved.
Show resolved Hide resolved
"""
Check if the given function has default values for its parameters.

Args:
func (function): The function to inspect.

Returns:
list: A list of boolean values indicating whether each parameter has a default value.
"""
params = inspect.signature(func).parameters.values()
return [param.default is not inspect.Parameter.empty for param in params]

def mpi_scatter_from_rank0(array, drop_last=True):
"""Scatter the given array into continuous subarrays of equal size from rank 0 to all ranks.
Expand Down
Loading