关键词:参数初始化、Xavier / He 初始值、对称性破坏、损失函数、MAE / MSE / CrossEntropy / BCE
一、参数初始化
1.1 固定值初始化(仅演示,权重不要用)
方法
代码
缺陷
全零
nn.init.zeros_(w)
对称性未被破坏,所有神经元等价
全一
nn.init.ones_(w)
同上,且激活后输出恒等
任意常数
nn.init.constant_(w, val)
仍无法打破对称性
python复制代码
import torch.nn as nn
fc = nn.Linear(4, 3)
nn.init.zeros_(fc.weight) # 仅偏置可用
1.2 随机初始化(打破对称性的起点)
分布
代码
方差公式
备注
均匀
nn.init.uniform_(w, a, b)
( b − a ) 2 12 \displaystyle \frac{(b-a)^2}{12} 12(b−a)2
需手动调区间
正态
nn.init.normal_(w, mean, std)
σ 2 \displaystyle \sigma^2 σ2
std 难校准
局限:未考虑前向/反向方差,深层网络仍需"自适应"方法。
1.3 Xavier(Glorot)初始化 ------ 均衡前向与反向方差
激活函数
数学依据
PyTorch API
Sigmoid / Tanh
Var ( W ) = 2 n in + n out \displaystyle \text{Var}(W)=\frac{2}{n_{\text{in}}+n_{\text{out}}} Var(W)=nin+nout2
xavier_uniform_ / xavier_normal_
均匀区间 : [ − 6 n in + n out , 6 n in + n out ] [-\sqrt{\frac{6}{n_{\text{in}}+n_{\text{out}}}},\; \sqrt{\frac{6}{n_{\text{in}}+n_{\text{out}}}}] [−nin+nout6 ,nin+nout6 ]
正态方差 : 2 n in + n out \frac{2}{n_{\text{in}}+n_{\text{out}}} nin+nout2
公式:
L = − 1 N ∑ i log e x i , y i − max ( x i ) ∑ j e x i , j − max ( x i ) \displaystyle \mathcal{L}=-\frac{1}{N}\sum_{i}\log\frac{e^{x_{i,y_i}-\max(x_i)}}{\sum_j e^{x_{i,j}-\max(x_i)}} L=−N1i∑log∑jexi,j−max(xi)exi,yi−max(xi)
公式:
− 1 N ∑ i [ y i log y ^ i + ( 1 − y i ) log ( 1 − y ^ i ) ] \displaystyle -\frac{1}{N}\sum_{i}\left[y_i\log\hat y_i+(1-y_i)\log(1-\hat y_i)\right] −N1i∑[yilogy^i+(1−yi)log(1−y^i)]