深度学习基础:感知机与神经网络

本文是深度学习系列笔记的第一篇,涵盖感知机模型、激活函数与反向传播算法。

1. 感知机(Perceptron)

感知机是最简单的神经元模型,由 Rosenblatt 在 1958 年提出。

给定输入向量 $\mathbf{x} = (x_1, x_2, \ldots, x_n)$ 和权重向量 $\mathbf{w}$,感知机输出:

$$y = f\left(\sum_{i=1}^n w_i x_i + b\right)$$

其中 $f$ 是激活函数,$b$ 是偏置项。

2. 常见激活函数

函数 公式 特点
Sigmoid $\sigma(x) = \frac{1}{1+e^{-x}}$ 输出范围 (0,1),梯度消失
ReLU $f(x) = \max(0, x)$ 计算高效,不饱和
Tanh $f(x) = \tanh(x)$ 输出范围 (-1,1)

3. 多层感知机(MLP)

多层感知机由输入层、若干隐藏层和输出层组成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import torch
import torch.nn as nn

class MLP(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, output_dim)
)

def forward(self, x):
return self.layers(x)

4. 反向传播算法

反向传播(Backpropagation)利用链式法则计算梯度:

$$\frac{\partial L}{\partial w_i} = \frac{\partial L}{\partial y} \cdot \frac{\partial y}{\partial w_i}$$

小结

  • 感知机是神经网络的基础单元
  • 激活函数引入非线性,使网络能拟合复杂函数
  • 反向传播是训练神经网络的核心算法

下一篇:卷积神经网络(CNN)原理


参考资料:《深度学习》- Goodfellow et al.