【动手学深度学习v2】1-1 手动实现线性回归

这个是跟随沐神学习深度学习的系列笔记,自己手写后对代码做了比较详细的注释(自己的理解)。本篇文章所对应的内容可跳转线性回归从0开始实现。

# 加载所需要的包
%matplotlib inline
import random
import torch
from d2l import torch as d2l

1. 生成数据集

# genrate our data
def gen_data(w, b, n_samples):
    # genrate X: from distribution N(0,1), 'n_samples' sample, every sample has len(w) features
    X = torch.normal(0, 1, (n_samples, len(w)))
    y = torch.matmul(X, w) + b # torch.matmul(), tensor's high-dim mutiply
    y += torch.normal(0, 0.01, y.shape) # add error term
    return X, y.reshape((-1, 1)) # reshape(1,-1)--> a row, reshape(-1,1)--> a column

# genrate ture values of w and b
true_w = torch.tensor([2.0, -3.4]) # here must have the same type(e.g. both are float) of X
true_b = 3.2

# get data (X, y)
features, labels = gen_data(true_w, true_b, 1000)
# print a element of our features and labels
print('features:', features[0])
print('\nlabels', labels[0])

# 结果为
features: tensor([ 1.0330, -0.2211])
labels tensor([6.0169])

2. 画散点图观察数据集

# scatter plot the last column of X and y
d2l.set_figsize()
d2l.plt.scatter(features[:, 1].detach().numpy(),
               labels.detach().numpy(), 1)

3. 将数据集切分成batch

# get batch data
def data_iter(batch_size, features, labels):
    n_samples = len(features) # get number of samples from X
    indices = list(range(n_samples))  # generate a index list
    random.shuffle(indices) # indices become a random sorting incidices
    for i in range(0, n_samples, batch_size):
        # generate index of every batch
        batch_indices = torch.tensor(indices[i:min(i+batch_size, n_samples)]) 
        # devided data(X, y) into batch
        yield features[batch_indices], labels[batch_indices]
batch_size = 10
# view 1st batch data
for X, y in data_iter(batch_size, features, labels):
    print(X, '\n', y)
    break

# 结果为
tensor([[ 0.6136, -0.5979],
        [ 0.5385,  1.1268],
        [ 0.5900, -0.8186],
        [ 0.2509, -1.3894],
        [-1.6209,  0.6753],
        [-1.5588,  1.6148],
        [ 0.1882, -1.4865],
        [-0.5050,  0.6768],
        [-0.6455, -1.2751],
        [ 1.1593, -1.7230]]) 
 tensor([[ 6.4411],
        [ 0.4455],
        [ 7.1471],
        [ 8.4169],
        [-2.3312],
        [-5.4097],
        [ 8.6301],
        [-0.1196],
        [ 6.2630],
        [11.3742]])

4. 设定参数的初始值

# give the initial values of w and b, and recoard grad
w = torch.normal(0, 0.01, size=(2, 1), requires_grad=True)
b = torch.zeros(1, requires_grad=True)

5. 定义模型

# def model--linear regression
def linreg(X, w, b):
    return torch.matmul(X, w) + b

6. 定义损失函数

# def loss function--square loss
def squared_loss(y_hat, y):
    # use.reshape() to ensure the dim of y_hat and y are the same
    return(y_hat-y.reshape(y_hat.shape))**2 / 2

7. 定义优化算法

# def optimal method--sgd
# input: 
## params: the params which are need to be update
## lr: learning rate
## batch_size: the size of batch
def sgd(params, lr, batch_size):
    # 在该模块下,所有计算得出的tensor的requires_grad都自动设置为False
    with torch.no_grad():
        for param in params:
            # update param
            param -= lr * param.grad / batch_size
            # 清零梯度值
            param.grad.zero_()

8. 训练

# training process
lr = 0.03
num_epoches = 3
net = linreg
loss = squared_loss

for epoch in range(num_epoches):
    for X, y in data_iter(batch_size, features, labels):
        # calling function loss = squared_loss(y_hat, y)
        # calling function y_hat = net = linreg(X, w, b)
        l = loss(net(X, w, b), y)
        # 对求和后的l(标量) 反向传播--对w和b求导,储存到w.grad和b.grad中
        l.sum().backward()
        sgd([w, b], lr, batch_size) # update params using sgd
    with torch.no_grad():
        # calculate training loss under current params
        train_l = loss(net(features, w, b), labels)
        print(f'epoch {epoch+1}, loss{float(train_l.mean()):f}')


结果为:
epoch 1, loss0.031723
epoch 2, loss0.000132
epoch 3, loss0.000054

9. 查看参数的误差

print(f'w error:{true_w - w.reshape(true_w.shape)}')
print(f'b error:{true_b - b}')

# 结果为
w error:tensor([ 0.0003, -0.0010], grad_fn=<SubBackward0>)
b error:tensor([-2.1458e-05], grad_fn=<RsubBackward1>)

你可能感兴趣的:(动手学深度学习,深度学习,线性回归,python)