Am I using polynomial regression right in pytorch?
I am trying to do polynomial regression by pytorch. First I just tried only linear regression (b + wx).

model_1 = RegressionModel()
W = torch.zeros(1, requires_grad=True)
b = torch.zeros(1, requires_grad = True)
optimizer_1 = torch.optim.SGD([W, b], lr = 0.001)

x_train = torch.FloatTensor(dataset.x_data['LSTAT'])
y_train = torch.FloatTensor(dataset.data['target'])

nb_epochs = 10000

for epoch in range(nb_epochs + 1):
    hypothesis = x_train * W + b
    cost = torch.nn.functional.mse_loss(hypothesis, y_train.float())

    optimizer_1.zero_grad()
    cost.backward()
    optimizer_1.step()

    print('Epoch {:4d}/{} W: {:.3f}, b: {:.3f}, Cost: {:.6f}'.format(epoch, 
    nb_epochs, W.item(), b.item(), cost.item()))


Then I changed and added some variables to do polynomial regression (b + w1x + w2x^2)

model_2 = RegressionModel()
W1 = torch.zeros(1, requires_grad=True) 
W2 = torch.zeros(1, requires_grad=True) 
b = torch.zeros(1, requires_grad = True)
optimizer_2 = torch.optim.SGD([W2, W1, b], lr = 0.0000099)

x_train = torch.FloatTensor(dataset.x_data['LSTAT'])
y_train = torch.FloatTensor(dataset.data['target'])

nb_epochs = 10000

for epoch in range(nb_epochs + 1):
    hypothesis = b + x_train * W1 + x_train * x_train * W2
    cost = torch.nn.functional.mse_loss(hypothesis, y_train.float())

    optimizer_2.zero_grad()
    cost.backward()
    optimizer_2.step()

    print('Epoch {:4d}/{} W1: {:.3f}, W2: {:.3f}, b: {:.3f}, Cost: 
    {:.6f}'.format(epoch, nb_epochs, W1.item(), W2.item(), b.item(), 
    cost.item()))


Can I try polynomial regression like this? If not, I would be really appreciate if you let me know. I'm really noob to pytorch...

Your code should work. When working with larger data, it will be more efficient if you do the regression in a single matrix operation. For that, you need to first pre-compute polynomials of your input features:

x_train_polynomial = torch.stack([x_train, x_train ** 2], dim=1)


To save some lines, you can rewrite the projection a linear layer:

import torch.nn as nn
projection = nn.Linear(2, 1, bias=True)


In the training loop, you can call:

hypothesis = projection(x_train_polynomial)

