"optimizer got an empty parameter list"Error

Hi Everyone ,I am a complete beginner in Pytorch,I was trying to implement a very simple Linear Regression Model,but I am getting this “optimizer got an empty list error”.Kindly note that the x_train is a one -dimensional vector.Please mention the appropriate changes .Thanks!!!in Advance
This my piece of code
class LinearRegressionModel(nn.Module):
def init(self):
super(LinearRegressionModel,self).init()
self.linear=nn.Linear(1,1)
def forward(self,x):
out=self.linear(x)
return out

model=LinearRegressionModel()
criterion=nn.MSELoss()
learning_rate=0.01
optimizer=torch.optim.SGD(model.parameters(),lr=learning_rate)

Your code works fine using this dummy training loop:

x = torch.randn(10, 1)
target = torch.randn(10, 1)

for epoch in range(10):
    optimizer.zero_grad()
    output = model(x)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()
    print('Epoch {}, loss {}'.format(epoch, loss.item()))

Are you sure you haven’t reassigned model to another variable/tensor/etc.?
If you print list(model.parameters()), you’ll see both the weight and bias of your linear layer.

PS: You can add code snippets using three backticks ` :wink:

Thank brother it really helps!!