I’m working on a Physics Informed Neural Network that has two inputs and N outputs. The loss function contains high-order derivatives of the outputs with respect to the inputs x and y. Is it possible to compute the derivatives in parallel without a for loop?
def gradient(y, x, grad_outputs=None):
if grad_outputs is None:
grad_outputs = torch.ones_like(y)
grad = torch.autograd.grad(y, [x], grad_outputs=grad_outputs, create_graph=True)[0]
return grad
def compute_derivatives(x, y, u):
dudx = gradient(u, x)
dudy = gradient(u, y)
dudxx = gradient(dudx, x)
dudyy = gradient(dudy, y)
dudxxx = gradient(dudxx, x)
dudxxy = gradient(dudxx, y)
dudyyy = gradient(dudy, y)
dudxxxx = gradient(dudxxx, x)
dudxxyy = gradient(dudxxy, y)
dudyyyy = gradient(dudyyy, y)
return dudxx, dudyy, dudxxxx, dudyyyy, dudxxyy
The code above works fine for a single output. For N outputs the shape of u is [batch_size, N] and I need to compute the derivatives for each column of u in parallel so that each derivative (ex. dudx, dudy…) shape matches the shape of u. Which is the most efficient way to do this? Thanks in advance.