i use want to get homography use svd, as formula, i use the last vector of V, but torch gets grad all zero;
import torch
A_rows = [torch.randn(8, 9)] # 示例数据
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
A = torch.cat(A_rows).view(-1, 9).to(device)
A.requires_grad_(True)
U, S, Vt = torch.linalg.svd(A)
H = Vt[-1] / Vt[-1, -1]
H = H.view(3, 3)
A.retain_grad()
H.retain_grad()
Vt.retain_grad()
loss = H.norm()
print(“Loss:”, loss)
loss.backward()
print(“A.grad:”, A.grad)
print(“V.grad:”, Vt.grad)
print(“H.grad:”, H.grad)
then, i print H’s Vt’s A’s grad, show:
estimated_homo_matrix grad:
A.grad: tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.]], device=‘cuda:0’)
V.grad: tensor([[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000],
[ 1.7672, -1.3057, 2.8489, 1.8927, 0.2041, 2.3516, 0.9954, -1.9848,
27.0288]], device=‘cuda:0’)
H.grad: tensor([[-0.3338, 0.2466, -0.5381],
[-0.3575, -0.0386, -0.4442],
[-0.1880, 0.3749, 0.1889]], device=‘cuda:0’)
why, all zeros?