lululxvi / deepxde

A library for scientific machine learning and physics-informed learning
https://deepxde.readthedocs.io
GNU Lesser General Public License v2.1
2.64k stars 739 forks source link

PINN inverse to predict a constant in a kernel for an IDE #1083

Open m-ayyad opened 1 year ago

m-ayyad commented 1 year ago

Discussed in https://github.com/lululxvi/deepxde/discussions/1064

Originally posted by **m-ayyad** December 3, 2022 I am trying to do an inverse PINN to predict a parameter in the integral term in an IDE. k = dde.Variable(1.0) c = dde.Variable(1.0) A = dde.Variable(1.0) def IDE_system(t, theta, int_mat): rhs = To_true / I_true * tf.math.sin(omega_true*t) dtheta_t = dde.grad.jacobian(theta, t) dtheta_tt = dde.grad.hessian(theta, t) lhs1 = c * tf.matmul(int_mat, dtheta_t) lhs2 = dtheta_tt + k * theta lhs = lhs1 + lhs2[: tf.size(lhs1)] return lhs - rhs[: tf.size(lhs1)] def kernel(t, tao): return np.exp(-A * (t - tao)) It shows the following error > NotImplementedError: Cannot convert a symbolic tf.Tensor (Exp:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported. I tried to change the return of the kernel to `return tf.math.exp(-A * (t - tao))` but the same error is issued I believe the problem is in the gauss-quadrature code in the IDE soyrce code. I tried to change the it to use tensorflow instead of numpy functions but I didn't find a gauss-quadrature function in tensorflow. Did anyone face a similar problem? Thanks, Mahmoud
tsarikahin commented 1 year ago

what about tf.exp? And where is this kernel used? You can provide the whole code

m-ayyad commented 1 year ago

tf.exp does not work. I use it here

geom = dde.geometry.TimeDomain(0, maxtime)

observe_t, ob_y = time_orig[:, None], theta_orig[:, None]
observe_x = dde.icbc.PointSetBC(observe_t, ob_y)

quad_deg = 5
data = dde.data.IDE(
    geom,
    IDE_system,
    observe_x,
    quad_deg=5,
    kernel=kernel,
    num_domain=len(theta_orig),
    num_boundary=0,
    train_distribution="uniform",
    anchors=observe_t
)

net = dde.nn.FNN([1] + [15] * 3 + [1], "tanh", "Glorot uniform")
model = dde.Model(data, net)
model.compile("adam", lr=0.001, external_trainable_variables=[k, c, A]) 

fnamevar = "variables.dat"
variable = dde.callbacks.VariableValue([k, c, A], period=100, filename=fnamevar)

model.train(iterations=2, callbacks=[variable])