
线性代数:AI开发者的数学工具箱
一、为什么AI开发者需要线性代数?
1.1 线性代数在AI中的无处不在
python
import numpy as np
import matplotlib.pyplot as plt
print("=" * 60)
print("线性代数在AI中的应用场景")
print("=" * 60)
applications = {
"神经网络": "每一层都是矩阵乘法:output = activation(W @ X + b)",
"词向量": "单词被表示为高维空间中的向量",
"图像处理": "图像是三维张量(高×宽×通道)",
"PCA降维": "基于特征值分解找到主要方向",
"推荐系统": "用户和物品被表示为向量,相似度用点积计算",
"卷积操作": "在张量上滑动窗口进行运算"
}
for app, desc in applications.items():
print(f"\n📌 {app}:")
print(f" {desc}")
# 直观示例:用向量表示单词
word_vectors = {
"king": [0.5, 0.8, 0.2, 0.9],
"queen": [0.5, 0.7, 0.3, 0.8],
"man": [0.4, 0.6, 0.1, 0.3],
"woman": [0.4, 0.5, 0.2, 0.4]
}
# 经典的词向量关系:king - man + woman ≈ queen
king = np.array(word_vectors["king"])
man = np.array(word_vectors["man"])
woman = np.array(word_vectors["woman"])
queen = np.array(word_vectors["queen"])
result = king - man + woman
print(f"\n✨ 词向量运算演示:")
print(f" king - man + woman = {result[:2]}...")
print(f" queen向量 = {queen[:2]}...")
print(f" 这就是为什么 AI 能理解 '国王-男人+女人≈女王'")
二、向量:数据的原子单位
2.1 什么是向量?
向量 = 有方向和大小的量,在AI中就是一组数字的列表
python
# 向量的表示
# 一个样本可以用向量表示
house_features = np.array([120, # 面积(平方米)
3, # 卧室数
2, # 卫生间数
2015, # 建造年份
85]) # 位置评分
print("🏠 房子的特征向量:")
print(f" {house_features}")
print(f" 向量的维度: {house_features.shape}")
print(f" 向量的长度(模): {np.linalg.norm(house_features):.2f}")
# 向量的几何意义:二维空间中的点
points = np.array([[1, 2], [3, 1], [2, 4], [4, 3]])
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.quiver(0, 0, 1, 2, angles='xy', scale_units='xy', scale=1, color='r', width=0.02)
plt.quiver(0, 0, 3, 1, angles='xy', scale_units='xy', scale=1, color='b', width=0.02)
plt.xlim(-1, 5)
plt.ylim(-1, 5)
plt.grid(True, alpha=0.3)
plt.title("向量在二维空间中的表示")
plt.xlabel("x")
plt.ylabel("y")
plt.subplot(1, 2, 2)
plt.scatter(points[:, 0], points[:, 1], c='purple', s=100)
for i, point in enumerate(points):
plt.annotate(f" 点{i+1}", point)
plt.xlim(0, 5)
plt.ylim(0, 5)
plt.grid(True, alpha=0.3)
plt.title("数据点就是向量")
plt.xlabel("特征1")
plt.ylabel("特征2")
plt.tight_layout()
plt.show()
2.2 向量的基本运算及其AI意义
python
# === 向量加法:特征组合 ===
# 两个商品的特征向量相加 = 购物篮表示
product1 = np.array([1, 0, 1, 0]) # [苹果, 香蕉, 橙子, 葡萄]
product2 = np.array([0, 1, 0, 1])
cart = product1 + product2
print(f"🛒 购物篮向量: {cart}")
# === 向量数乘:缩放特征 ===
original_price = np.array([100, 200, 150]) # 三个商品原价
discount = 0.8 # 8折
sale_price = discount * original_price
print(f"💰 折扣后价格: {sale_price}")
# === 向量点积:相似度计算 ===
def cosine_similarity(v1, v2):
"""余弦相似度:衡量两个向量的方向是否一致"""
dot_product = np.dot(v1, v2)
norm1 = np.linalg.norm(v1)
norm2 = np.linalg.norm(v2)
return dot_product / (norm1 * norm2)
# 用户对电影的评分向量
user1_ratings = np.array([5, 4, 3, 0, 2]) # 用户1对5部电影的评分
user2_ratings = np.array([4, 4, 3, 1, 2]) # 用户2
user3_ratings = np.array([1, 2, 5, 4, 5]) # 用户3
similarity_12 = cosine_similarity(user1_ratings, user2_ratings)
similarity_13 = cosine_similarity(user1_ratings, user3_ratings)
print(f"\n📊 用户相似度:")
print(f" 用户1和用户2相似度: {similarity_12:.3f} (喜欢相似电影)")
print(f" 用户1和用户3相似度: {similarity_13:.3f} (品味不同)")
# === 向量在推荐系统中的应用 ===
def recommend_items(user_vector, item_vectors, top_k=2):
"""基于向量相似度的推荐"""
similarities = []
for item_name, item_vec in item_vectors.items():
sim = cosine_similarity(user_vector, item_vec)
similarities.append((item_name, sim))
similarities.sort(key=lambda x: x[1], reverse=True)
return similarities[:top_k]
# 用户兴趣向量(喜欢动作片,不喜欢爱情片)
user_interest = np.array([0.9, 0.1, 0.2, 0.8]) # [动作, 爱情, 科幻, 喜剧]
# 电影特征向量
movies = {
"终结者": np.array([0.9, 0.1, 0.7, 0.3]),
"泰坦尼克号": np.array([0.1, 0.9, 0.1, 0.2]),
"阿凡达": np.array([0.7, 0.2, 0.9, 0.4]),
"大话西游": np.array([0.6, 0.6, 0.2, 0.8])
}
recommendations = recommend_items(user_interest, movies)
print(f"\n🎬 基于向量相似度的推荐:")
for movie, score in recommendations:
print(f" 推荐: {movie} (相似度: {score:.3f})")
三、矩阵:批处理的艺术
3.1 什么是矩阵?
矩阵 = 向量的集合,表格化数据
python
# 数据集 = 矩阵
# 行:样本,列:特征
dataset = np.array([
[25, 70000, 2], # 样本1: 年龄, 工资, 经验
[30, 80000, 5], # 样本2
[35, 90000, 8], # 样本3
[28, 75000, 3] # 样本4
])
print("📊 数据集矩阵:")
print(dataset)
print(f"\n形状: {dataset.shape}")
print(f"含义: {dataset.shape[0]}个样本,{dataset.shape[1]}个特征")
# 矩阵的转置:交换行和列
dataset_T = dataset.T
print(f"\n转置后形状: {dataset_T.shape}")
print("用途: 转置后每列是一个样本,便于按样本操作")
3.2 矩阵乘法(AI的核心运算)
python
# === 矩阵乘法的规则 ===
# (m × n) @ (n × p) = (m × p)
# 关键:第一个矩阵的列数 = 第二个矩阵的行数
print("=" * 60)
print("矩阵乘法规则详解")
print("=" * 60)
# 示例1:全连接层
batch_size = 32 # m = 32个样本
input_dim = 784 # n = 784个输入特征(如28x28图片)
output_dim = 256 # p = 256个输出神经元
# 输入数据:32张图片,每张784维
X = np.random.randn(batch_size, input_dim)
print(f"\n输入矩阵 X: {X.shape}")
# 权重矩阵:784个输入到256个输出
W = np.random.randn(input_dim, output_dim)
print(f"权重矩阵 W: {W.shape}")
# 偏置:256个神经元
b = np.random.randn(output_dim)
print(f"偏置向量 b: {b.shape}")
# 全连接层计算:Y = X @ W + b
Y = X @ W + b
print(f"输出矩阵 Y: {Y.shape}")
print(f"\n✅ 计算过程: ({batch_size}×{input_dim}) @ ({input_dim}×{output_dim}) = ({batch_size}×{output_dim})")
# === 可视化矩阵乘法 ===
plt.figure(figsize=(12, 5))
# 左矩阵
plt.subplot(1, 3, 1)
plt.imshow(np.random.rand(4, 6), cmap='Blues', aspect='auto')
plt.title(f"输入矩阵\n(4个样本×6特征)")
plt.colorbar()
# 乘号
plt.subplot(1, 3, 2)
plt.text(0.5, 0.5, '×', fontsize=40, ha='center', va='center')
plt.axis('off')
# 右矩阵
plt.subplot(1, 3, 3)
plt.imshow(np.random.rand(6, 3), cmap='Oranges', aspect='auto')
plt.title(f"权重矩阵\n(6特征×3输出)")
plt.colorbar()
plt.suptitle("矩阵乘法:输入 × 权重 = 输出", fontsize=14)
plt.tight_layout()
plt.show()
# === 实战:批量预测 ===
def batch_predict(X, W, b):
"""批量预测:一次性计算所有样本的输出"""
return X @ W + b
# 模拟1000个样本
X_test = np.random.randn(1000, 784)
W = np.random.randn(784, 10) # 10个类别
b = np.random.randn(10)
# 批量预测
predictions = batch_predict(X_test, W, b)
print(f"\n🚀 批量预测结果: {predictions.shape}")
print(f" 一次计算了{1000}个样本的预测值!")
四、张量:高维数据的表示
4.1 什么是张量?
张量 = 多维数组,标量(0维) → 向量(1维) → 矩阵(2维) → 张量(3维及以上)
python
# 不同维度的张量
scalar = np.array(42) # 0维:标量
vector = np.array([1, 2, 3]) # 1维:向量
matrix = np.array([[1, 2], [3, 4]]) # 2维:矩阵
tensor_3d = np.random.rand(3, 4, 5) # 3维:张量
tensor_4d = np.random.rand(32, 224, 224, 3) # 4维:批量图像
print("📐 张量的维度层级:")
print(f" 标量(0维): {scalar.shape}")
print(f" 向量(1维): {vector.shape}")
print(f" 矩阵(2维):\n{matrix}")
print(f" 3维张量: {tensor_3d.shape}")
print(f" 4维张量: {tensor_4d.shape}")
# === AI中的常见张量 ===
data_shapes = {
"灰度图像": (224, 224), # 2维:高×宽
"彩色图像": (224, 224, 3), # 3维:高×宽×通道
"视频帧": (32, 224, 224, 3), # 4维:批次×高×宽×通道
"文本序列": (64, 128, 512), # 3维:批次×序列长度×词向量维度
"注意力权重": (8, 12, 64, 64) # 4维:头数×批次×查询×键
}
print("\n🖼️ AI中的张量形状:")
for name, shape in data_shapes.items():
print(f" {name}: {shape}")
# === 图像作为张量 ===
# 创建一个模拟的彩色图像(10x10像素,3通道)
image = np.random.randint(0, 256, (10, 10, 3), dtype=np.uint8)
fig, axes = plt.subplots(1, 4, figsize=(12, 3))
axes[0].imshow(image)
axes[0].set_title("彩色图像\n(高×宽×3)")
axes[0].axis('off')
axes[1].imshow(image[:, :, 0], cmap='Reds')
axes[1].set_title("红色通道")
axes[1].axis('off')
axes[2].imshow(image[:, :, 1], cmap='Greens')
axes[2].set_title("绿色通道")
axes[2].axis('off')
axes[3].imshow(image[:, :, 2], cmap='Blues')
axes[3].set_title("蓝色通道")
axes[3].axis('off')
plt.suptitle("图像 = 三维张量(高度 × 宽度 × 颜色通道)", fontsize=14)
plt.tight_layout()
plt.show()
五、矩阵乘法的形状匹配规则
5.1 核心规则与常见模式
python
print("=" * 60)
print("矩阵乘法形状匹配规则")
print("=" * 60)
# 规则总结
print("""
📐 规则: (m × n) @ (n × p) = (m × p)
- 第一个矩阵的列数 = 第二个矩阵的行数
- 结果的形状: (第一个的行数 × 第二个的列数)
""")
# 示例1:向量 × 矩阵
v = np.array([1, 2, 3]) # shape: (3,)
M = np.array([[1, 2], [3, 4], [5, 6]]) # shape: (3, 2)
result = v @ M # 自动将v视为(1,3),结果是(1,2)
print(f"\n1️⃣ 向量 × 矩阵:")
print(f" {v.shape} @ {M.shape} = {result.shape}")
# 示例2:矩阵 × 向量
M = np.array([[1, 2, 3], [4, 5, 6]]) # shape: (2, 3)
v = np.array([1, 2, 3]) # shape: (3,)
result = M @ v # 结果是(2,)
print(f"\n2️⃣ 矩阵 × 向量:")
print(f" {M.shape} @ {v.shape} = {result.shape}")
# 示例3:批量矩阵乘法
A = np.random.randn(32, 10, 20) # 32个10x20矩阵
B = np.random.randn(32, 20, 15) # 32个20x15矩阵
# 批量乘法:对应位置的矩阵相乘
result = A @ B # 形状: (32, 10, 15)
print(f"\n3️⃣ 批量矩阵乘法:")
print(f" {A.shape} @ {B.shape} = {result.shape}")
# === AI中的常见形状模式 ===
patterns = {
"全连接层": " (batch, in_features) @ (in_features, out_features) = (batch, out_features)",
"自注意力": " (batch, seq, d_k) @ (d_k, d_k) = (batch, seq, d_k)",
"卷积展平": " (batch, channels, h, w) → 展平 → (batch, channels*h*w)",
"RNN隐藏状态": " (batch, hidden) @ (hidden, hidden) = (batch, hidden)"
}
print("\n📌 AI中的常见形状模式:")
for name, pattern in patterns.items():
print(f" {name}: {pattern}")
# === 错误示例 ===
try:
A = np.random.randn(10, 5)
B = np.random.randn(10, 5) # 形状不匹配
C = A @ B
except ValueError as e:
print(f"\n❌ 常见错误: {e}")
print(" 解决方法: 确保第一个矩阵的列数 = 第二个矩阵的行数")
六、转置与逆矩阵
6.1 转置:交换维度
python
# 转置在AI中的应用
# 1. 调整维度以匹配矩阵乘法
X = np.random.randn(32, 784) # 输入
W = np.random.randn(784, 256) # 权重
# 如果需要使用不同的权重形状
W_T = W.T # 变成 (256, 784)
print(f"转置前: {W.shape}")
print(f"转置后: {W_T.shape}")
# 2. 计算协方差矩阵
# 数据中心化后,协方差矩阵 = (1/n) * X.T @ X
X_centered = X - X.mean(axis=0)
cov_matrix = (X_centered.T @ X_centered) / (len(X) - 1)
print(f"\n协方差矩阵形状: {cov_matrix.shape}") # (784, 784)
# 3. 神经网络中的梯度传播
# 反向传播时需要用转置来匹配维度
dY = np.random.randn(32, 256) # 上游梯度
W = np.random.randn(784, 256) # 权重
dX = dY @ W.T # 梯度传播到输入
print(f"\n反向传播梯度形状: {dX.shape}") # (32, 784)
6.2 逆矩阵:解方程组
python
# 逆矩阵的概念:A @ A^(-1) = I
# 只有方阵且满秩才有逆矩阵
# === 应用1:线性回归的闭式解 ===
# 正规方程: w = (X^T·X)^(-1)·X^T·y
# 生成数据
np.random.seed(42)
X = np.random.randn(100, 3) # 100个样本,3个特征
true_w = np.array([2, -1, 0.5])
y = X @ true_w + np.random.randn(100) * 0.1
# 正规方程求解
X_with_bias = np.column_stack([np.ones(100), X]) # 添加偏置项
w_hat = np.linalg.inv(X_with_bias.T @ X_with_bias) @ X_with_bias.T @ y
print(f"\n📈 线性回归闭式解:")
print(f" 真实权重: {np.concatenate([[0.5], true_w])}") # 偏置+系数
print(f" 估计权重: {w_hat}")
# === 应用2:求解线性方程组 ===
# 3个方程,3个未知数
# 2x + y = 8
# 5x + 3y = 19
A = np.array([[2, 1], [5, 3]])
b = np.array([8, 19])
x = np.linalg.solve(A, b) # 等价于 inv(A) @ b
print(f"\n🔢 解方程组:")
print(f" x = {x[0]:.0f}, y = {x[1]:.0f}")
# === 注意事项:不要直接求逆 ===
# 数值不稳定,使用 solve 或 lstsq 替代
print(f"\n⚠️ 重要提示:")
print(" 实际AI开发中很少直接求逆矩阵!")
print(" - 数值不稳定")
print(" - 计算复杂度高 O(n³)")
print(" - 推荐使用 np.linalg.solve() 或 np.linalg.lstsq()")
七、特征值与特征向量:PCA降维的原理
7.1 直观理解
特征向量 = 数据的主要方向
特征值 = 该方向上的重要性
python
# 生成二维数据(椭圆形分布)
np.random.seed(42)
n_points = 500
angle = np.pi / 6 # 30度倾斜
rotation = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
data = np.random.randn(n_points, 2) @ rotation.T
data[:, 0] *= 3 # x方向拉伸
data[:, 1] *= 1 # y方向
# 计算协方差矩阵和特征值分解
cov_matrix = np.cov(data.T)
eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
# 可视化
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.scatter(data[:, 0], data[:, 1], alpha=0.5, s=20)
plt.axis('equal')
# 绘制特征向量
center = np.mean(data, axis=0)
for i in range(2):
vec = eigenvectors[:, i] * np.sqrt(eigenvalues[i]) * 2
plt.arrow(center[0], center[1], vec[0], vec[1],
color='red' if i == 0 else 'blue',
width=0.05, head_width=0.2,
label=f'特征向量{i+1} (λ={eigenvalues[i]:.2f})')
plt.title("PCA:找到数据的主要方向")
plt.xlabel("特征1")
plt.ylabel("特征2")
plt.legend()
plt.grid(True, alpha=0.3)
# PCA降维效果
plt.subplot(1, 2, 2)
# 投影到第一主成分
pca_projection = data @ eigenvectors[:, 0]
# 重建为1维(沿主成分方向)
reconstructed = np.outer(pca_projection, eigenvectors[:, 0])
plt.scatter(reconstructed[:, 0], reconstructed[:, 1], alpha=0.5, s=20)
plt.axis('equal')
plt.title(f"降维到1维(保留{eigenvalues[0]/eigenvalues.sum()*100:.1f}%信息)")
plt.xlabel("主成分1")
plt.ylabel("主成分2")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
print(f"\n📊 特征值分析:")
print(f" 特征值1: {eigenvalues[0]:.2f} (重要性: {eigenvalues[0]/eigenvalues.sum()*100:.1f}%)")
print(f" 特征值2: {eigenvalues[1]:.2f} (重要性: {eigenvalues[1]/eigenvalues.sum()*100:.1f}%)")
print(f" 总方差: {eigenvalues.sum():.2f}")
7.2 PCA完整实现与可视化
python
from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
# 加载手写数字数据集
digits = load_digits()
X = digits.data # (1797, 64) - 64维特征
y = digits.target
print("=" * 60)
print("PCA降维实战:手写数字识别")
print("=" * 60)
print(f"原始数据形状: {X.shape} (64维)")
# === 实现PCA ===
def my_pca(X, n_components):
"""手动实现PCA"""
# 1. 标准化
X_centered = X - X.mean(axis=0)
# 2. 计算协方差矩阵
cov_matrix = X_centered.T @ X_centered / (len(X) - 1)
# 3. 特征值分解
eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
# 4. 按特征值降序排列
idx = np.argsort(eigenvalues)[::-1]
eigenvalues = eigenvalues[idx]
eigenvectors = eigenvectors[:, idx]
# 5. 选择前n_components个主成分
selected_eigenvectors = eigenvectors[:, :n_components]
# 6. 投影
X_pca = X_centered @ selected_eigenvectors
return X_pca, eigenvalues, eigenvectors
# 降到2维和3维
X_pca_2d, eigenvalues, _ = my_pca(X, 2)
X_pca_3d, _, _ = my_pca(X, 3)
print(f"\n降维后:")
print(f" 2维: {X_pca_2d.shape}")
print(f" 3维: {X_pca_3d.shape}")
# 计算累积方差贡献率
variance_ratio = eigenvalues[:10] / eigenvalues.sum()
cumulative_ratio = np.cumsum(variance_ratio)
# 可视化
fig = plt.figure(figsize=(15, 5))
# 1. 方差贡献率
ax1 = fig.add_subplot(1, 3, 1)
ax1.bar(range(1, 11), variance_ratio[:10], alpha=0.7, label='单个')
ax1.plot(range(1, 11), cumulative_ratio[:10], 'ro-', label='累积')
ax1.set_xlabel('主成分数量')
ax1.set_ylabel('方差贡献率')
ax1.set_title('主成分方差贡献率')
ax1.legend()
ax1.grid(True, alpha=0.3)
# 2. 2D可视化
ax2 = fig.add_subplot(1, 3, 2)
scatter = ax2.scatter(X_pca_2d[:, 0], X_pca_2d[:, 1],
c=y, cmap='tab10', alpha=0.6, s=10)
ax2.set_xlabel('第一主成分')
ax2.set_ylabel('第二主成分')
ax2.set_title('PCA降维到2维')
plt.colorbar(scatter, ax=ax2)
# 3. 3D可视化
ax3 = fig.add_subplot(1, 3, 3, projection='3d')
scatter = ax3.scatter(X_pca_3d[:, 0], X_pca_3d[:, 1], X_pca_3d[:, 2],
c=y, cmap='tab10', alpha=0.6, s=10)
ax3.set_xlabel('PC1')
ax3.set_ylabel('PC2')
ax3.set_zlabel('PC3')
ax3.set_title('PCA降维到3维')
plt.colorbar(scatter, ax=ax3)
plt.suptitle('PCA降维:64维 → 2/3维,仍能区分不同数字', fontsize=14)
plt.tight_layout()
plt.show()
print(f"\n📈 累积方差贡献率:")
for i, (var, cum) in enumerate(zip(variance_ratio[:10], cumulative_ratio[:10]), 1):
print(f" 前{i}个主成分: {cum*100:.1f}%")
八、实战:完整的PCA图像压缩
python
# 使用PCA进行图像压缩
from sklearn.datasets import load_sample_image
# 加载示例图像
china = load_sample_image("china.jpg")
print(f"原始图像形状: {china.shape}")
# 转换为浮点数并标准化
img = china / 255.0
h, w, c = img.shape
# 将图像重塑为 (像素数, 通道数)
img_reshaped = img.reshape(-1, c)
print(f"重塑后形状: {img_reshaped.shape}")
# 对每个通道进行PCA
def pca_compress_image(img, n_components):
"""使用PCA压缩图像"""
h, w, c = img.shape
img_reshaped = img.reshape(-1, c)
compressed_channels = []
for channel in range(c):
# 对该通道的数据进行PCA
X = img_reshaped[:, channel:channel+1]
# 中心化
X_centered = X - X.mean()
# 计算协方差矩阵的特征向量
# 对于一维数据,主成分就是自身
# 这里简化:用SVD代替
U, S, Vt = np.linalg.svd(X_centered, full_matrices=False)
# 只保留前n_components个奇异值
S_compressed = np.zeros(n_components)
S_compressed[:min(n_components, len(S))] = S[:min(n_components, len(S))]
# 重建
X_reconstructed = U[:, :n_components] @ np.diag(S_compressed) @ Vt[:n_components, :]
X_reconstructed = X_reconstructed + X.mean()
compressed_channels.append(X_reconstructed)
# 合并通道
img_compressed = np.stack(compressed_channels, axis=1)
return img_compressed.reshape(h, w, c)
# 测试不同压缩率
components_list = [5, 20, 50, 100]
fig, axes = plt.subplots(1, len(components_list) + 1, figsize=(15, 4))
# 原始图像
axes[0].imshow(img)
axes[0].set_title(f"原始图像\n{img.shape[1]}x{img.shape[0]}")
axes[0].axis('off')
# 压缩后的图像
for i, n_comp in enumerate(components_list):
img_compressed = pca_compress_image(img, n_comp)
img_compressed = np.clip(img_compressed, 0, 1)
axes[i+1].imshow(img_compressed)
compression_ratio = (n_comp * 3) / (img.shape[1] * img.shape[0] * 3) * 100
axes[i+1].set_title(f"{n_comp}个主成分\n压缩率: {compression_ratio:.1f}%")
axes[i+1].axis('off')
plt.suptitle("PCA图像压缩:用少量主成分保留主要信息", fontsize=14)
plt.tight_layout()
plt.show()
九、学习检查清单
基础概念(必须掌握)
- 理解向量的几何意义和表示
- 掌握向量点积与相似度的关系
- 理解矩阵的维度和形状
- 掌握矩阵乘法的形状匹配规则
- 理解转置的作用
核心应用(重要)
- 神经网络中的矩阵乘法
- 批量数据处理(张量)
- PCA降维的原理
- 协方差矩阵与特征值分解
扩展了解(按需)
- 特征值分解的数学细节
- SVD分解
- 矩阵的秩
- 各种矩阵分解方法
十、总结
线性代数在AI中的核心价值:
| 概念 | AI应用 | 解决的问题 |
|---|---|---|
| 向量 | 样本表示、词嵌入 | 将数据转换为可计算的数值 |
| 矩阵 | 批量计算、权重存储 | 高效处理大量数据 |
| 张量 | 图像、视频、序列 | 表示高维结构化数据 |
| 矩阵乘法 | 神经网络前向传播 | 特征提取与变换 |
| 转置 | 梯度传播、维度匹配 | 调整数据流向 |
| 特征值分解 | PCA降维 | 数据压缩、去噪、可视化 |
记住:你不需要成为数学家,但理解这些概念能帮你:
- 调试模型(shape不匹配时知道为什么)
- 优化代码(用矩阵运算代替循环)
- 理解论文(看懂公式)
学习建议:
- 先用代码感受,再理解理论
- 多画图,直观理解几何意义
- 记住形状匹配规则就能解决80%的问题