前言:
知识读不懂怎么办???
没关系,只有一个办法靠谱:硬着头皮读完,然后反复读很多遍。这是一个特别重要的本领,很多人不知道。其实,重要的知识,从来都是通过反复学习才能获得的。一下就能学会得东西,通常上价值不大。对于我这种努力大于天赋的人来说,这句话可以说是很受用了。
线性回归理论知识比较简单,已经很熟悉了,这里不再赘述。但面试时经常问:你学的那些算法,你自己有实现过吗?在不用库函数的前提下。甚至有的时候,面试官会挑一个算法让你去实现,快速地写在纸上。
#导入要用到的库
# import packages and modules
%matplotlib inline
import torch
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random
'''生成数据集'''
# 设置特征个数
num_inputs = 2
# 设置样本个数
num_examples = 1000
# 分别设置真实的特征权重和偏置权重
true_w = [2, -3.4]
true_b = 4.2
#随机生成1000个样本,每个样本有两个特征,即1000*2
features = torch.randn(num_examples, num_inputs,
dtype=torch.float32)
#生成标签,加入噪声扰动,因为实际生活中的数据往往不会那么规则的呈线性分布
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.float32)
''' 定义读取数据集的函数'''
def data_iter(batch_size, features, labels):
'''
batch_size:共多少个批量
features:特征
labels:标签
'''
num_examples = len(features)
indices = list(range(num_examples))#indices [0, 1, 2, 3, 4, 5, 6, 7....999]
random.shuffle(indices) # random read 10 samples
# print(indices)
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 = 10
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)
#requires_grad=True 要求梯度
w.requires_grad_(requires_grad=True)
b.requires_grad_(requires_grad=True)
def linreg(X, w, b):
#torch.mm(a, b)是矩阵a和b矩阵相乘,比如a的维度是(1, 2),b的维度是(2, 3),返回的就是(1, 3)的矩阵
return torch.mm(X, w) + b
def squared_loss(y_hat, y):
#定义均方损失函数
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 # ues .data to operate param without gradient track
# super parameters init
lr = 0.03
num_epochs = 5
net = linreg
loss = squared_loss
# training
for epoch in range(num_epochs): # training repeats num_epochs times
#循环计算每一个批量
# 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()
# 计算每一个样本的loss
l.backward()
#用梯度下降优化参数
sgd([w, b], lr, 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()))
'''
运行结果
epoch 1, loss 0.034440
epoch 2, loss 0.000117
epoch 3, loss 0.000046
epoch 4, loss 0.000046
epoch 5, loss 0.000046
'''