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

change "plot_loss_history" because of safety #1571

Merged
merged 6 commits into from
Nov 25, 2023
Merged

Conversation

cozy-hn
Copy link
Contributor

@cozy-hn cozy-hn commented Nov 23, 2023

The change to the new code was made after discovering that np.sum(loss_history.loss_train, axis=1) does not work when the number of arrays changes during the learning process of PDE. This issue arises because the original function assumes a regular 2D numpy array structure, which is not the case when the array sizes vary during training. The list comprehension approach is therefore used to sum each inner array individually, allowing effective handling of this irregular data structure. This solution is particularly suitable given the minimal impact on performance due to the small overall size of the data set((we store a loss every 1000 epochs, so the number of lists is not large).

@lululxvi
Copy link
Owner

Could you point out an example when the array sizes vary during training?

@cozy-hn
Copy link
Contributor Author

cozy-hn commented Nov 23, 2023

Before recompiling with L-BFGS, I attempted to modify the loss function by setting

model.data.pde = pde_2

However, the array returned by this new pde function had a different length, resulting in the loss graph not being generated.

Therefore, the length of arrays inside loss_train changes, and the code

loss_train = np.sum(loss_history.loss_train, axis=1)

results in an error.
However, by redefining the function as I did, this error can be eliminated.


def pde(X, Y):
    du_x = dde.grad.jacobian(Y, X, i = 0, j = 0)
    du_y = dde.grad.jacobian(Y, X, i = 0, j = 1)
    dv_x = dde.grad.jacobian(Y, X, i = 1, j = 0)
    dv_y = dde.grad.jacobian(Y, X, i = 1, j = 1)
    dp_x = dde.grad.jacobian(Y, X, i = 2, j = 0)
    dp_y = dde.grad.jacobian(Y, X, i = 2, j = 1)
    du_xx = dde.grad.hessian(Y, X, i = 0, j = 0, component = 0)
    du_yy = dde.grad.hessian(Y, X, i = 1, j = 1, component = 0)
    dv_xx = dde.grad.hessian(Y, X, i = 0, j = 0, component = 1)
    dv_yy = dde.grad.hessian(Y, X, i = 1, j = 1, component = 1)

    pde_u = Y[:,0:1]*du_x + Y[:,1:2]*du_y + 1/rho * dp_x - (mu/rho)*(du_xx + du_yy)
    pde_v = Y[:,0:1]*dv_x + Y[:,1:2]*dv_y + 1/rho * dp_y - (mu/rho)*(dv_xx + dv_yy)
    pde_cont = du_x + dv_y

    return [pde_u, pde_v, pde_cont]
def pde_2(X, Y):
    du_x = dde.grad.jacobian(Y, X, i = 0, j = 0)
    du_y = dde.grad.jacobian(Y, X, i = 0, j = 1)
    dv_x = dde.grad.jacobian(Y, X, i = 1, j = 0)
    dv_y = dde.grad.jacobian(Y, X, i = 1, j = 1)
    dp_x = dde.grad.jacobian(Y, X, i = 2, j = 0)
    dp_y = dde.grad.jacobian(Y, X, i = 2, j = 1)
    du_xx = dde.grad.hessian(Y, X, i = 0, j = 0, component = 0)
    du_yy = dde.grad.hessian(Y, X, i = 1, j = 1, component = 0)
    du_yx = dde.grad.hessian(Y, X, i = 1, j = 0, component = 0)
    du_xy = dde.grad.hessian(Y, X, i = 0, j = 1, component = 0)
    dv_xx = dde.grad.hessian(Y, X, i = 0, j = 0, component = 1)
    dv_yy = dde.grad.hessian(Y, X, i = 1, j = 1, component = 1)
    dv_xy = dde.grad.hessian(Y, X, i = 0, j = 1, component = 1)
    dv_yx = dde.grad.hessian(Y, X, i = 1, j = 0, component = 1)
    dp_xx = dde.grad.hessian(Y, X, i = 0, j = 0, component = 2)
    dp_xy = dde.grad.hessian(Y, X, i = 0, j = 1, component = 2)
    dp_yx = dde.grad.hessian(Y, X, i = 1, j = 0, component = 2)
    dp_yy = dde.grad.hessian(Y, X, i = 1, j = 1, component = 2)
    du_xxx = dde.grad.jacobian(du_xx, X, j=0)
    du_xyy = dde.grad.jacobian(du_yy, X, j=0)
    du_yxx = dde.grad.jacobian(du_xx, X, j=1)
    du_yyy = dde.grad.jacobian(du_yy, X, j=1)
    dv_xxx = dde.grad.jacobian(dv_xx, X, j=0)
    dv_xyy = dde.grad.jacobian(dv_yy, X, j=0)
    dv_yxx = dde.grad.jacobian(dv_xx, X, j=1)
    dv_yyy = dde.grad.jacobian(dv_yy, X, j=1)

    V = mu/rho
    pde_u = Y[:,0:1]*du_x + Y[:,1:2]*du_y + 1/rho * dp_x - V*(du_xx + du_yy)
    pde_u_x = - V*(du_xxx + du_xyy) + 1/rho * dp_xx + Y[:,0:1]*du_xx + Y[:,1:2]*du_xy + du_x*du_x + dv_x*du_y
    pde_u_y = - V*(du_yxx + du_yyy) +1/rho * dp_yx +Y[:,0:1]*du_yx + Y[:,1:2]*du_yy + du_y*du_x + dv_y*du_y
    pde_v = Y[:,0:1]*dv_x + Y[:,1:2]*dv_y + 1/rho * dp_y - V*(dv_xx + dv_yy)
    pde_v_x = - V*(dv_xxx + dv_xyy) + 1/rho * dp_xy + Y[:,0:1]*dv_xx + Y[:,1:2]*dv_xy + du_x*dv_x + dv_x*dv_y
    pde_v_y = - V*(dv_yxx + dv_yyy) + 1/rho * dp_yy + Y[:,0:1]*dv_yx + Y[:,1:2]*dv_yy + du_y*dv_x + dv_y*dv_y
    pde_cont = du_x + dv_y
    pde_cont_mod = du_xx + dv_xy
    pde_cont_mod_2 = du_yx + dv_yy

    return [pde_u, pde_u_x, pde_u_y, pde_v, pde_v_x, pde_v_y, pde_cont, pde_cont_mod, pde_cont_mod_2]
 'loss_train': [array([0.04726982, 0.00698001, 0.07082939, 0.00120296, 0.01194395,
         0.8888629 , 0.00602983, 0.04843263, 0.00616049], dtype=float32),
  array([1.1833496e-03, 9.0652617e-04, 8.7317917e-03, 3.3910487e-02,
         1.5672268e-02, 3.7684571e-02, 2.2763922e-03, 5.6915195e-04,
         2.9539286e-05], dtype=float32),
  array([1.57421008e-02, 2.27812561e-03, 6.07683323e-03, 2.68560331e-02,
         1.22102415e-02, 3.02209910e-02, 1.00009795e-03, 1.19509296e-02,
         3.28731003e-05], dtype=float32),
  array([3.4056688e-03, 9.3010388e-04, 4.1855085e-03, 2.3339095e-02,
         1.1147979e-02, 2.2902276e-02, 2.5180816e-03, 1.5161597e-03,
         2.0256150e-05], dtype=float32),
  array([2.7247712e-03, 8.7075820e-04, 2.8752405e-03, 2.1314245e-02,
         9.8875137e-03, 2.0474203e-02, 3.9394982e-03, 1.2650636e-03,
         1.0909113e-05], dtype=float32),
  array([2.0315973e-03, 9.2529098e-04, 2.6743268e-03, 2.0484118e-02,
         9.0067303e-03, 1.8166516e-02, 5.0312411e-03, 8.0506445e-04,
         1.3629055e-05], dtype=float32),
  array([4.3111277e-04, 2.8702384e-04, 2.5311073e-03, 1.9625025e-02,
         8.2691777e-03, 1.6922861e-02, 5.7788324e-03, 8.1356229e-05,
         1.3621720e-05], dtype=float32),
  array([3.2704547e-03, 1.4207005e-03, 2.5081690e-03, 1.9377101e-02,
         7.5014569e-03, 1.6471364e-02, 5.9313816e-03, 1.7559605e-04,
         6.3411558e-06], dtype=float32),
  array([5.95013960e-04, 6.44668413e-04, 2.30458612e-03, 1.93411522e-02,
         7.08718505e-03, 1.52231725e-02, 6.29690289e-03, 2.60401503e-05,
         1.29099662e-05], dtype=float32),
  array([1.1401109e-03, 2.3732863e-04, 2.2850388e-03, 1.9137543e-02,
         6.5878541e-03, 1.5103507e-02, 6.2107644e-03, 1.8903258e-05,
         8.8633842e-06], dtype=float32),
  array([9.1892685e-04, 2.0221423e-04, 2.1571189e-03, 1.8278580e-02,
         5.7868697e-03, 1.6089967e-02, 6.0939365e-03, 1.0464229e-03,
         8.7694398e-06], dtype=float32),
  array([7.5434768e-03, 2.8409758e-03, 1.9297212e-03, 1.8537272e-02,
         5.9275054e-03, 1.4672440e-02, 6.4749718e-03, 1.5716421e-04,
         4.4827510e-05], dtype=float32),
  array([1.2364826e-03, 3.3453802e-04, 1.8457462e-03, 1.8233228e-02,
         5.7173036e-03, 1.4245455e-02, 6.6001727e-03, 2.3345861e-05,
         5.6063473e-06], dtype=float32),
  array([4.5824540e-03, 1.5852941e-03, 1.7526904e-03, 1.8139098e-02,
         5.3726099e-03, 1.4449499e-02, 6.4826589e-03, 6.9159415e-04,
         2.1971879e-05], dtype=float32),
  array([2.3909973e-03, 3.5924598e-04, 1.9187705e-03, 1.7984148e-02,
         4.9744085e-03, 1.4350639e-02, 6.4007649e-03, 4.5066490e-04,
         3.8523744e-06], dtype=float32),
  array([3.1931833e-03, 7.2930049e-04, 1.7743702e-03, 1.7886499e-02,
         4.9390495e-03, 1.3945018e-02, 6.5406351e-03, 5.9486018e-04,
         6.9224620e-06], dtype=float32),
  array([3.1931833e-03, 7.1719432e-01, 4.0206361e-01, 7.2930049e-04,
         3.8920367e-01, 6.6453254e-01, 1.7743702e-03, 1.8954869e-01,
         8.2233675e-02, 1.7886499e-02, 4.9390495e-03, 1.3945018e-02,
         6.5406351e-03, 5.9486018e-04, 6.9224620e-06], dtype=float32),
  array([1.13852332e-06, 1.35426337e-04, 1.87983576e-04, 1.22219910e-06,
         1.52339999e-04, 1.98931390e-04, 1.98011432e-04, 2.14829270e-04,
         3.36410187e-04, 1.90417953e-02, 5.06643299e-03, 1.68161634e-02,
         7.68097630e-03, 4.81373136e-05, 1.13805345e-05], dtype=float32),
  array([4.2906143e-07, 4.9999479e-05, 8.1155165e-05, 5.8330198e-07,
         4.8006979e-05, 1.1319345e-04, 3.0157156e-05, 9.0941227e-05,
         1.4252686e-04, 1.8235333e-02, 3.9619915e-03, 1.3767680e-02,
         9.9275112e-03, 3.2984564e-05, 4.4490618e-05], dtype=float32),
  array([7.8718836e-07, 1.0334032e-04, 1.2143861e-04, 7.4593589e-07,
         8.3010862e-05, 1.2954908e-04, 3.6721605e-05, 1.1775842e-04,
         2.1212305e-04, 1.8672131e-02, 2.8699592e-03, 1.2787160e-02,
         8.9156684e-03, 6.5215372e-06, 1.1255889e-05], dtype=float32),
  array([5.73017871e-07, 1.16575655e-04, 1.47192390e-04, 9.64910555e-07,
         1.05453953e-04, 2.06074154e-04, 7.68015962e-05, 1.43114594e-04,
         2.21397931e-04, 1.75479650e-02, 2.60949205e-03, 1.13498727e-02,
         8.93757213e-03, 8.57612940e-06, 3.92263973e-06], dtype=float32),
  array([1.2753900e-06, 1.4323239e-04, 2.5491972e-04, 1.1276155e-06,
         2.3292690e-04, 2.4314229e-04, 5.3835738e-05, 1.0968471e-04,
         1.6481047e-04, 1.7125493e-02, 2.2821249e-03, 1.0338106e-02,
         7.8802127e-03, 1.7418850e-05, 5.6888739e-06], dtype=float32),
  array([1.4725665e-06, 1.5726943e-04, 2.6287718e-04, 1.8170614e-06,
         2.4307966e-04, 4.0841781e-04, 5.4477470e-05, 1.3326493e-04,
         2.6377637e-04, 1.6553726e-02, 2.1522855e-03, 9.5005305e-03,
         7.1082413e-03, 3.4351258e-05, 4.6209852e-06], dtype=float32),
  array([1.1586641e-06, 1.3518748e-04, 1.4777576e-04, 1.6049793e-06,
         2.0710363e-04, 3.0400488e-04, 4.7917936e-05, 2.1295935e-04,
         2.7041527e-04, 1.6434003e-02, 2.0197169e-03, 8.7055909e-03,
         6.1926558e-03, 2.1108173e-05, 1.4231287e-05], dtype=float32),
  array([1.2639226e-06, 2.2334511e-04, 1.9029653e-04, 2.2553229e-06,
         3.0123044e-04, 3.2440983e-04, 5.6718756e-05, 2.4283606e-04,
         3.2081260e-04, 1.5271850e-02, 1.9571672e-03, 8.2972469e-03,
         5.5769519e-03, 1.5546255e-05, 9.9993476e-06], dtype=float32),
  array([1.8596508e-06, 2.2684011e-04, 3.0733025e-04, 1.8496694e-06,
         3.4304793e-04, 3.0548641e-04, 5.6206307e-05, 1.2338228e-04,
         3.5852991e-04, 1.4567948e-02, 1.8977183e-03, 7.6846182e-03,
         5.1395963e-03, 1.9535773e-05, 6.9746648e-06], dtype=float32),
  array([9.6037900e-07, 2.7810683e-04, 2.8552744e-04, 1.4587042e-06,
         3.4882245e-04, 3.3100264e-04, 6.0973449e-05, 1.5751609e-04,
         3.5377723e-04, 1.3952691e-02, 1.7399048e-03, 7.3088096e-03,
         4.6354090e-03, 1.7886307e-05, 6.0348052e-06], dtype=float32)],

@cozy-hn
Copy link
Contributor Author

cozy-hn commented Nov 23, 2023

My modification of the loss function was merely out of curiosity. However, considering that changing the code did not seem to significantly impact performance, and there might be instances in the future where I experiment with altering the loss function, I suggested a pull request.

deepxde/utils/external.py Outdated Show resolved Hide resolved
deepxde/utils/external.py Outdated Show resolved Hide resolved
deepxde/utils/external.py Outdated Show resolved Hide resolved
@lululxvi lululxvi merged commit c55e5d3 into lululxvi:master Nov 25, 2023
13 checks passed
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