Hi,
Regarding your error, I don’t see any problem in the code you provided. Can you double check if the error still exists by restarting your jupyter notebook? You can try it on google colab and share it here.
BTW, pytorch provides much simpler and more elegant way to do linear regression. The following code is an example of linear regression.
import torch.nn as nn
from torch.optim import SGD

net = nn.Linear(4, 1, bias=True)  
# Build your model, by default weights and bias are initialized by Kaiming initialization see code at https://github.com/pytorch/pytorch/blob/e6a3154519721eec323b227ea8dfbd05409cfe37/torch/nn/modules/linear.py#L88

# you can also initialize the weights manually by nn.init
optimizer = SGD(lr=0.02)

for epoch in range(num_of_epochs):
    # train
    for example, label in data_iter(x, y, batch_size):
        optimizer.zero_grad()                          # clean the gradient before backward()
        output = net(example)                         # forward pass
        train_loss = loss(output, label).sum()
        train_loss.backward()                         # backward pass to accumulate gradient 
        optimizer.step()                                   # update model parameters
       
    # estimate
    with torch.no_grad():
        test_loss = loss(net(x), y)
        print(f"epoch {epoch + 1:3d}, loss = {float(test_loss.mean()):.2f}")