Pytorch入门案例---线性回归
程序员文章站
2022-06-11 22:47:03
...
import torch
from torch import nn
import numpy as np
import torch.utils.data as Data
from torch.nn import init
# 使得模型的可复现性
torch.manual_seed(1)
# 设置默认的数据格式
torch.set_default_tensor_type('torch.FloatTensor')
# 1.数据处理
num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
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)
# 2.读取数据
batch_size = 10
dataset = Data.TensorDataset(features, labels)
# 查看数据的存储类型 [*dataset]
data_iter = Data.DataLoader(dataset, batch_size, shuffle=True, num_workers=2)
# 3.定义网络
class LinearNet(nn.Module):
def __init__(self, n_feature):
super(LinearNet, self).__init__()
self.linear = nn.Linear(n_feature, 1)
def forward(self, x):
return self.linear(x)
net = LinearNet(feature)
# 初始化网络的参数(有多种初始化的方法,但是一般偏值设置为零)
for name, param in net.named_parameters():
if name == "linear.weight":
#权重
init.normal_(param, mean=0.0, std=0.01)
else:
#偏值
init.constant_(param, val=0.0)
# 4.训练
# 超参数
lr = 0.01
batch_size = 10
num_epochs = 5
# 定义损失
critrion = nn.MSELoss()
# 定义优化函数
optimizer = t.optim.SGD(net.parameters(), lr=lr)
for epoch in range(num_epochs):
for X, y in data_iter:
out = net(X)
loss = critrion(out, y.view(-1, 1))
# 梯度清零
optimizer.zero_grad()
loss.backward()
# 梯度更新
optimizer.step()
print("epcoch:%d, loss:%f"%(epoch, loss.item()))
- 结果
epcoch:0, loss:0.861575
epcoch:1, loss:0.015179
epcoch:2, loss:0.000336
epcoch:3, loss:0.000168
epcoch:4, loss:0.000151
上一篇: Kendo UI dropdownlist 后台绑定默认值
下一篇: yii 怎么改变默认的控制器