jacobian
- paddle.autograd. jacobian ( ys: Tensor, xs: Tensor, batch_axis: int | None = None ) Jacobian [source]
- paddle.autograd. jacobian ( ys: Sequence[Tensor], xs: Sequence[Tensor], batch_axis: int | None = None ) tuple[tuple[paddle.autograd.autograd.Jacobian, ...], ...]
- paddle.autograd. jacobian ( ys: Tensor, xs: Sequence[Tensor], batch_axis: int | None = None ) tuple[paddle.autograd.autograd.Jacobian, ...]
- paddle.autograd. jacobian ( ys: Sequence[Tensor], xs: Tensor, batch_axis: int | None = None ) tuple[paddle.autograd.autograd.Jacobian, ...]
-
Computes the Jacobian of the dependent variable
ysversus the independent variablexs.Where
ysrepresents the output ofxsafter a certain operation,ysandxscan be Tensor or tuple of Tensors,batch_axisindicates the position of the batch dimension of the parameter data.When the input is a tuple Tensors, the returned result is a
Jacobianobject with the same number of nesting levels asxs, and each Jacobian has the same shape as Thexstuples are identical in one-to-one correspondence.When
batch_axis=None, only 0-dimensional Tensor or 1-dimensional Tensor is supported, assuming the shape ofxsis[N, ], the shape ofysis[M, ], then the output Jacobian matrix shape is[M, N].When
batch_axis=0, only 1-dimensional Tensor or 2-dimensional Tensor is supported, assuming the shape ofxsis[B, N], The shape ofysis[B, M], then the output Jacobian matrix shape is[B, M, N].
After the
Jacobianobject is created, the actual calculation process does not occur, but the lazy evaluation method is used for calculation. It can be multi-dimensional indexed to obtain the entire Jacobian matrix or sub-matrix, and the actual calculation will be performed at this time the value is calculated and the result is returned. At the same time, in the actual evaluation process, the calculated sub-matrix will be cached to avoid duplicate calculations in the subsequent indexing process.For example, assuming
JacobianinstanceJhas shape[B, M, N], assumingM > 4, thenJ[:, 1:4:1, :]means to get the values from row1to row3ofJ. In actual calculation, only the rows1to3are evaluated, and the calculation results of1to3will be cached at the granularity of the row, and will be used next time. When obtaining one or more rows of results above, the already calculated parts will not be recalculated.- Parameters
-
ys (Union[paddle.Tensor, Tuple[paddle.Tensor, ...]]) – Output or tuple of outputs derived from xs.
xs (Union[paddle.Tensor, Tuple[paddle.Tensor, ...]]) – Input or tuple of inputs.
batch_axis (Optional[int], optional) – Index of batch axis. Defaults to None.
- Returns
-
Jacobian(s) of ys derived from xs.
- Return type
-
Union[Tuple[Tuple[Jacobian, …], …], Tuple[Jacobian, …], Jacobian]
Examples
>>> import paddle >>> x1 = paddle.randn([3, ]) >>> x2 = paddle.randn([3, ]) >>> x1.stop_gradient = False >>> x2.stop_gradient = False >>> y = x1 + x2 >>> J = paddle.autograd.jacobian(y, (x1, x2)) >>> J_y_x1 = J[0][:] # evaluate result of dy/dx1 >>> J_y_x2 = J[1][:] # evaluate result of dy/dx2 >>> print(J_y_x1.shape) [3, 3] >>> print(J_y_x2.shape) [3, 3]
