首页 > 解决方案 > NameError:“线性回归”未定义

问题描述

这是一个代码片段,我在其中使用 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()

标签: pythonpytorchlinear-regression

解决方案


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()

推荐阅读