Pytorch Linear Algebra and Autograd
Pytorch Autograd can be used when doing linear algebra calculations. It must kept in mind that autograd requires a function that returns a scalar (usually interpreted as a loss function) in order to compute a gradient.Sample Code
x = [[1.0],[1.0]]
W = [[1.0,1.0],[1.0,2.0]]
input = torch.tensor(x)
weights = torch.tensor(W, requires_grad=True)
result = weights @ input
lresult = result.tolist()
#grad will be none here
#you need a function that returns a scalar in order to
grad = weights.grad
# 2. Reduce the result to a scalar (e.g., sum all elements)
loss = result.sum()
# 3. Backward pass (this computes the gradients)
loss.backward()
# Now grad will not be None
print(weights.grad)