【PyTorch】基于PyTorch的线性回归

代码

#导入库
import torch
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random
%matplotlib inline  #jupyter notebook 需要添加,pycharm不需要

#两个输入,设置1000个样本,真实权重true_w,真实偏置true_b
num_inputs = 2
num_examples = 1000
true_w = [2,-3.4]
true_b = 4.2
#随机生成1000*2的向量 即特征
features = torch.randn(num_examples,num_inputs,dtype = torch.float32)
#y = w1*x1 + w2*x2 + b
labels = true_w[0]*features[:,0] + true_w[1]*features[:,1] +true_b
#对于lables天机一些随机变量,否则完全按照线性运算执行,没有训练的必要
labels += torch.tensor(np.random.normal(0,0.01,size = labels.size()))
print(features[0])
print(labels[0])

#生成散点图  
def use_svg_display():
    #矢量图显示
    display.set_matplotlib_formats('svg')
    
def set_figsize(figsize = (3.5,2.5)):
    use_svg_display()
    #设置图片尺寸
    plt.rcParams['figure.figsize']= figsize
#plt.scatter()用来生成散点图  ,最后一个参数用来设置浮点的大小 
set_figsize()
plt.scatter(features[:,1].numpy(),labels.numpy(),1)

#每次返回bitchsize个样本特征和标签
def data_iter(batch_size,features,labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    #random.shuffle随机排列列表
    random.shuffle(indices)
    
    for i in range(0,num_examples,batch_size):
        #最后一次可能不足一个batchsize
        j = torch.LongTensor(indices[i:min(i+batch_size,num_examples)])
        #yield 和return区别,就是都会返回值,但是yield会接着上次停止的位置执行
        yield features.index_select(0,j),labels.index_select(0,j)

batch_size = 10
#输出特征及类别
for X,y in data_iter(batch_size,features,labels):
    print(X,y)
    break

#参数初始化,均值为0,标准差为0.01的正太随机数
w = torch.tensor(np.random.normal(0,0.01,(num_inputs,1)),dtype = torch.float32)
b = torch.zeros(1,dtype = torch.float32)
#求梯度
w.requires_grad_(requires_grad = True)
b.requires_grad_(requires_grad = True)

#定义模型
def linreg(X,w,b):
    return torch.mm(X,w)+b

#定义损失函数
def squared_loss(y_hat,y):
    #view  这里返回的是向量
    return (y_hat - y.view(y_hat.size()))**2/2

#定义优化算法  小批量随机梯度下降算法
def sgd(params,lr,batch_size):
    for param in params:
        param.data -= lr*param.grad/batch_size
#训练
lr = 0.03
num_epochs = 3
net = linreg
loss = squared_loss

#遍历每一个epoch
for epoch in range(num_epochs):
    for X,y in data_iter(batch_size,features,labels):
        #小批量损失
        l = loss(net(X,w,b),y).sum()
        l.backward()
        sgd([w,b],lr,batch_size)
        
        #梯度清零
        w.grad.data.zero_()
        b.grad.data.zero_()
        
    train_l = loss(net(features,w,b),labels)
    print('epoch %d, loss %f'%(epoch+1,train_l.mean().item()))

#比较学习到的参数和真实参数,比较接近
print(true_w,'\n',w)
print(true_b,'\n',b)

你可能感兴趣的:(python,深度学习,pytorch)