多层感知机的从零开始实现
:label:sec_mlp_scratch
我们已经在 :numref:sec_mlp
中描述了多层感知机(MLP),
现在让我们尝试自己实现一个多层感知机。
为了与之前softmax回归( :numref:sec_softmax_scratch
)
获得的结果进行比较,
我们将继续使用Fashion-MNIST图像分类数据集
( :numref:sec_fashion_mnist
)。
```{.python .input} from d2l import mxnet as d2l from mxnet import gluon, np, npx npx.set_np()
```{.python .input}
#@tab pytorch
from d2l import torch as d2l
import torch
from torch import nn
```{.python .input}
@tab tensorflow
from d2l import tensorflow as d2l import tensorflow as tf
```{.python .input}
#@tab all
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
初始化模型参数
回想一下,Fashion-MNIST中的每个图像由 $28 \times 28 = 784$个灰度像素值组成。 所有图像共分为10个类别。 忽略像素之间的空间结构, 我们可以将每个图像视为具有784个输入特征 和10个类的简单分类数据集。 首先,我们将[实现一个具有单隐藏层的多层感知机, 它包含256个隐藏单元]。 注意,我们可以将这两个变量都视为超参数。 通常,我们选择2的若干次幂作为层的宽度。 因为内存在硬件中的分配和寻址方式,这么做往往可以在计算上更高效。
我们用几个张量来表示我们的参数。 注意,对于每一层我们都要记录一个权重矩阵和一个偏置向量。 跟以前一样,我们要为损失关于这些参数的梯度分配内存。
```{.python .input} num_inputs, num_outputs, num_hiddens = 784, 10, 256
W1 = np.random.normal(scale=0.01, size=(num_inputs, num_hiddens)) b1 = np.zeros(num_hiddens) W2 = np.random.normal(scale=0.01, size=(num_hiddens, num_outputs)) b2 = np.zeros(num_outputs) params = [W1, b1, W2, b2]
for param in params: param.attach_grad()
```{.python .input}
#@tab pytorch
num_inputs, num_outputs, num_hiddens = 784, 10, 256
W1 = nn.Parameter(torch.randn(
num_inputs, num_hiddens, requires_grad=True) * 0.01)
b1 = nn.Parameter(torch.zeros(num_hiddens, requires_grad=True))
W2 = nn.Parameter(torch.randn(
num_hiddens, num_outputs, requires_grad=True) * 0.01)
b2 = nn.Parameter(torch.zeros(num_outputs, requires_grad=True))
params = [W1, b1, W2, b2]
```{.python .input}
@tab tensorflow
num_inputs, num_outputs, num_hiddens = 784, 10, 256
W1 = tf.Variable(tf.random.normal( shape=(num_inputs, num_hiddens), mean=0, stddev=0.01)) b1 = tf.Variable(tf.zeros(num_hiddens)) W2 = tf.Variable(tf.random.normal( shape=(num_hiddens, num_outputs), mean=0, stddev=0.01)) b2 = tf.Variable(tf.zeros(num_outputs))
params = [W1, b1, W2, b2]
## 激活函数
为了确保我们对模型的细节了如指掌,
我们将[**实现ReLU激活函数**],
而不是直接调用内置的`relu`函数。
```{.python .input}
def relu(X):
return np.maximum(X, 0)
```{.python .input}
@tab pytorch
def relu(X): a = torch.zeros_like(X) return torch.max(X, a)
```{.python .input}
#@tab tensorflow
def relu(X):
return tf.math.maximum(X, 0)
模型
因为我们忽略了空间结构,
所以我们使用reshape
将每个二维图像转换为一个长度为num_inputs
的向量。
只需几行代码就可以(实现我们的模型)。
```{.python .input} def net(X): X = d2l.reshape(X, (-1, num_inputs)) H = relu(np.dot(X, W1) + b1) return np.dot(H, W2) + b2
```{.python .input}
#@tab pytorch
def net(X):
X = d2l.reshape(X, (-1, num_inputs))
H = relu(X@W1 + b1) # 这里“@”代表矩阵乘法
return (H@W2 + b2)
```{.python .input}
@tab tensorflow
def net(X): X = d2l.reshape(X, (-1, num_inputs)) H = relu(tf.matmul(X, W1) + b1) return tf.matmul(H, W2) + b2
## 损失函数
由于我们已经从零实现过softmax函数( :numref:`sec_softmax_scratch`),
因此在这里我们直接使用高级API中的内置函数来计算softmax和交叉熵损失。
回想一下我们之前在 :numref:`subsec_softmax-implementation-revisited`中
对这些复杂问题的讨论。
我们鼓励感兴趣的读者查看损失函数的源代码,以加深对实现细节的了解。
```{.python .input}
loss = gluon.loss.SoftmaxCrossEntropyLoss()
```{.python .input}
@tab pytorch
loss = nn.CrossEntropyLoss(reduction=’none’)
```{.python .input}
#@tab tensorflow
def loss(y_hat, y):
return tf.losses.sparse_categorical_crossentropy(
y, y_hat, from_logits=True)
训练
幸运的是,[多层感知机的训练过程与softmax回归的训练过程完全相同]。
可以直接调用d2l
包的train_ch3
函数(参见 :numref:sec_softmax_scratch
),
将迭代周期数设置为10,并将学习率设置为0.1.
```{.python .input} num_epochs, lr = 10, 0.1 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, lambda batch_size: d2l.sgd(params, lr, batch_size))
```{.python .input}
#@tab pytorch
num_epochs, lr = 10, 0.1
updater = torch.optim.SGD(params, lr=lr)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
```{.python .input}
@tab tensorflow
num_epochs, lr = 10, 0.1 updater = d2l.Updater([W1, W2, b1, b2], lr) d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
为了对学习到的模型进行评估,我们将[**在一些测试数据上应用这个模型**]。
```{.python .input}
#@tab all
d2l.predict_ch3(net, test_iter)
小结
- 手动实现一个简单的多层感知机是很容易的。然而如果有大量的层,从零开始实现多层感知机会变得很麻烦(例如,要命名和记录模型的参数)。
练习
- 在所有其他参数保持不变的情况下,更改超参数
num_hiddens
的值,并查看此超参数的变化对结果有何影响。确定此超参数的最佳值。 - 尝试添加更多的隐藏层,并查看它对结果有何影响。
- 改变学习速率会如何影响结果?保持模型架构和其他超参数(包括轮数)不变,学习率设置为多少会带来最好的结果?
- 通过对所有超参数(学习率、轮数、隐藏层数、每层的隐藏单元数)进行联合优化,可以得到的最佳结果是什么?
- 描述为什么涉及多个超参数更具挑战性。
- 如果要构建多个超参数的搜索方法,你能想到的最聪明的策略是什么?
:begin_tab:mxnet
Discussions
:end_tab:
:begin_tab:pytorch
Discussions
:end_tab:
:begin_tab:tensorflow
Discussions
:end_tab: