《动手学深度学习》用PyCharm实现 学习笔记(2)3.2线性回归的从零开始实现

第一个问题:matplotlib代码没办法照抄 画图的东西一直报错

查阅matplotlib文档,直接画散点图
惊奇之下 发现把关于画图的代码全删了就好
emmmm
虽然学新东西也蛮重要的
但是先学一下深度学习的东西

代码

from matplotlib import pyplot as plt
from mxnet import autograd, nd
import random

num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
features = nd.random.normal(scale=1, shape=(num_examples, num_inputs))
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += nd.random.normal(scale=0.01, shape=labels.shape)

plt.scatter(features[:, 1].asnumpy(), labels.asnumpy(), 1)


def data_iter(batch_size, features, labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    random.shuffle(indices)  # 样本的读取顺序是随机的
    for i in range(0, num_examples, batch_size):
        j = nd.array(indices[i: min(i + batch_size, num_examples)])
        yield features.take(j), labels.take(j)  # take函数根据索引返回对应元素

batch_size = 10

for X, y in data_iter(batch_size, features, labels):
    print(X, y)
    break
w = nd.random.normal(scale=0.01, shape=(num_inputs, 1))
b = nd.zeros(shape=(1,))
w.attach_grad()
b.attach_grad()


def linreg(X, w, b):  # 本函数已保存在d2lzh包中方便以后使用
    return nd.dot(X, w) + b


def squared_loss(y_hat, y):  # 本函数已保存在d2lzh包中方便以后使用
    return (y_hat - y.reshape(y_hat.shape)) ** 2 / 2


def sgd(params, lr, batch_size):  # 本函数已保存在d2lzh包中方便以后使用
    for param in params:
        param[:] = param - lr * param.grad / batch_size


lr = 0.03
num_epochs = 3
net = linreg
loss = squared_loss

for epoch in range(num_epochs):  # 训练模型一共需要num_epochs个迭代周期
    # 在每一个迭代周期中,会使用训练数据集中所有样本一次(假设样本数能够被批量大小整除)。X
    # 和y分别是小批量样本的特征和标签
    for X, y in data_iter(batch_size, features, labels):
        with autograd.record():
            l = loss(net(X, w, b), y)  # l是有关小批量X和y的损失
        l.backward()  # 小批量的损失对模型参数求梯度
        sgd([w, b], lr, batch_size)  # 使用小批量随机梯度下降迭代模型参数
    train_l = loss(net(features, w, b), labels)
    print('epoch %d, loss %f' % (epoch + 1, train_l.mean().asnumpy()))
print(true_w, w)
print(true_b, b)

输出结果

[[-0.5489446   0.9783404 ]
 [-0.8288413   0.9591693 ]
 [-0.57819754  0.22655894]
 [ 1.5509241   0.63531184]
 [ 1.0059307  -0.41143978]
 [-0.23816615 -0.33349228]
 [ 0.1924875  -0.34041473]
 [-0.55021375 -1.5918757 ]
 [-2.513012    1.205448  ]
 [-0.71230924 -0.45548564]]
<NDArray 10x2 @cpu(0)> 
[-0.22527038 -0.7172271   2.2729282   5.141447    7.6126523   4.874075
  5.746613    8.525719   -4.9235134   4.3303328 ]
<NDArray 10 @cpu(0)>
epoch 1, loss 0.034922
epoch 2, loss 0.000127
epoch 3, loss 0.000049
[2, -3.4] 
[[ 1.9996107]
 [-3.399508 ]]
<NDArray 2x1 @cpu(0)>
4.2 
[4.199965]
<NDArray 1 @cpu(0)>

课后练习 更改学习率 观察损失函数下降快慢

学习率0.01

epoch 1, loss 2.185575
epoch 2, loss 0.287532
epoch 3, loss 0.038025

学习率 0.05

epoch 1, loss 0.000542
epoch 2, loss 0.000049
epoch 3, loss 0.000049

学习率 0.10

epoch 1, loss 0.000049
epoch 2, loss 0.000048
epoch 3, loss 0.000048

应该是 学习率越高,损失函数下降速度越快

你可能感兴趣的:(学习笔记,python,深度学习,神经网络)