Here the graph built by autograd will represent only " y = mat . inpt".

That’s because mat = Variable(torch.Tensor([1,cosval,cosval])) is a leaf node (think that mat creation torch.Tensor([1,cosval,cosval] is not a differentiable operation).

Also Variableis deprecated and you should use Tensor instead.
If you replace them by Tensor and use torch.cat to create mat:

mat = torch.cat((torch.Tensor([1]), cosval, cosval))

it should do.

You will also need to call retain_grad on Tensors you want to access the gradient after the backward pass. So you should end up with something like:

Thank you. Your solution solved my problem. But I am just wondering how come torch.Tensor([1,cosval,cosval]) is not differentiable and torch.cat((torch.Tensor([1]),cosval,cosval)) is differentiable?. Is it because torch.Tensor() is a leaf node where as torch.cat() is a torch op and makes it as a part of the graph?