deepxde.gradients
deepxde.gradients.gradients module
Compute gradients using reverse-mode or forward-mode autodiff.
- deepxde.gradients.gradients.hessian(ys, xs, component=0, i=0, j=0)[source]
Compute Hessian matrix H as H[i, j] = d^2y / dx_i dx_j, where i,j = 0, …, dim_x - 1.
Use this function to compute second-order derivatives instead of
tf.gradients()
ortorch.autograd.grad()
, becauseIt is lazy evaluation, i.e., it only computes H[i, j] when needed.
It will remember the gradients that have already been computed to avoid duplicate computation.
- Parameters:
ys – Output Tensor of shape (batch_size, dim_y) or (batch_size_out, batch_size, dim_y). Here, the batch_size is the same one for xs, and batch_size_out is the batch size for an additional/outer dimension.
xs – Input Tensor of shape (batch_size, dim_x).
component – ys[:, component] is used as y to compute the Hessian.
i (int) – `i`th row.
j (int) – `j`th column.
- Returns:
H[i, j]. When ys has shape (batch_size, dim_y), the output shape is (batch_size, 1). When ys has shape (batch_size_out, batch_size, dim_y), the output shape is (batch_size_out, batch_size, 1).
- deepxde.gradients.gradients.jacobian(ys, xs, i=None, j=None)[source]
Compute Jacobian matrix J as J[i, j] = dy_i / dx_j, where i = 0, …, dim_y - 1 and j = 0, …, dim_x - 1.
Use this function to compute first-order derivatives instead of
tf.gradients()
ortorch.autograd.grad()
, becauseIt is lazy evaluation, i.e., it only computes J[i, j] when needed.
It will remember the gradients that have already been computed to avoid duplicate computation.
- Parameters:
ys – Output Tensor of shape (batch_size, dim_y) or (batch_size_out, batch_size, dim_y). Here, the batch_size is the same one for xs, and batch_size_out is the batch size for an additional/outer dimension.
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].j (int or None) – j`th column. If `j is
None
, returns the i`th row J[`i, :], i.e., the gradient of y_i. i and j cannot be bothNone
, unless J has only one element, which is returned.
- Returns:
], or j`th column J[:, `j]. When ys has shape (batch_size, dim_y), the output shape is (batch_size, 1). When ys has shape (batch_size_out, batch_size, dim_y), the output shape is (batch_size_out, batch_size, 1).
- Return type:
(i, j)th entry J[i, j], i`th row J[`i,