I am making my own linear regression model from scratch

Here is the code

w = torch.tensor(-10.0, requires_grad=True)
X = torch.arange(-3, 3.1, 0.1, requires_grad=True).view(-1, 1)
F = -3*X
Y = f + 0.1 * torch.randn(X.size())
def forward(x):
global w
y = w*x
return y
def mse(yhat, y):
return torch.mean((y-yhat)**2)
learning_rate = 0.1
for epoch in range(1, 11):
yhat = forward(x)
loss = mse(yhat, y)
loss.backward(retain_graph=True) # error occurs here
pass

The following exception is thrown after a successful iteration of the dataset

RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.