Solving using python Jacoby (the Jacobian) matrix of the vector-valued function

  Consider a vector-valued function R $ ^ m \ rightarrow R ^ n $, i.e. $ \ textbf {y} = f (\ textbf {x}) $, its Jacoby (the Jacobian) matrix defined as follows.

  

  Recording a period of use under the following python code to request the function value of the Jacobian matrix, vector-valued function is available only if an error occurs for the scalar function.

import torch

# 定义函数
x = torch.tensor([1, 3, 5.], requires_grad=True)
A = torch.tensor([[1., 0, 1], [0, 1, 0], [1, 0, 1]])
y = A@x


Weight = torch.eye(y.size()[0])
B = torch.tensor([])
for i, weight in enumerate(Weight):
    B = torch.cat((B, torch.autograd.grad(y, x, grad_outputs=weight, retain_graph=True)[0]), 0) 
print(B.view((y.size()[0], -1)))

  Here we $ x = [1,3,5] ^ T, y = Ax $ example, the output results are as follows:

 

Guess you like

Origin www.cnblogs.com/chester-cs/p/11754603.html