PyTorch之线性回归
第1关:初始化参数
import torch
import torchvision.datasets as dsets
import torchvision.transforms as transforms
#/********** Begin *********/
# 下载MNIST数据集
Mnist_dataset = dsets.MNIST(root='./data',train=True,transform=transforms.ToTensor(),
download=True)
# 创建batch_size=100, shuffle=True的DataLoader类型的变量data_loader
data_loader = torch.utils.data.DataLoader(dataset=Mnist_dataset,
batch_size=100,
shuffle=True)
#输出 data_loader中数据类型
print(type(data_loader.dataset))
#/********** End *********/
第2关:建立模型,定义损失和优化函数
import torch.nn as nn
#/********** Begin *********/
# 线性回归模型
class LinearRegression(nn.Module):
def __init__(self):
# 调用Module的初始化
super(LinearRegression, self).__init__()
# 输入和输出分别为一维
self.linear = nn.Linear(1, 1)
# module调用forward,将按forward进行前向传播,并构建网络
def forward(self, x):
out = self.linear(x)
return out
# 实例化一个新建的模型变量model
model = LinearRegression()
# 输出该模型 model 的‘.parameters'属性
print(model.parameters)
#/********** End *********/
第3关:训练模型
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from torch.autograd import Variable
import os
import sys
path = os.path.split(os.path.abspath(os.path.realpath(sys.argv[0])))[0] + os.path.sep
print(path)
# 超参数
input_size = 1
output_size = 1
num_epochs = 60
learning_rate = 0.001
# 数据集
x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168],
[9.779], [6.182], [7.59], [2.167], [7.042],
[10.791], [5.313], [7.997], [3.1]], dtype=np.float32)
y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573],
[3.366], [2.596], [2.53], [1.221], [2.827],
[3.465], [1.65], [2.904], [1.3]], dtype=np.float32)
# 线性回归模型
class LinearRegression(nn.Module):
def __init__(self, input_size, output_size):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(input_size, output_size)
def forward(self, x):
out = self.linear(x)
return out
model = LinearRegression(input_size, output_size)
#创建输出文件 output.txt
f = open(path + 'output.txt', 'w')
f.seek(0)
f.truncate() #清空文件
#/********** Begin *********/
# 创建损失函数MSELoss
criterion = nn.MSELoss()
# 创建SGD的Optimizer,学习率l'r为0.001
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
# 将x_train,y_train数据转换为Variable类型
inputs = Variable(torch.from_numpy(x_train))
targets = Variable(torch.from_numpy(y_train))
# Forward
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
# Backward
loss.backward()
#Optimize
optimizer.step()
#共训练60次,分别10次输出一回loss信息,并将输出信息存到文件中
if (epoch+1) % 10 == 0:
f.writelines('Epoch [%d/%d], Loss: %.4f \n'%(epoch+1, num_epochs, loss.data[0]))
print ('Epoch [%d/%d], Loss: %.4f'
%(epoch+1, num_epochs, loss.data[0]))
f.close()
#/********** End *********/
#保存模型
torch.save(model,path + 'model.pkl')
第4关:validation
import torch
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from torch.autograd import Variable
import torch.nn as nn
import warnings
warnings.filterwarnings('ignore')
import os,sys
path = os.path.split(os.path.abspath(os.path.realpath(sys.argv[0])))[0] + os.path.sep
path = path[:-6]
print("validation path:" ,path)
# Linear Regression Model
class LinearRegression(nn.Module):
def __init__(self, input_size, output_size):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(input_size, output_size)
def forward(self, x):
out = self.linear(x)
return out
model = LinearRegression(1, 1)
x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168],
[9.779], [6.182], [7.59], [2.167], [7.042],
[10.791], [5.313], [7.997], [3.1]], dtype=np.float32)
y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573],
[3.366], [2.596], [2.53], [1.221], [2.827],
[3.465], [1.65], [2.904], [1.3]], dtype=np.float32)
#加载整个模型
model = torch.load( path + 'step3/model.pkl')
#/********** Begin *********/
#将模型转化为测试模式
#将模型转化为测试模式
model.eval()
#利用 model 计算预测值
predicted = model(Variable(torch.from_numpy(x_train))).data.numpy()
print(predicted)
#画图
plt.plot(x_train, y_train, 'ro', label='Original data')
plt.plot(x_train, predicted, label='Fitted line')
plt.legend()
plt.savefig(path + "step4/outputimages/mylossTest.png")
#/********** End *********/
文章来源地址https://www.toymoban.com/news/detail-526364.html
文章来源:https://www.toymoban.com/news/detail-526364.html
到了这里,关于PyTorch之线性回归的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!