针对y=ax+.by+c 格式 首先自动生成一千个数据,然后使用模型训练。代码复制即可运行。
import torch
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random
import torch.utils.data as Data
from torch import nn
from torch.nn import init
import torch.optim as optim
#shengchengshujuji
num_inputs = 2
num_examples = 1000
true_b = 3.2
true_w = [1.2,-2.4]
features = torch.tensor(np.random.normal(0,1,(num_examples,num_inputs)),dtype=torch.float)
labels = true_w[0] * features[:,0] + true_w[1] * features[:,1] + true_b
labels += torch.tensor(np.random.normal(0,0.01,size=labels.size()),dtype=torch.float)
#读取数据集
batch_size = 10
dataset = Data.TensorDataset(features,labels)
data_iter = Data.DataLoader(dataset,batch_size,shuffle=True)
#定义模型
net = nn.Sequential(
nn.L
深度学习入门,自动生成数据训练线性回归模型
最新推荐文章于 2024-08-21 17:58:45 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)