近期在跟小伙伴一起 结对学习深度学习,《动手学习深度学习》这本书还是挺推荐的。
本篇博客是我第一篇博客,也是学习的第一课,一个简单的线性回归从0开始实现。过程较为简单,直接上代码吧。
import torch
from matplotlib import pyplot as plt
import numpy as np
import random
from torch import cuda
#生成数据集
#设置特征的数量
num_inputs=2
#设置样本数量
num_example=2000
#设置真实的参数,权重矩阵和偏差
true_w=[2,-3.4]
true_b=4.2
#根据上述模型,生成随机数据集
features = torch.randn(num_example, num_inputs,
dtype=torch.float32)
# print(features)
#根据特征生成标签,并且依照权重生成
labels=true_w[0]*features[:,0]+true_w[1]*features[:,1]+true_b
# print(labels.size())
#在原有的labels的基础上,加上一个均值为0,方差为0.01的正态分布的偏差,随机加入偏差
labels+=torch.tensor(np.random.normal(0,0.01,size=labels.size()),dtype=torch.float32)
#可视化一下样本数据
# plt.scatter(features[:,1].numpy(),labels.numpy(),1)
# plt.show()
#根据之前定义好的数据集,结合每次取出的数据量,对feature中的数据进行读取
def data_iter(batch_size, features, labels):
num_examples = len(features)
indices = list(range(num_examples))
random.shuffle(indices) # random read 10 samples
for i in range(0, num_examples, batch_size):
j = torch.LongTensor(indices[i: min(i + batch_size, num_examples)]) # the last time may be not enough for a whole batch
yield features.index_select(0, j), labels.index_select(0, j)
#测试,取出特征和标签
batch_size=100
for X,y in data_iter(batch_size,features,labels):
print(X,'\n',y)
break
#初始化模型参数,参数的数量和上面的内容保持一致
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):
return (y_hat - y.view(y_hat.size())) ** 2 / 2
#定义SGD随机梯度下降优化函数
def sgd(params, lr, batch_size):
for param in params:
param.data -= lr * param.grad / batch_size # ues .data to operate param without gradient track
#训练模型
learning_rate=0.03
num_epochs=5000
#初始化模型到
# torch.cuda.set_device(0)
net=linreg
loss=squared_loss
from datetime import datetime
import time
a = datetime.now() #获得当前时间
# training
for epoch in range(num_epochs): # training repeats num_epochs times
# in each epoch, all the samples in dataset will be used once
# X is the feature and y is the label of a batch sample
for X, y in data_iter(batch_size, features, labels):
l = loss(net(X, w, b), y).sum()
# calculate the gradient of batch sample loss
l.backward()
# using small batch random gradient descent to iter model parameters
sgd([w, b], learning_rate, batch_size)
# reset parameter gradient
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()))
b = datetime.now() # 获取当前时间
durn = (b-a).seconds #两个时间差,并以秒显示出来
print(durn)
因为注释写的很详细,所以就不再做过多解释。这是第一次博客经历,后续会经常更新,如果有错误,还请各位同仁能原谅新手。