Gradients in DeepINN

Gradients in DeepINN#

# This is only valid when the package is not installed
import sys
sys.path.append('../../') # two folders up
import DeepINN as dp
import torch
Using default backend: PyTorch
Using Pytorch:  2.0.1+cu117
def laplace(X, y):
    """
    1D Laplace equation.
    u__x = 0 
    """
    dy_x = dp.constraint.Jacobian(X, y)(i=0, j=0)
    print(dy_x.requires_grad)  # Check if requires_grad is True
    print(dy_x.grad_fn)  # Check the gradient function associated with dy_x
    dy_xx = dp.constraint.Jacobian(X, dy_x+0)(i=0, j=0)

    return dy_xx
boundary_point_labels =  [torch.tensor([[0.]],requires_grad=True), torch.tensor([[1.]])]
boundary_point_sample = [torch.tensor([[0.]]), torch.tensor([[1.]])]
y = boundary_point_labels[0]**2
y.requires_grad
True
laplace(boundary_point_labels[0], y)
True
<IndexBackward0 object at 0x7581944093d0>
tensor([[2.]], grad_fn=<IndexBackward0>)
len(boundary_point_labels[0].size()), boundary_point_labels[0]
(2, tensor([[0.]], requires_grad=True))