NameError:“线性回归”未定义

时间:2019-12-23 09:03:36

标签: python pytorch linear-regression

这是我使用Pytorch应用线性回归的代码片段。我遇到一个NameError,说未定义名称“线性回归”。请帮助纠正它。

import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

x_data=Variable(torch.Tensor([[10.0],[9.0],[3.0],[2.0]]))
y_data=Variable(torch.Tensor([[90.0],[80.0],[50.0],[30.0]]))

class LinearRegression(torch.nn.Module):

  def __init__(self):
    super(LinearRegression,self). __init__ ()
    self.linear = torch.nn.Linear(1,1)

  def forward(self, x):
    y_pred = self.linear(x)
    return y_pred

  model = LinearRegression()

1 个答案:

答案 0 :(得分:2)

model = LinearRegression()应该在课堂之外

import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

x_data=Variable(torch.Tensor([[10.0],[9.0],[3.0],[2.0]]))
y_data=Variable(torch.Tensor([[90.0],[80.0],[50.0],[30.0]]))

class LinearRegression(torch.nn.Module):

  def __init__(self):
    super(LinearRegression,self). __init__ ()
    self.linear = torch.nn.Linear(1,1)

  def forward(self, x):
    y_pred = self.linear(x)
    return y_pred

model = LinearRegression()