Skip to content

Commit

Permalink
rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry-Jzy committed Jan 3, 2025
1 parent ab5547f commit 39f5af4
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 23 deletions.
22 changes: 3 additions & 19 deletions deepxde/data/pde_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .data import Data
from .sampler import BatchSampler
from .. import backend as bkd
from ..backend import backend_name
from .. import config
from ..utils import run_if_all_none

Expand Down Expand Up @@ -264,33 +263,18 @@ def _losses(self, outputs, loss_fn, inputs, model, num_func, aux=None):
# Use stack instead of as_tensor to keep the gradients.
losses = [bkd.reduce_mean(bkd.stack(loss, 0)) for loss in losses]
elif config.autodiff == "forward": # forward mode AD
batchsize1, batchsize2 = bkd.shape(outputs)[:2]
shape_3d = (batchsize1, batchsize2, model.net.num_outputs)

# Uniformly reshape the output into the shape (N1, N2, num_outputs),
def forward_call(trunk_input):
output = aux[0]((inputs[0], trunk_input))
return bkd.reshape(output, shape_3d)
return aux[0]((inputs[0], trunk_input))

f = []
if self.pde.pde is not None:
if backend_name in ["tensorflow.compat.v1"]:
outputs_pde = bkd.reshape(outputs, shape_3d)
elif backend_name in ["tensorflow", "pytorch"]:
outputs_pde = (bkd.reshape(outputs, shape_3d), forward_call)
# Each f has the shape (N1, N2)
f = self.pde.pde(
inputs[1],
outputs_pde,
bkd.reshape(
model.net.auxiliary_vars,
shape_3d,
),
inputs[1], (outputs, forward_call), model.net.auxiliary_vars
)
if not isinstance(f, (list, tuple)):
f = [f]
f = [bkd.reshape(fi, (batchsize1, batchsize2)) for fi in f]

# Each error has the shape (N1, ~N2)
error_f = [fi[:, bcs_start[-1] :] for fi in f]
for error in error_f:
Expand Down Expand Up @@ -365,4 +349,4 @@ def test(self):
)
self.test_x = (func_vals, self.pde.test_x)
self.test_aux_vars = vx
return self.test_x, self.test_y, self.test_aux_vars
return self.test_x, self.test_y, self.test_aux_vars
2 changes: 1 addition & 1 deletion deepxde/gradients/gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def jacobian(ys, xs, i=None, j=None):
computation.
Args:
ys: Output Tensor of shape (batch_size, dim_y) or (batch_size1, batch_size2, dim_y).
ys: Output Tensor of shape (batch_size, dim_y).
xs: Input Tensor of shape (batch_size, dim_x).
i (int or None): `i`th row. If `i` is ``None``, returns the `j`th column
J[:, `j`].
Expand Down
4 changes: 2 additions & 2 deletions deepxde/gradients/gradients_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ def grad_fn(x):
# Compute J[i, j]
if (i, j) not in self.J:
if backend_name == "tensorflow.compat.v1":
self.J[i, j] = self.J[j][..., i : i + 1]
self.J[i, j] = self.J[j][:, i : i + 1]
elif backend_name in ["tensorflow", "pytorch", "jax"]:
# In backend tensorflow/pytorch/jax, a tuple of a tensor/tensor/array
# and a callable is returned, so that it is consistent with the argument,
# which is also a tuple. This is useful for further computation, e.g.,
# Hessian.
self.J[i, j] = (
self.J[j][0][..., i : i + 1],
self.J[j][0][:, i : i + 1],
lambda x: self.J[j][1](x)[i : i + 1],
)
return self.J[i, j]
Expand Down
2 changes: 1 addition & 1 deletion deepxde/gradients/jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, ys, xs):
elif config.autodiff == "forward":
# For forward-mode AD, a tuple of a tensor and a callable is passed,
# similar to backend jax.
self.dim_y = ys[0].shape[-1]
self.dim_y = ys[0].shape[1]
elif backend_name == "jax":
# For backend jax, a tuple of a jax array and a callable is passed as one of
# the arguments, since jax does not support computational graph explicitly.
Expand Down

0 comments on commit 39f5af4

Please sign in to comment.