NameError:未定义“线性回归”

问题描述 投票:0回答:1

这里是一个代码片段,其中我正在使用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()
python pytorch linear-regression
1个回答
0
投票

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()
© www.soinside.com 2019 - 2024. All rights reserved.