d=2
n=50
X=torch.randn(n,d)
true_w=torch.tensor([[-1.0],[2.0]])
y=X@true_w+torch.randn(n,1)*0.1
print(‘X shape’,X.shape)
print(‘y shape’,y.shape)
print(‘w shape’,true_w.shape)
def model(X,w):
return X@w
def rss(y,y_hat):
return torch.norm(y-y_hat)**2/n
def grad_rss(X,y,y_hat):
return -2*X.t()@(y-y_hat)/n
w=torch.tensor([[1.0],[0.0]],requires_grad=True)
y_hat=model(X,w)
loss=rss(y,y_hat)
loss.backward()
print(‘Analytical gradient’, grad_rss(X, y, y_hat).detach().view(2).numpy())
print(‘PyTorch’s gradient’, w.grad.view(2).numpy())
print(grad_rss(X,y,y_hat))
console:
grad_rss(X,y,y_hat)
tensor([[ 5.1305],
[-3.3439]], grad_fn=)