梯度下降入门
import torch
def gradDescent(X, y, eps = torch.tensor(0.01, requires_grad = True), numIt = 1000):
m, n = X.shape
weights = torch.zeros(n, 1, requires_grad = True)
for k in range(numIt):
grad = torch.mm(X.t(), (torch.mm(X, weights) - y))/2
weights = weights - eps * grad
return weights
X = torch.tensor([[1.,1],[3, 1]], requires_grad = True)
y = torch.tensor([2.,4], requires_grad = True).reshape(2,1)
weights = gradDescent(X, y, numIt = 10000)
print(weights)