B = [ B 1 , B 2 , ... , B N ] T ∈ R N × 1 \boldsymbol B = [B_1,B_2,\dots,B_N]^T \in \mathbb{R}^{N \times 1} B=[B1,B2,...,BN]T∈RN×1, W = [ W 1 , W 2 , ... , W N ] T ∈ R N × N r \boldsymbol W = [\boldsymbol W_1,\boldsymbol W_2,\dots,\boldsymbol W_N]^T \in \mathbb{R}^{N \times N_r} W=[W1,W2,...,WN]T∈RN×Nr, H = [ H 1 , H 2 , ... , H N ] T ∈ R N × N r \boldsymbol H = [\boldsymbol H_1,\boldsymbol H_2,\dots,\boldsymbol H_N]^T \in \mathbb{R}^{N \times N_r} H=[H1,H2,...,HN]T∈RN×Nr
将 ∑ n ∈ N ( W n H n B n ) \sum_{n \in \mathcal{N}}(\boldsymbol W_n \boldsymbol H_nB_n) ∑n∈N(WnHnBn)写成矩阵形式
∑ n ∈ N ( W n H n B n ) = Tr ( W ⊤ diag ( B ) H ) \sum_{n \in \mathcal{N}} (\boldsymbol{W}_n \boldsymbol{H}_n B_n) =\text{Tr}(\boldsymbol{W}^\top \text{diag}(\boldsymbol{B}) \boldsymbol{H}) ∑n∈N(WnHnBn)=Tr(W⊤diag(B)H)
代码验证
matlab
% 定义参数
N = 5; % 样本数
Nr = 3; % 每个向量的列维度
% 随机生成矩阵和向量
W = rand(N, Nr); % N x Nr 矩阵
H = rand(N, Nr); % N x Nr 矩阵
B = rand(N, 1); % N x 1 列向量
% 逐项求和方式计算
sum_result = 0;
for n = 1:N
sum_result = sum_result + W(n, :) * H(n, :)' * B(n);
end
% 矩阵形式计算
diag_B = diag(B); % 对角矩阵
trace_result = trace(W' * diag_B * H);
% 显示结果
disp('逐项求和结果:');
disp(sum_result);
disp('矩阵形式结果:');
disp(trace_result);
% 验证是否相等
if abs(sum_result - trace_result) < 1e-10
disp('验证成功:两者相等!');
else
disp('验证失败:两者不相等!');
end