You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

训练MMOE/PLE多任务推荐网络时,如何用TensorBoard可视化GATE张量并观测任务区分能力?

在TensorBoard中观测MMoE/PLE门控网络的任务区分能力

要验证门控网络对不同任务样本的区分能力,核心是记录不同样本类型对应的门控权重分布(而非仅观测门控层参数,参数本身无法直接反映对样本的区分性),具体实现步骤如下:

1. 提取门控网络的样本级权重

在模型前向传播过程中,除计算任务输出外,需额外留存门控层给各专家的权重值。比如针对CLICK和PAY两类专家,每个样本会得到两个权重(对应给CLICK专家的权重、给PAY专家的权重),将这些权重与样本的任务标签(点击正/负、付费正/负)关联存储。

2. 按样本类型分组权重数据

将提取到的权重按样本标签分组,至少区分以下类别:

  • 点击正样本:对应CLICK专家的权重、对应PAY专家的权重
  • 付费正样本:对应CLICK专家的权重、对应PAY专家的权重
  • (可选)负样本(点击/付费均为负):两类专家的权重

分组是为了后续能直观对比不同样本类型下的权重差异。

3. 用TensorBoard可视化权重分布

根据需求选择合适的可视化方式:

直方图(Histogram)

为每个分组的权重创建独立直方图,命名示例:

  • gate_weights/click_positive/click_expert
  • gate_weights/click_positive/pay_expert
  • gate_weights/pay_positive/click_expert
  • gate_weights/pay_positive/pay_expert

观测逻辑:

  • 门控有效:点击正样本的CLICK专家权重均值显著高于PAY专家,付费正样本则相反
  • 门控失效:两类样本的两类专家权重均值接近,分布重叠严重

散点图(Scatter Plot)

将每个样本的CLICK专家权重作为X轴,PAY专家权重作为Y轴,按样本标签给点上色(比如点击正标红色,付费正标蓝色)。

  • 门控有效:两类正样本的点会分别聚集在「高X低Y」和「低X高Y」区域
  • 门控失效:所有样本点集中在对角线(X=Y)附近,无明显聚类

4. 代码示例(以TensorFlow为例)

import tensorflow as tf

# 假设已得到门控权重gating_weights(shape: [batch_size, 2],第0列是CLICK专家权重,第1列是PAY专家权重)
# 假设样本标签:click_label(1为正,0为负)、pay_label(1为正,0为负)
click_pos_mask = tf.equal(click_label, 1)
pay_pos_mask = tf.equal(pay_label, 1)

# 筛选对应样本的权重
click_pos_click_weights = tf.boolean_mask(gating_weights[:, 0], click_pos_mask)
click_pos_pay_weights = tf.boolean_mask(gating_weights[:, 1], click_pos_mask)
pay_pos_click_weights = tf.boolean_mask(gating_weights[:, 0], pay_pos_mask)
pay_pos_pay_weights = tf.boolean_mask(gating_weights[:, 1], pay_pos_mask)

# 写入TensorBoard日志
with tf.summary.create_file_writer('./tb_logs').as_default():
    tf.summary.histogram('gate_weights/click_pos/click_expert', click_pos_click_weights, step=current_epoch)
    tf.summary.histogram('gate_weights/click_pos/pay_expert', click_pos_pay_weights, step=current_epoch)
    tf.summary.histogram('gate_weights/pay_pos/click_expert', pay_pos_click_weights, step=current_epoch)
    tf.summary.histogram('gate_weights/pay_pos/pay_expert', pay_pos_pay_weights, step=current_epoch)

    # 可选:写入均值散点数据,方便趋势观测
    if tf.size(click_pos_click_weights) > 0:
        tf.summary.scalar('scatter/click_pos_click_mean', tf.reduce_mean(click_pos_click_weights), step=current_epoch)
        tf.summary.scalar('scatter/click_pos_pay_mean', tf.reduce_mean(click_pos_pay_weights), step=current_epoch)
    if tf.size(pay_pos_click_weights) > 0:
        tf.summary.scalar('scatter/pay_pos_click_mean', tf.reduce_mean(pay_pos_click_weights), step=current_epoch)
        tf.summary.scalar('scatter/pay_pos_pay_mean', tf.reduce_mean(pay_pos_pay_weights), step=current_epoch)

若使用PyTorch,可通过torch.utils.tensorboard.SummaryWriter实现相同逻辑,只需将张量转为numpy数组后写入即可。

5. 最终判断逻辑

  • 有效门控:点击正样本的CLICK专家权重大幅高于PAY专家,付费正样本则完全相反,直方图无重叠,散点图聚类明显
  • 无效门控:两类样本的两类专家权重均值接近,直方图重叠严重,散点图集中在对角线区域

内容的提问来源于stack exchange,提问作者sempre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 13:40:40