python - 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()
解决方案
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()
推荐阅读
- java - 如何为具有其他相关实体的实体管理我的 API 端点?
- java - 如何在不使用“抛出 XXXException”的情况下处理“未处理的异常”
- next.js - Nextjs 9.3 tailwindcss + antd 构建缺少 css
- c# - catch 子句的奇怪行为
- php - 难以在 PHP 脚本中修改 JSON 对象
- c# - 如何在 .NET Core 中检索当前登录用户的电子邮件?
- node.js - Node.js SSE res.write() 不在流中发送数据
- python - 为什么程序多次运行时python的set对象中元素的顺序会发生变化
- python - 由于权限问题而更改缓存目录
- git - Pip 安装修改后的 MLflow 分叉